kiba-engine
|
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>
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... | |
Base allocator implementation.
Allows for the creation and usage of specific allocator implementations.
Definition in file allocator.c.
#define KB_ALLOCATOR_MANAGED_BLOCK_SIZE (3 * sizeof(usize)) |
Size of the managed block attached to each allocation.
Definition at line 177 of file allocator.c.
void* allocator_allocate | ( | allocator * | alloc, |
usize | size | ||
) |
Allocate unaligned memory.
The returned memory block will not be aligned. (aligned to 1 byte boundaries)
alloc | allocator to use |
size | number of bytes to allocate |
Definition at line 92 of file allocator.c.
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.
alloc | allocator to use |
size | number of bytes to allocate |
alignment | the number of bytes (powers of 2) to align to |
Definition at line 94 of file allocator.c.
b8 allocator_create | ( | allocator * | alloc, |
allocator_type | type, | ||
usize | size | ||
) |
Create an allocator of a specific type.
alloc | allocator structure to be filled |
type | kind of allocator implementation to be used |
size | minimum size in bytes the allocator should be able to provide |
Definition at line 66 of file allocator.c.
b8 allocator_create_custom | ( | allocator * | alloc, |
allocator_impl_create_fn | create_fn, | ||
usize | size | ||
) |
Create an allocator based on a custom create function.
alloc | allocator structure to be filled |
create_fn | custom allocator creation procedure |
size | minimum size in bytes the allocator should be able to provide |
Definition at line 79 of file allocator.c.
void allocator_destroy | ( | allocator * | alloc | ) |
Destroy an allocator.
Release all structures created by the allocator.
alloc | allocator to destroy |
Definition at line 84 of file allocator.c.
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.
alloc | allocator which initially provided the memory |
mem | pointer to memory to give back |
Definition at line 134 of file allocator.c.
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.
alloc | allocator to reset |
Definition at line 143 of file allocator.c.
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.
alloc | allocator to set impl for |
destroy | implementation's destroy function |
allocate | implementation's allocate function |
free | implementation's free function |
free_all | implementation's free_all function |
Definition at line 149 of file allocator.c.
void * allocator_managed_block_create | ( | void * | allocated, |
usize | size, | ||
usize | alignment | ||
) |
Create managed block in pre-allocated memory.
Structure of the managed block:
allocated | pre-allocated memory |
size | desired size of the usable space |
alignment | desired alignment of the usable space |
Definition at line 185 of file allocator.c.
void * allocator_managed_block_extract | ( | const void * | block, |
usize * | size, | ||
usize * | alignment, | ||
usize * | offset | ||
) |
extract metadata from managed block.
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.
[in] | block | pointer to the usable part of the block |
[out] | size | will be filled with the size of the whole block (not just the usable part) |
[out] | alignment | will be filled with the alignment of the usable block |
[out] | offset | will be filled with the offset of the usable block start to the start of the managed block |
Definition at line 195 of file allocator.c.
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:
Allocating the returned number of bytes guarantees a managed block can be created containing size bytes of usable memory aligned to alignment.
size | the number of bytes to be allocated |
alignment | the desired alignment of the allocation |
Definition at line 179 of file allocator.c.
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.
alloc | allocator to use |
original | pointer to the original memory block |
new_size | number of bytes the reallocated space should contain |
Definition at line 107 of file allocator.c.
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.
alloc | allocator to use |
original | pointer to the original memory block |
new_size | number of bytes the reallocated space should contain |
alignment | the number of bytes (powers of 2) the new block should be aligned to |
Definition at line 111 of file allocator.c.