Function libyml::memory::yaml_realloc

source ·
pub unsafe fn yaml_realloc(ptr: *mut c_void, size: u64) -> *mut c_void
Expand description

Reallocate memory using the system’s realloc function.

This function changes the size of the memory block pointed to by ptr to size bytes.

§Arguments

  • ptr - A pointer to the memory block to reallocate. If null, this function behaves like yaml_malloc.
  • size - The new size of the memory block in bytes.

§Returns

Returns a pointer to the reallocated memory, which may be different from ptr, or a null pointer if the reallocation failed.

§Safety

This function is unsafe because:

  • It directly calls the system’s realloc function, which is not memory-safe.
  • The caller must ensure that ptr is either null or was previously allocated by yaml_malloc or yaml_realloc.
  • The caller must ensure that the reallocated memory is properly freed using yaml_free.
  • The contents of the reallocated memory beyond the original size are undefined.

§Note

If the reallocation fails, the original memory block is left untouched and a null pointer is returned.

§Examples

use libyml::memory::{yaml_malloc, yaml_realloc, yaml_free};
use libyml::yaml::size_t;

unsafe {
    let mut size: size_t = 1024;
    let mut ptr = yaml_malloc(size);
    if !ptr.is_null() {
        // Use the allocated memory
        // ...
        size = 2048;
        ptr = yaml_realloc(ptr, size);
        if !ptr.is_null() {
            // Use the reallocated memory
            // ...
            yaml_free(ptr);
        }
    }
}