ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
ds_allocator_t Struct Reference

An implementation of Allocator can allocate, reallocate (grow, shrink), and deallocate arbitrary blocks of data. More...

#include <alloc.h>

Data Fields

void *(* allocate )(ds_usize_t size)
 Attempts to allocate a block of memory. More...
 
void *(* reallocate )(void *ptr, ds_usize_t new_size)
 Attempts to reallocate (extend, shrink) the memory block. More...
 
void(* deallocate )(void *ptr)
 Deallocates the memory referenced by ptr. More...
 

Detailed Description

An implementation of Allocator can allocate, reallocate (grow, shrink), and deallocate arbitrary blocks of data.

Zero-sized allocations are allowed in Allocator. If an underlying allocator does not support this (like jemalloc) or return a NULL pointer (such as malloc), this must be caught by the implementation.

Definition at line 27 of file alloc.h.

Field Documentation

◆ allocate

void *(* ds_allocator_t::allocate) (ds_usize_t size)

Attempts to allocate a block of memory.

The returned block may have a larger size than specified by size, and may or may not have its contents initialized.

Parameters
sizeThe size of the block of memory to allocate.
Returns
A non-NULL pointer to the block of memory meeting the size and alignment guarantees on success.
NULL on failure.

Definition at line 38 of file alloc.h.

◆ deallocate

void(* ds_allocator_t::deallocate) (void *ptr)

Deallocates the memory referenced by ptr.

Parameters
[out]ptrA reference to the memory to deallocate.

Safety

ptr must denote a block of memory currently allocated via this allocator.

Definition at line 75 of file alloc.h.

◆ reallocate

void *(* ds_allocator_t::reallocate) (void *ptr, ds_usize_t new_size)

Attempts to reallocate (extend, shrink) the memory block.

The returned block may have a larger size than specified by new_size, and may or may not have its contents initialized.

If this returns a non-NULL pointer, then ownership of the memory block referenced by ptr has been transferred to this allocator. Any access to the old ptr is Undefined Behavior, even if the allocation was grown in-place. The newly returned pointer is the only valid pointer for accessing this memory now.

If this method returns NULL, then ownership of the memory block has not been transferred to this allocator, and the contents of the memory block are unaltered.

Parameters
ptrA reference to the memory block to reallocate.
new_sizeThe new size of the block of memory to reallocate.
Returns
A non-NULL pointer to the block of memory meeting the size and alignment guarantees on success.
NULL on failure.

Safety

ptr must denote a block of memory currently allocated via this allocator.

Definition at line 66 of file alloc.h.


The documentation for this struct was generated from the following file: