@@ -31,6 +31,36 @@ extern "C" {
3131 /// - `alignment` fulfills all the requirements as `rust_alloc`
3232 /// The program may be forced to abort if the constrains are not full-filled.
3333 pub fn rust_realloc ( ptr : * mut c_void , alignment : size_t , old_size : size_t , new_size : size_t ) -> * mut c_void ;
34+
35+ /// Allocate `count` items of `size` length each.
36+ ///
37+ /// Returns `null` if `count * size` overflows or on out-of-memory.
38+ ///
39+ /// All items are initialized to zero.
40+ pub fn sn_calloc ( count : usize , size : usize ) -> * mut c_void ;
41+
42+ /// Allocate `size` bytes.
43+ ///
44+ /// Returns pointer to the allocated memory or null if out of memory.
45+ /// Returns a unique pointer if called with `size` 0.
46+ pub fn sn_malloc ( size : usize ) -> * mut c_void ;
47+
48+ /// Re-allocate memory to `newsize` bytes.
49+ ///
50+ /// Return pointer to the allocated memory or null if out of memory. If null
51+ /// is returned, the pointer `p` is not freed. Otherwise the original
52+ /// pointer is either freed or returned as the reallocated result (in case
53+ /// it fits in-place with the new size).
54+ ///
55+ /// If `p` is null, it behaves as [`mi_malloc`]. If `newsize` is larger than
56+ /// the original `size` allocated for `p`, the bytes after `size` are
57+ /// uninitialized.
58+ pub fn sn_realloc ( p : * mut c_void , newsize : usize ) -> * mut c_void ;
59+
60+ /// Free previously allocated memory.
61+ ///
62+ /// The pointer `p` must have been allocated before (or be null).
63+ pub fn sn_free ( p : * mut c_void ) ;
3464}
3565
3666#[ cfg( test) ]
@@ -43,7 +73,19 @@ mod tests {
4373 unsafe { * ptr = 127 ; assert_eq ! ( * ptr, 127 ) } ;
4474 unsafe { rust_dealloc ( ptr as * mut c_void , 8 , 8 ) } ;
4575 }
46-
76+ #[ test]
77+ fn it_frees_memory_sn_malloc ( ) {
78+ let ptr = unsafe { sn_malloc ( 8 ) } as * mut u8 ;
79+ unsafe { sn_free ( ptr as * mut c_void ) } ;
80+ }
81+
82+ #[ test]
83+ fn it_frees_memory_sn_realloc ( ) {
84+ let ptr = unsafe { sn_malloc ( 8 ) } as * mut u8 ;
85+ let ptr = unsafe { sn_realloc ( ptr as * mut c_void , 8 ) } as * mut u8 ;
86+ unsafe { sn_free ( ptr as * mut c_void ) } ;
87+ }
88+
4789 #[ test]
4890 fn it_reallocs_correctly ( ) {
4991 let mut ptr = unsafe { rust_alloc ( 8 , 8 ) } as * mut u8 ;
@@ -52,4 +94,4 @@ mod tests {
5294 unsafe { assert_eq ! ( * ptr, 127 ) } ;
5395 unsafe { rust_dealloc ( ptr as * mut c_void , 8 , 16 ) } ;
5496 }
55- }
97+ }
0 commit comments