@@ -82,23 +82,25 @@ unsafe fn alloc_helper(size: usize, zero: bool) -> *mut c_void {
8282/// # Safety
8383/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
8484#[ unsafe( no_mangle) ]
85- pub unsafe extern "C" fn malloc ( size : usize ) -> * mut c_void { unsafe {
86- alloc_helper ( size, false )
87- } }
85+ pub unsafe extern "C" fn malloc ( size : usize ) -> * mut c_void {
86+ unsafe { alloc_helper ( size, false ) }
87+ }
8888
8989/// Allocates a block of memory for an array of `nmemb` elements, each of `size` bytes.
9090/// The memory is initialized to 0s.
9191///
9292/// # Safety
9393/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
9494#[ unsafe( no_mangle) ]
95- pub unsafe extern "C" fn calloc ( nmemb : usize , size : usize ) -> * mut c_void { unsafe {
96- let total_size = nmemb
97- . checked_mul ( size)
98- . expect ( "nmemb * size should not overflow in calloc" ) ;
95+ pub unsafe extern "C" fn calloc ( nmemb : usize , size : usize ) -> * mut c_void {
96+ unsafe {
97+ let total_size = nmemb
98+ . checked_mul ( size)
99+ . expect ( "nmemb * size should not overflow in calloc" ) ;
99100
100- alloc_helper ( total_size, true )
101- } }
101+ alloc_helper ( total_size, true )
102+ }
103+ }
102104
103105/// Frees the memory block pointed to by `ptr`.
104106///
@@ -124,39 +126,37 @@ pub unsafe extern "C" fn free(ptr: *mut c_void) {
124126pub unsafe extern "C" fn realloc ( ptr : * mut c_void , size : usize ) -> * mut c_void {
125127 if ptr. is_null ( ) {
126128 // If the pointer is null, treat as a malloc
127- return unsafe { malloc ( size) } ;
129+ return unsafe { malloc ( size) } ;
128130 }
129131
130132 if size == 0 {
131133 // If the size is 0, treat as a free and return null
132- unsafe {
133- free ( ptr) ;
134+ unsafe {
135+ free ( ptr) ;
134136 }
135137 return ptr:: null_mut ( ) ;
136138 }
137139
138-
139- let total_new_size = size
140- . checked_add ( size_of :: < Layout > ( ) )
141- . expect ( "data and layout size should not overflow in realloc" ) ;
140+ let total_new_size = size
141+ . checked_add ( size_of :: < Layout > ( ) )
142+ . expect ( "data and layout size should not overflow in realloc" ) ;
142143
143- let block_start = unsafe { ( ptr as * const Layout ) . sub ( 1 ) } ;
144- let old_layout = unsafe { block_start. read ( ) } ;
145- let new_layout = Layout :: from_size_align ( total_new_size, MAX_ALIGN ) . unwrap ( ) ;
144+ let block_start = unsafe { ( ptr as * const Layout ) . sub ( 1 ) } ;
145+ let old_layout = unsafe { block_start. read ( ) } ;
146+ let new_layout = Layout :: from_size_align ( total_new_size, MAX_ALIGN ) . unwrap ( ) ;
146147
147- let new_block_start =
148- unsafe { alloc:: alloc:: realloc ( block_start as * mut u8 , old_layout, total_new_size) }
149- as * mut Layout ;
148+ let new_block_start =
149+ unsafe { alloc:: alloc:: realloc ( block_start as * mut u8 , old_layout, total_new_size) }
150+ as * mut Layout ;
150151
151- if new_block_start. is_null ( ) {
152- // Realloc failed
153- abort_with_code ( & [ ErrorCode :: MallocFailed as u8 ] ) ;
154- } else {
155- // Update the stored Layout, then return ptr to memory right after the Layout.
156- unsafe {
157- new_block_start. write ( new_layout) ;
158- new_block_start. add ( 1 ) as * mut c_void
159- }
152+ if new_block_start. is_null ( ) {
153+ // Realloc failed
154+ abort_with_code ( & [ ErrorCode :: MallocFailed as u8 ] ) ;
155+ } else {
156+ // Update the stored Layout, then return ptr to memory right after the Layout.
157+ unsafe {
158+ new_block_start. write ( new_layout) ;
159+ new_block_start. add ( 1 ) as * mut c_void
160160 }
161-
161+ }
162162}
0 commit comments