kiba-engine
|
Central header providing allocator functionality. More...
#include <kiba/defines.h>
Go to the source code of this file.
Data Structures | |
struct | allocator_impl |
Structure holding a specific allocator type's implementation. More... | |
struct | allocator |
Central allocator structure. More... | |
Typedefs | |
typedef enum allocator_type | allocator_type |
Available allocator types. More... | |
typedef b8(* | allocator_impl_create_fn) (struct allocator *alloc, usize size) |
Create a specific allocator. More... | |
typedef void(* | allocator_impl_destroy_fn) (struct allocator *alloc) |
Destroy the allocator. More... | |
typedef void *(* | allocator_impl_allocate_fn) (struct allocator *alloc, usize size) |
Allocate memory. More... | |
typedef void(* | allocator_impl_free_fn) (struct allocator *alloc, void *mem, usize size) |
Give memory back to the allocator. More... | |
typedef void(* | allocator_impl_free_all_fn) (struct allocator *alloc) |
Give all allocated memory back to the allocator. More... | |
typedef struct allocator_impl | allocator_impl |
Structure holding a specific allocator type's implementation. | |
typedef struct allocator | allocator |
Central allocator structure. More... | |
Enumerations | |
enum | allocator_type { ALLOCATOR_LINEAR , ALLOCATOR_FREE_LIST , ALLOCATOR_SYSTEM } |
Available allocator types. More... | |
Functions | |
KB_API b8 | allocator_create (allocator *alloc, allocator_type type, usize size) |
Create an allocator of a specific type. More... | |
KB_API b8 | allocator_create_custom (allocator *alloc, allocator_impl_create_fn create_fn, usize size) |
Create an allocator based on a custom create function. More... | |
KB_API void | allocator_destroy (allocator *alloc) |
Destroy an allocator. More... | |
KB_API void * | allocator_allocate (allocator *alloc, usize size) |
Allocate unaligned memory. More... | |
KB_API void * | allocator_allocate_aligned (allocator *alloc, usize size, usize alignment) |
Allocate aligned memory. More... | |
KB_API void * | allocator_reallocate (allocator *alloc, void *original, usize new_size) |
Reallocate memory to unaligned block. More... | |
KB_API void * | allocator_reallocate_aligned (allocator *alloc, void *original, usize new_size, usize alignment) |
Rellocate aligned memory. More... | |
KB_API void | allocator_free (allocator *alloc, void *mem) |
Give back memory to the allocator. More... | |
KB_API void | allocator_free_all (allocator *alloc) |
Reset the allocator. More... | |
KB_API 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... | |
Central header providing allocator functionality.
Provides general interface to create and interact with allocators.
Definition in file allocator.h.
Central allocator structure.
This type is used to interact with the allocator subsystem.
typedef void*(* allocator_impl_allocate_fn) (struct allocator *alloc, usize size) |
Allocate memory.
alloc | the allocator to allocate from |
size | size of the memory block to allocate |
Definition at line 54 of file allocator.h.
typedef b8(* allocator_impl_create_fn) (struct allocator *alloc, usize size) |
Create a specific allocator.
Must initialize the state and provide implementations for all functions.
alloc | allocator to populate with the allocator implementation |
size | minimum size of the initial memory block to manage |
Definition at line 38 of file allocator.h.
typedef void(* allocator_impl_destroy_fn) (struct allocator *alloc) |
Destroy the allocator.
Cleans up the state. Also returns the managed memory to the memory system.
alloc | the allocator to destroy |
Definition at line 46 of file allocator.h.
typedef void(* allocator_impl_free_all_fn) (struct allocator *alloc) |
Give all allocated memory back to the allocator.
alloc | the allocator to free all allocations from |
Definition at line 68 of file allocator.h.
typedef void(* allocator_impl_free_fn) (struct allocator *alloc, void *mem, usize size) |
Give memory back to the allocator.
alloc | the allocator the memory was initially requested from |
mem | pointer to the block of memory to give back |
size | number of bytes that should be given back to the allocator |
Definition at line 62 of file allocator.h.
typedef enum allocator_type allocator_type |
Available allocator types.
This type is used to determine the specific implementation of an allocator.
enum allocator_type |
Available allocator types.
This type is used to determine the specific implementation of an allocator.
Definition at line 18 of file allocator.h.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.
KB_API 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.