Macro libyml::yaml_realloc

source ·
macro_rules! yaml_realloc {
    ($ptr:expr, $t:ty, $size:expr) => { ... };
}
Expand description

Reallocates memory using the system’s realloc function.

This macro wraps the yaml_realloc function, providing a more Rust-like interface.

§Arguments

  • ptr - A pointer to the memory block to reallocate.
  • size - The new size of the memory block in bytes.

§Returns

Returns a *mut T pointer to the reallocated memory, or None if the reallocation failed.

§Safety

While this macro provides a safer interface than direct use of yaml_realloc, it is still unsafe because:

  • It may move the memory to a new location.
  • The caller must ensure that ptr was previously allocated by yaml_malloc! or yaml_realloc!.
  • The caller is responsible for properly freeing the reallocated memory using yaml_free!.
  • The contents of the reallocated memory beyond the original size are undefined.

§Examples

use libyml::{yaml_malloc, yaml_realloc, yaml_free};

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