Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
Memory Management

Data Structures

struct  bj_memory_callbacks

Typedefs

typedef void *(* bj_malloc_fn) (void *user_data, size_t size)
typedef void *(* bj_realloc_fn) (void *user_data, void *original, size_t size)
typedef void(* bj_free_fn) (void *user_data, void *memory)

Functions

void * bj_malloc (size_t size)
void * bj_calloc (size_t size)
void * bj_realloc (void *memory, size_t size)
void bj_free (void *memory)
void bj_set_memory_defaults (const struct bj_memory_callbacks *allocator)
void bj_unset_memory_defaults (void)
void * bj_memcpy (void *dest, const void *src, size_t mem_size)
void * bj_memmove (void *dest, const void *src, size_t mem_size)
int bj_memcmp (const void *block_a, const void *block_b, size_t size)
void bj_memset (void *dest, uint8_t value, size_t mem_size)
void bj_memzero (void *dest, size_t mem_size)

Detailed Description


Data Structure Documentation

◆ bj_memory_callbacks

struct bj_memory_callbacks

Custom allocation callbacks.

This structure holds function pointers for allocation, reallocation, and deallocation callbacks along with user data.

These callbacks can be assigned per-object or set globally with bj_set_memory_defaults.

Data Fields
bj_malloc_fn fn_allocation Allocation function pointer.
bj_free_fn fn_free Deallocation function pointer.
bj_realloc_fn fn_reallocation Reallocation function pointer.
void * user_data General purpose context data.

Typedef Documentation

◆ bj_free_fn

typedef void(* bj_free_fn) (void *user_data, void *memory)

Memory free callback.

Used in bj_memory_callbacks to set the function used for custom deallocations.

Parameters
user_dataGeneral purpose context data.
memoryObject memory to dispose.

◆ bj_malloc_fn

typedef void *(* bj_malloc_fn) (void *user_data, size_t size)

Memory allocation callback.

Used in bj_memory_callbacks to set the function used for custom allocations.

Parameters
user_dataGeneral purpose context data.
sizeAllocation size in bytes requested by the caller.
Returns
Pointer to allocated memory block.

◆ bj_realloc_fn

typedef void *(* bj_realloc_fn) (void *user_data, void *original, size_t size)

Memory reallocation callback.

Used in bj_memory_callbacks to set the function used for custom reallocations.

Parameters
user_dataGeneral purpose context data.
originalInitial object to reallocate.
sizeAllocation size in bytes requested by the caller.
Returns
Pointer to reallocated memory block.

Function Documentation

◆ bj_calloc()

void * bj_calloc ( size_t size)

Allocate size bytes of zero-initialized memory.

The returned memory is set to zero bytes.

Parameters
[in]sizeNumber of bytes to allocate.
Returns
Pointer to newly allocated zeroed memory block.

◆ bj_free()

void bj_free ( void * memory)

Free a previously allocated memory block.

Parameters
[in]memoryPointer to memory to free.
Note
memory must have been allocated by bj_malloc or bj_realloc.

◆ bj_malloc()

void * bj_malloc ( size_t size)

Allocate size bytes of memory.

Parameters
[in]sizeNumber of bytes to allocate.
Returns
Pointer to newly allocated memory block.

◆ bj_memcmp()

int bj_memcmp ( const void * block_a,
const void * block_b,
size_t size )

Compare two memory blocks.

Parameters
[in]block_aPointer to first memory block.
[in]block_bPointer to second memory block.
[in]sizeNumber of bytes to compare.
Returns
Zero if equal, negative if a < b, positive if a > b.

◆ bj_memcpy()

void * bj_memcpy ( void * dest,
const void * src,
size_t mem_size )

Copy mem_size bytes from src to dest.

Parameters
[in]destDestination pointer.
[in]srcSource pointer.
[in]mem_sizeNumber of bytes to copy.
Returns
Pointer to dest.

◆ bj_memmove()

void * bj_memmove ( void * dest,
const void * src,
size_t mem_size )

Move mem_size bytes from src to dest.

Similar to bj_memcpy but handles overlapping memory regions safely.

Parameters
[in]destDestination pointer.
[in]srcSource pointer.
[in]mem_sizeNumber of bytes to move.
Returns
Pointer to dest.

◆ bj_memset()

void bj_memset ( void * dest,
uint8_t value,
size_t mem_size )

Fill mem_size bytes at dest with value.

Parameters
[in]destDestination pointer.
[in]valueByte value to fill.
[in]mem_sizeNumber of bytes to fill.

◆ bj_memzero()

void bj_memzero ( void * dest,
size_t mem_size )

Zero out mem_size bytes at dest.

Effectively calls bj_memset with zero.

Parameters
[in]destDestination pointer.
[in]mem_sizeNumber of bytes to zero.

◆ bj_realloc()

void * bj_realloc ( void * memory,
size_t size )

Reallocate a memory block to a new size.

Parameters
[in]memoryPointer to previously allocated memory.
[in]sizeNumber of bytes to allocate.
Returns
Pointer to newly reallocated memory block.
Note
memory must have been allocated by bj_malloc or bj_realloc.

◆ bj_set_memory_defaults()

void bj_set_memory_defaults ( const struct bj_memory_callbacks * allocator)

Set the global default memory allocators.

If allocator is NULL, resets to system defaults (malloc, realloc, free).

Parameters
[in]allocatorPointer to custom allocator callbacks.

◆ bj_unset_memory_defaults()

void bj_unset_memory_defaults ( void )

Reset the global default allocators to system defaults.

Sets allocators back to standard system functions (malloc, realloc, free).