Skip to main content

memory_reallocate

Function memory_reallocate 

Source
pub unsafe fn memory_reallocate(
    pointer: *mut c_void,
    size: usize,
) -> *mut c_void
Expand description

Reallocates a memory block to a new size.

This function changes the size of a previously allocated memory block. If the new size is larger, the additional memory is uninitialized. If the new size is smaller, the excess memory is freed. The original data is preserved up to the minimum of the old and new sizes. The platform resizes the block in place when it can, and a shrink that keeps the alignment and DMA constraints can stay in place. Growth follows the platform’s placement policy and always preserves the capability flags requested at allocation time. If no suitable region can serve the new size and alignment, reallocation fails and the old block is unchanged.

§Safety

  • If Pointer is not null, it must have been returned by a previous call to memory_allocate or memory_reallocate
  • Use the returned pointer after a successful resize
  • A non-null original pointer remains valid only if a nonzero resize fails

§Parameters

  • Pointer - Pointer to the memory block to reallocate, or null for new allocation
  • Size - New size in bytes (0 is equivalent to deallocation). On 32-bit targets a block, including its header and padding, is limited to 32 MiB.

§Returns

  • Pointer to the reallocated memory block
  • Null if allocation fails or if size is 0
  • If Pointer is null and Size is nonzero, allocates with default alignment

§Examples

// Allocate initial memory
void* ptr = memory_reallocate(NULL, 1024);

// Expand the memory
ptr = memory_reallocate(ptr, 2048);

// Shrink the memory
ptr = memory_reallocate(ptr, 512);

// Free the memory
memory_reallocate(ptr, 0);