Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
Memory Management
Collaboration diagram for Memory Management:

Detailed Description

Data Structures

struct  bj_memory_callbacks
 Custom allocation callbacks. More...
 

Typedefs

typedef void *(* bj_malloc_fn) (void *p_user_data, size_t size)
 Memory allocation callback.
 
typedef void *(* bj_realloc_fn) (void *p_user_data, void *p_original, size_t size)
 Memory allocation callback.
 
typedef void(* bj_free_fn) (void *p_user_data, void *p_memory)
 Memory allocation callback.
 
typedef struct bj_memory_callbacks bj_memory_callbacks
 Custom allocation callbacks.
 

Functions

void * bj_malloc (size_t size)
 Allocate size bytes of memory and returns a pointer to it.
 
void * bj_calloc (size_t size)
 Allocate size bytes of memory and returns a pointer to it.
 
void * bj_realloc (void *p_memory, size_t size)
 Reallocate the given area of memory.
 
void bj_free (void *p_memory)
 Deallocate the given area of memory.
 
void bj_memory_set_defaults (const bj_memory_callbacks *p_allocator)
 Set the default allocators.
 
void bj_memory_unset_defaults (void)
 Reset the default allocators.
 
void * bj_memcpy (void *p_dest, const void *p_src, size_t mem_size)
 Copies mem_size bytes from the object pointed to by p_src to to object pointed to by p_dest.
 
void * bj_memmove (void *p_dest, const void *p_src, size_t mem_size)
 
int bj_memcmp (const void *p_block_a, const void *p_block_b, size_t size)
 
void bj_memset (void *p_dest, uint8_t value, size_t mem_size)
 Copies the value value into each ot the first num bytes of the object pointed to by p_dest.
 
void bj_memzero (void *p_dest, size_t mem_size)
 Sets the mem_size bytes starting from p_dest to 0.
 

Data Structure Documentation

◆ bj_memory_callbacks

struct bj_memory_callbacks

This structure is used to set the function used by the API upon managing memory. Each object created by the API can be set a specific set of callbacks. Also, the custom allocators can be set globally with bj_memory_set_defaults.

Data Fields
bj_malloc_fn fn_allocation The allocation function.
bj_free_fn fn_free The deallocation function.
bj_realloc_fn fn_reallocation The reallocation function.
void * p_user_data General purpose context data.

Typedef Documentation

◆ bj_free_fn

typedef void(* bj_free_fn) (void *p_user_data, void *p_memory)

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

◆ bj_malloc_fn

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

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

◆ bj_memory_callbacks

typedef struct bj_memory_callbacks bj_memory_callbacks

This structure is used to set the function used by the API upon managing memory. Each object created by the API can be set a specific set of callbacks. Also, the custom allocators can be set globally with bj_memory_set_defaults.

◆ bj_realloc_fn

typedef void *(* bj_realloc_fn) (void *p_user_data, void *p_original, size_t size)

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

Function Documentation

◆ bj_calloc()

void * bj_calloc ( size_t size)

The returned memory is automatically filled with 0's using bj_memzero.

Parameters
[in]sizeThe number of bytes to allocate.
Returns
The memory address of the newly allocated block.

◆ bj_free()

void bj_free ( void * p_memory)
Parameters
[in]p_memoryPointer to the memory area to be reallocated.

p_memory must be previously allocated by bj_malloc or bj_realloc.

The latter can have been overriden by bj_memory_set_defaults.

◆ bj_malloc()

void * bj_malloc ( size_t size)
Parameters
[in]sizeThe number of bytes to allocate.
Returns
The memory address of the newly allocated block.

◆ bj_memcpy()

void * bj_memcpy ( void * p_dest,
const void * p_src,
size_t mem_size )
Parameters
[in]p_destPointer to the object to copy to.
[in]p_srcPointer to the object to copy from.
[in]mem_sizeNumber of bytes to copy.
Returns
p_dest pointer.

The function effectively calls the standard memcpy function.

◆ bj_memory_set_defaults()

void bj_memory_set_defaults ( const bj_memory_callbacks * p_allocator)

When a function requires memory changes, the caller can send callback memory functions. If the given allocator is 0, Banjo uses global defaults (malloc/realloc/free).

This function can override the default allocator globally. After calling this function, default allocators (0) will be using the callbacks set in parameter.

If 0 is passed to this function, Banjo sets back the systems allocators.

Parameters
[in]p_allocatorThe new default allocators.
See also
bj_memory_unset_defaults

◆ bj_memory_unset_defaults()

void bj_memory_unset_defaults ( void )

Sets back the global allocators to standard defaults (malloc/realloc/free).

See also
bj_memory_set_defaults

◆ bj_memset()

void bj_memset ( void * p_dest,
uint8_t value,
size_t mem_size )
Parameters
[in]p_destPointer to the object to fill.
[in]valuefill byte.
[in]mem_sizenumber of bytes to fill.

◆ bj_memzero()

void bj_memzero ( void * p_dest,
size_t mem_size )

Effectively calls bj_memset(p_dest, 0, mem_size)

Parameters
[in]p_destPointer to the object to fill.
[in]mem_sizenumber of bytes to fill.

◆ bj_realloc()

void * bj_realloc ( void * p_memory,
size_t size )
Parameters
[in]p_memoryPointer to the memory area to be reallocated.
[in]sizeThe number of bytes to allocate.

p_memory must be previously allocated by bj_malloc or bj_realloc.

Returns
The memory address of the newly allocated block.