Skip to main content

GhosttyAllocatorVtable

Struct GhosttyAllocatorVtable 

Source
#[repr(C)]
pub struct GhosttyAllocatorVtable { pub alloc: Option<unsafe extern "C" fn(ctx: *mut c_void, len: usize, alignment: u8, ret_addr: usize) -> *mut c_void>, pub resize: Option<unsafe extern "C" fn(ctx: *mut c_void, memory: *mut c_void, memory_len: usize, alignment: u8, new_len: usize, ret_addr: usize) -> bool>, pub remap: Option<unsafe extern "C" fn(ctx: *mut c_void, memory: *mut c_void, memory_len: usize, alignment: u8, new_len: usize, ret_addr: usize) -> *mut c_void>, pub free: Option<unsafe extern "C" fn(ctx: *mut c_void, memory: *mut c_void, memory_len: usize, alignment: u8, ret_addr: usize)>, }
Expand description

Function table for custom memory allocator operations.

This vtable defines the interface for a custom memory allocator. All function pointers must be valid and non-NULL.

@ingroup allocator

If you’re not going to use a custom allocator, you can ignore all of this. All functions that take an allocator pointer allow NULL to use a default allocator.

The interface is based on the Zig allocator interface. I’ll say up front that it is easy to look at this interface and think “wow, this is really overcomplicated”. The reason for this complexity is well thought out by the Zig folks, and it enables a diverse set of allocation strategies as shown by the Zig ecosystem. As a consolation, please note that many of the arguments are only needed for advanced use cases and can be safely ignored in simple implementations. For example, if you look at the Zig implementation of the libc allocator in lib/std/heap.zig (search for CAllocator), you’ll see it is very simple.

We chose to align with the Zig allocator interface because:

  1. It is a proven interface that serves a wide variety of use cases in the real world via the Zig ecosystem. It’s shown to work.

  2. Our core implementation itself is Zig, and this lets us very cheaply and easily convert between C and Zig allocators.

NOTE(mitchellh): In the future, we can have default implementations of resize/remap and allow those to be null.

Fields§

§alloc: Option<unsafe extern "C" fn(ctx: *mut c_void, len: usize, alignment: u8, ret_addr: usize) -> *mut c_void>

Return a pointer to len bytes with specified alignment, or return NULL indicating the allocation failed.

@param ctx The allocator context @param len Number of bytes to allocate @param alignment Required alignment for the allocation. Guaranteed to be a power of two between 1 and 16 inclusive. @param ret_addr First return address of the allocation call stack (0 if not provided) @return Pointer to allocated memory, or NULL if allocation failed

§resize: Option<unsafe extern "C" fn(ctx: *mut c_void, memory: *mut c_void, memory_len: usize, alignment: u8, new_len: usize, ret_addr: usize) -> bool>

Attempt to expand or shrink memory in place.

memory_len must equal the length requested from the most recent successful call to alloc, resize, or remap. alignment must equal the same value that was passed as the alignment parameter to the original alloc call.

new_len must be greater than zero.

@param ctx The allocator context @param memory Pointer to the memory block to resize @param memory_len Current size of the memory block @param alignment Alignment (must match original allocation) @param new_len New requested size @param ret_addr First return address of the allocation call stack (0 if not provided) @return true if resize was successful in-place, false if relocation would be required

§remap: Option<unsafe extern "C" fn(ctx: *mut c_void, memory: *mut c_void, memory_len: usize, alignment: u8, new_len: usize, ret_addr: usize) -> *mut c_void>

Attempt to expand or shrink memory, allowing relocation.

memory_len must equal the length requested from the most recent successful call to alloc, resize, or remap. alignment must equal the same value that was passed as the alignment parameter to the original alloc call.

A non-NULL return value indicates the resize was successful. The allocation may have same address, or may have been relocated. In either case, the allocation now has size of new_len. A NULL return value indicates that the resize would be equivalent to allocating new memory, copying the bytes from the old memory, and then freeing the old memory. In such case, it is more efficient for the caller to perform the copy.

new_len must be greater than zero.

@param ctx The allocator context @param memory Pointer to the memory block to remap @param memory_len Current size of the memory block @param alignment Alignment (must match original allocation) @param new_len New requested size @param ret_addr First return address of the allocation call stack (0 if not provided) @return Pointer to resized memory (may be relocated), or NULL if manual copy is needed

§free: Option<unsafe extern "C" fn(ctx: *mut c_void, memory: *mut c_void, memory_len: usize, alignment: u8, ret_addr: usize)>

Free and invalidate a region of memory.

memory_len must equal the length requested from the most recent successful call to alloc, resize, or remap. alignment must equal the same value that was passed as the alignment parameter to the original alloc call.

@param ctx The allocator context @param memory Pointer to the memory block to free @param memory_len Size of the memory block @param alignment Alignment (must match original allocation) @param ret_addr First return address of the allocation call stack (0 if not provided)

Trait Implementations§

Source§

impl Clone for GhosttyAllocatorVtable

Source§

fn clone(&self) -> GhosttyAllocatorVtable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GhosttyAllocatorVtable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GhosttyAllocatorVtable

Source§

fn default() -> GhosttyAllocatorVtable

Returns the “default value” for a type. Read more
Source§

impl Copy for GhosttyAllocatorVtable

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.