@@ -130,17 +130,23 @@ const NULL: *mut c_void = null_mut();
130130///
131131/// - `old_ptr` must point to a C allocation of at least `size` bytes.
132132/// - `ptr` must point to an allocation of at least `size` bytes.
133- pub unsafe fn try_move ( ptr : * mut c_void , old_ptr : * mut c_void , size : usize , old_align : usize ) {
133+ pub unsafe fn try_move (
134+ ptr : * mut c_void ,
135+ old_ptr : * mut c_void ,
136+ copy_count : usize ,
137+ old_align : usize ,
138+ old_size : usize
139+ ) {
134140 if ptr != NULL {
135141 // SAFETY: `ptr` validated nonnull, caller guarantees `old_ptr` is valid. caller guarantees
136142 // `size` is <= size of allocation at `ptr` and <= size of allocation at `old_ptr`,
137143 // so copying that many bytes is safe.
138144 unsafe {
139- memcpy ( ptr, old_ptr, size ) ;
145+ memcpy ( ptr, old_ptr, copy_count ) ;
140146 }
141147 // SAFETY: caller guarantees that `old_ptr` is valid
142148 unsafe {
143- c_dealloc ( old_ptr, old_align) ;
149+ c_dealloc ( old_ptr, old_align, old_size ) ;
144150 }
145151 }
146152}
@@ -249,11 +255,11 @@ pub unsafe fn c_zalloc(align: usize, size: usize) -> (*mut c_void, c_int) {
249255/// - `ptr` points to the start of a valid allocation returned by an allocation function listed
250256/// above, or is `NULL`.
251257/// - `ptr` has not yet been deallocated.
252- pub unsafe fn c_dealloc ( ptr : * mut c_void , _align : usize ) {
258+ pub unsafe fn c_dealloc ( ptr : * mut c_void , _size : usize , _align : usize ) {
253259 #[ cfg( windows) ]
254260 {
255261 #[ allow( clippy:: used_underscore_binding) ]
256- if _align > MIN_ALIGN && size >= align {
262+ if _align > MIN_ALIGN && _size >= _align {
257263 // SAFETY: requirements are passed onto the caller; as align > MIN_ALIGN,
258264 // _aligned_{malloc,realloc} was used so _aligned_free works.
259265 unsafe {
@@ -316,7 +322,7 @@ pub unsafe fn grow_aligned(
316322 // if successful, move data to new pointer
317323 // SAFETY: requirements are passed on to the caller
318324 unsafe {
319- try_move ( ptr, old_ptr, old_size, old_align) ;
325+ try_move ( ptr, old_ptr, old_size, old_align, old_size ) ;
320326 }
321327
322328 ( ptr, status)
@@ -346,6 +352,7 @@ pub unsafe fn grow_aligned(
346352pub unsafe fn shrink_aligned (
347353 old_ptr : * mut c_void ,
348354 old_align : usize ,
355+ old_size : usize ,
349356 align : usize ,
350357 size : usize // a zero here is useless, as it will just be overwritten anyway.
351358) -> ( * mut c_void , c_int ) {
@@ -357,7 +364,7 @@ pub unsafe fn shrink_aligned(
357364 // if successful, move data to new pointer
358365 // SAFETY: requirements are passed on to the caller
359366 unsafe {
360- try_move ( ptr, old_ptr, size, old_align) ;
367+ try_move ( ptr, old_ptr, size, old_align, old_size ) ;
361368 }
362369
363370 ( ptr, status)
@@ -426,7 +433,6 @@ extern "C" {
426433 will leak memory"]
427434 pub fn aligned_alloc ( align : usize , size : usize ) -> * mut c_void ;
428435
429- #[ cfg( not( windows) ) ]
430436 /// Frees memory previously returned by the primary C allocator.
431437 ///
432438 /// The closest Rust equivalent is [`dealloc`](::stdalloc::alloc::dealloc).
0 commit comments