@@ -39,15 +39,21 @@ extern "C" {
3939 new_size : usize ,
4040 ) -> * mut c_void ;
4141
42+ /// Return the available bytes in a memory block.
43+ pub fn sn_rust_usable_size ( p : * const c_void ) -> usize ;
44+ }
45+
46+ #[ cfg( feature = "libc-api" ) ]
47+ extern "C" {
4248 /// Allocate `count` items of `size` length each.
4349 /// Returns `null` if `count * size` overflows or on out-of-memory.
4450 /// All items are initialized to zero.
45- pub fn calloc ( count : usize , size : usize ) -> * mut c_void ;
51+ pub fn sn_calloc ( count : usize , size : usize ) -> * mut c_void ;
4652
4753 /// Allocate `size` bytes.
4854 /// Returns pointer to the allocated memory or null if out of memory.
4955 /// Returns a unique pointer if called with `size` 0.
50- pub fn malloc ( size : usize ) -> * mut c_void ;
56+ pub fn sn_malloc ( size : usize ) -> * mut c_void ;
5157
5258 /// Re-allocate memory to `newsize` bytes.
5359 /// Return pointer to the allocated memory or null if out of memory. If null
@@ -57,18 +63,19 @@ extern "C" {
5763 /// If `p` is null, it behaves as [`sn_malloc`]. If `newsize` is larger than
5864 /// the original `size` allocated for `p`, the bytes after `size` are
5965 /// uninitialized.
60- pub fn realloc ( p : * mut c_void , newsize : usize ) -> * mut c_void ;
66+ pub fn sn_realloc ( p : * mut c_void , newsize : usize ) -> * mut c_void ;
6167
6268 /// Free previously allocated memory.
6369 /// The pointer `p` must have been allocated before (or be null).
64- pub fn free ( p : * mut c_void ) ;
70+ pub fn sn_free ( p : * mut c_void ) ;
6571
6672 /// Return the available bytes in a memory block.
67- pub fn sn_rust_usable_size ( p : * const c_void ) -> usize ;
73+ pub fn sn_malloc_usable_size ( p : * const c_void ) -> usize ;
74+
6875}
6976
7077#[ cfg( test) ]
71- mod tests {
78+ mod rust_tests {
7279 use super :: * ;
7380
7481 #[ test]
@@ -90,19 +97,6 @@ mod tests {
9097 unsafe { sn_rust_dealloc ( ptr as * mut c_void , 8 , 8 ) } ;
9198 }
9299
93- #[ test]
94- fn it_frees_memory_sn_malloc ( ) {
95- let ptr = unsafe { malloc ( 8 ) } as * mut u8 ;
96- unsafe { free ( ptr as * mut c_void ) } ;
97- }
98-
99- #[ test]
100- fn it_frees_memory_sn_realloc ( ) {
101- let ptr = unsafe { malloc ( 8 ) } as * mut u8 ;
102- let ptr = unsafe { realloc ( ptr as * mut c_void , 8 ) } as * mut u8 ;
103- unsafe { free ( ptr as * mut c_void ) } ;
104- }
105-
106100 #[ test]
107101 fn it_reallocs_correctly ( ) {
108102 let mut ptr = unsafe { sn_rust_alloc ( 8 , 8 ) } as * mut u8 ;
@@ -126,3 +120,32 @@ mod tests {
126120 unsafe { sn_rust_dealloc ( ptr as * mut c_void , 32 , 8 ) } ;
127121 }
128122}
123+
124+ #[ cfg( all( test, feature = "libc-api" ) ) ]
125+ mod libc_tests {
126+ use super :: * ;
127+
128+ #[ test]
129+ fn it_frees_memory_sn_malloc ( ) {
130+ let ptr = unsafe { sn_malloc ( 8 ) } as * mut u8 ;
131+ unsafe { sn_free ( ptr as * mut c_void ) } ;
132+ }
133+
134+ #[ test]
135+ fn it_frees_memory_sn_realloc ( ) {
136+ let ptr = unsafe { sn_malloc ( 8 ) } as * mut u8 ;
137+ let ptr = unsafe { sn_realloc ( ptr as * mut c_void , 8 ) } as * mut u8 ;
138+ unsafe { sn_free ( ptr as * mut c_void ) } ;
139+ }
140+
141+ #[ test]
142+ fn it_calculates_malloc_usable_size ( ) {
143+ let ptr = unsafe { sn_malloc ( 32 ) } as * mut u8 ;
144+ let usable_size = unsafe { sn_malloc_usable_size ( ptr as * mut c_void ) } ;
145+ assert ! (
146+ usable_size >= 32 ,
147+ "usable_size should at least equal to the allocated size"
148+ ) ;
149+ unsafe { sn_free ( ptr as * mut c_void ) } ;
150+ }
151+ }
0 commit comments