kiba-engine
Macros | Functions
allocator.c File Reference

Base allocator implementation. More...

#include <kiba/allocators/allocator.h>
#include <kiba/allocators/free_list.h>
#include <kiba/allocators/linear.h>
#include <kiba/allocators/system.h>
#include <kiba/core/log.h>
#include <kiba/core/memory.h>
Include dependency graph for allocator.c:

Go to the source code of this file.

Macros

#define KB_ALLOCATOR_MANAGED_BLOCK_SIZE   (3 * sizeof(usize))
 Size of the managed block attached to each allocation. More...
 

Functions

usize allocator_managed_block_required_size (usize size, usize alignment)
 Calculate required size of an managed block based on size and alignment. More...
 
void * allocator_managed_block_create (void *allocated, usize size, usize alignment)
 Create managed block in pre-allocated memory. More...
 
void * allocator_managed_block_extract (const void *block, usize *size, usize *alignment, usize *offset)
 extract metadata from managed block. More...
 
b8 allocator_create (allocator *alloc, allocator_type type, usize size)
 Create an allocator of a specific type. More...
 
b8 allocator_create_custom (allocator *alloc, allocator_impl_create_fn create_fn, usize size)
 Create an allocator based on a custom create function. More...
 
void allocator_destroy (allocator *alloc)
 Destroy an allocator. More...
 
void * allocator_allocate (allocator *alloc, usize size)
 Allocate unaligned memory. More...
 
void * allocator_allocate_aligned (allocator *alloc, usize size, usize alignment)
 Allocate aligned memory. More...
 
void * allocator_reallocate (allocator *alloc, void *original, usize new_size)
 Reallocate memory to unaligned block. More...
 
void * allocator_reallocate_aligned (allocator *alloc, void *original, usize new_size, usize alignment)
 Rellocate aligned memory. More...
 
void allocator_free (allocator *alloc, void *mem)
 Give back memory to the allocator. More...
 
void allocator_free_all (allocator *alloc)
 Reset the allocator. More...
 
void allocator_implementation (allocator *alloc, allocator_impl_destroy_fn destroy, allocator_impl_allocate_fn allocate, allocator_impl_free_fn free, allocator_impl_free_all_fn free_all)
 Set allocator implementation. More...
 

Detailed Description

Base allocator implementation.

Allows for the creation and usage of specific allocator implementations.

Definition in file allocator.c.

Macro Definition Documentation

◆ KB_ALLOCATOR_MANAGED_BLOCK_SIZE

#define KB_ALLOCATOR_MANAGED_BLOCK_SIZE   (3 * sizeof(usize))

Size of the managed block attached to each allocation.

Structure of the managed block:
------ Start of allocated space
x bytes padding in the range [0,alignment)
sizeof(usize) offset, the offset of the user block from the allocated block
sizeof(usize) alignment, the alignment of the user block
sizeof(usize) size, the size of the entire allocated block (including padding)
size bytes of user block
y bytes padding in the range [0, alignment)
------ End of allocated space

Definition at line 177 of file allocator.c.

Function Documentation

◆ allocator_allocate()

void* allocator_allocate ( allocator alloc,
usize  size 
)

Allocate unaligned memory.

The returned memory block will not be aligned. (aligned to 1 byte boundaries)

See also
allocator_allocate_aligned
Parameters
allocallocator to use
sizenumber of bytes to allocate
Returns
pointer to allocated memory or KB_NULL if not successful

Definition at line 92 of file allocator.c.

Here is the call graph for this function:

◆ allocator_allocate_aligned()

void* allocator_allocate_aligned ( allocator alloc,
usize  size,
usize  alignment 
)

Allocate aligned memory.

Tries to find the necessary space in its managed memory block(s). If there is not enough space left and the allocator cannot get more then KB_NULL will be returned.

Alignment means the returned address will be a multiple of the specified alignment.

Parameters
allocallocator to use
sizenumber of bytes to allocate
alignmentthe number of bytes (powers of 2) to align to
Returns
pointer to allocated memory or KB_NULL if not successful

Definition at line 94 of file allocator.c.

Here is the call graph for this function:

◆ allocator_create()

b8 allocator_create ( allocator alloc,
allocator_type  type,
usize  size 
)

Create an allocator of a specific type.

Parameters
allocallocator structure to be filled
typekind of allocator implementation to be used
sizeminimum size in bytes the allocator should be able to provide
Returns
true if successful, false otherwise

Definition at line 66 of file allocator.c.

◆ allocator_create_custom()

b8 allocator_create_custom ( allocator alloc,
allocator_impl_create_fn  create_fn,
usize  size 
)

Create an allocator based on a custom create function.

See also
allocator_impl_create_fn
Parameters
allocallocator structure to be filled
create_fncustom allocator creation procedure
sizeminimum size in bytes the allocator should be able to provide
Returns
true if successful, false otherwise

Definition at line 79 of file allocator.c.

◆ allocator_destroy()

void allocator_destroy ( allocator alloc)

Destroy an allocator.

Release all structures created by the allocator.

Parameters
allocallocator to destroy

Definition at line 84 of file allocator.c.

Here is the call graph for this function:

◆ allocator_free()

void allocator_free ( allocator alloc,
void *  mem 
)

Give back memory to the allocator.

Does not free the memory for the OS. Instead the allocator may reuse the returned block of memory for future calls to allocator_allocate.

Parameters
allocallocator which initially provided the memory
mempointer to memory to give back

Definition at line 134 of file allocator.c.

Here is the call graph for this function:

◆ allocator_free_all()

void allocator_free_all ( allocator alloc)

Reset the allocator.

Clears the allocators state besides its managed memory blocks. Does not free the memory for the OS. Instead the allocator may reuse the memory for future calls to allocator_allocate.

Parameters
allocallocator to reset

Definition at line 143 of file allocator.c.

◆ allocator_implementation()

void allocator_implementation ( allocator alloc,
allocator_impl_destroy_fn  destroy,
allocator_impl_allocate_fn  allocate,
allocator_impl_free_fn  free,
allocator_impl_free_all_fn  free_all 
)

Set allocator implementation.

Parameters
allocallocator to set impl for
destroyimplementation's destroy function
allocateimplementation's allocate function
freeimplementation's free function
free_allimplementation's free_all function

Definition at line 149 of file allocator.c.

◆ allocator_managed_block_create()

void * allocator_managed_block_create ( void *  allocated,
usize  size,
usize  alignment 
)

Create managed block in pre-allocated memory.

Precondition
The pre-allocated memory must have at least allocator_managed_block_required_size bytes.
See also
allocator_managed_block_required_size

Structure of the managed block:

See also
KB_ALLOCATOR_MANAGED_BLOCK_SIZE
Parameters
allocatedpre-allocated memory
sizedesired size of the usable space
alignmentdesired alignment of the usable space
Returns
aligned pointer to the beginning of the usable memory space

Definition at line 185 of file allocator.c.

Here is the call graph for this function:

◆ allocator_managed_block_extract()

void * allocator_managed_block_extract ( const void *  block,
usize *  size,
usize *  alignment,
usize *  offset 
)

extract metadata from managed block.

Precondition
The given pointer must originate from allocator_managed_block_create.

Given a pointer returned by allocator_managed_block_create, this will extract the size of the whole block, the alignment of the usable space and the offset of the usable space to the whole block of memory. Allows user to recover all info about the originally pre-allocated block.

Parameters
[in]blockpointer to the usable part of the block
[out]sizewill be filled with the size of the whole block (not just the usable part)
[out]alignmentwill be filled with the alignment of the usable block
[out]offsetwill be filled with the offset of the usable block start to the start of the managed block
Returns
pointer to the start of the managed block

Definition at line 195 of file allocator.c.

◆ allocator_managed_block_required_size()

usize allocator_managed_block_required_size ( usize  size,
usize  alignment 
)

Calculate required size of an managed block based on size and alignment.

Structure of the managed block:

See also
KB_ALLOCATOR_MANAGED_BLOCK_SIZE

Allocating the returned number of bytes guarantees a managed block can be created containing size bytes of usable memory aligned to alignment.

Parameters
sizethe number of bytes to be allocated
alignmentthe desired alignment of the allocation
Returns
number of bytes required to have size bytes properly aligned with a preceding metadata block

Definition at line 179 of file allocator.c.

◆ allocator_reallocate()

void* allocator_reallocate ( allocator alloc,
void *  original,
usize  new_size 
)

Reallocate memory to unaligned block.

The returned memory block will retain the same alignment as the oiginal block.

See also
allocator_reallocate_aligned
Parameters
allocallocator to use
originalpointer to the original memory block
new_sizenumber of bytes the reallocated space should contain
Returns
pointer to the reallocated memory or KB_NULL if not successful

Definition at line 107 of file allocator.c.

Here is the call graph for this function:

◆ allocator_reallocate_aligned()

void* allocator_reallocate_aligned ( allocator alloc,
void *  original,
usize  new_size,
usize  alignment 
)

Rellocate aligned memory.

New memory block contains min(size, <previous block's size>) bytes of original block.

Tries to find the necessary space in its managed memory block(s). If there is not enough space left and the allocator cannot get more then KB_NULL will be returned. The original memory block will remain valid in this case.

Note
Setting the alignment to 0 will cause the new block to retain the alignment of the original block.
Parameters
allocallocator to use
originalpointer to the original memory block
new_sizenumber of bytes the reallocated space should contain
alignmentthe number of bytes (powers of 2) the new block should be aligned to
Returns
pointer to the reallocated memory or KB_NULL if not successful

Definition at line 111 of file allocator.c.

Here is the call graph for this function: