68 case ALLOCATOR_LINEAR:
70 case ALLOCATOR_FREE_LIST:
72 case ALLOCATOR_SYSTEM:
75 KB_ERROR(
"no valid core allocator type selected: {i32}", type);
81 return create_fn(alloc, size);
86 "allocator must have initialized state");
96 "allocator must have initialized state");
97 KB_ASSERT(size != 0,
"size to allocate must not be 0");
98 KB_ASSERT(alignment != 0,
"alignment for allocation must not be 0");
114 "allocator must have initialized state");
115 KB_ASSERT(original !=
KB_NULL,
"original pointer for reallocate must not be nullptr");
116 KB_ASSERT(new_size != 0,
"new size for reallocation must not be 0");
117 usize original_size, original_alignment, original_offset;
118 void *actual_original =
121 alignment = original_alignment;
125 void *new_block = alloc->
impl.
allocate(alloc, actual_new_size);
128 memory_copy(new_block, original,
KB_MIN(new_size, original_size - original_offset));
129 alloc->
impl.
free(alloc, actual_original, original_size);
136 "allocator must have initialized state");
138 usize size, alignment, offset;
140 alloc->
impl.
free(alloc, actual_mem, size);
145 "allocator must have initialized state");
177 #define KB_ALLOCATOR_MANAGED_BLOCK_SIZE (3 * sizeof(usize))
181 KB_ASSERT_DEBUG(alignment != 0,
"alignment of managed block must not be 0");
188 usize *header = new_addr;
190 header[-2] = alignment;
191 header[-3] = (uptr) new_addr - (uptr) allocated;
197 const usize *data = block;
199 *alignment = data[-2];
201 return (u8 *) block - *offset;
204 #undef KB_ALLOCATOR_MANAGED_BLOCK_SIZE
void * allocator_allocate(allocator *alloc, usize size)
Allocate unaligned memory.
b8 allocator_create_custom(allocator *alloc, allocator_impl_create_fn create_fn, usize size)
Create an allocator based on a custom create function.
void * allocator_managed_block_extract(const void *block, usize *size, usize *alignment, usize *offset)
extract metadata from managed block.
usize allocator_managed_block_required_size(usize size, usize alignment)
Calculate required size of an managed block based on size and alignment.
void * allocator_managed_block_create(void *allocated, usize size, usize alignment)
Create managed block in pre-allocated memory.
void * allocator_allocate_aligned(allocator *alloc, usize size, usize alignment)
Allocate aligned memory.
void * allocator_reallocate(allocator *alloc, void *original, usize new_size)
Reallocate memory to unaligned block.
void allocator_free(allocator *alloc, void *mem)
Give back memory to the allocator.
#define KB_ALLOCATOR_MANAGED_BLOCK_SIZE
Size of the managed block attached to each allocation.
b8 allocator_create(allocator *alloc, allocator_type type, usize size)
Create an allocator of a specific type.
void allocator_destroy(allocator *alloc)
Destroy an allocator.
void allocator_free_all(allocator *alloc)
Reset the allocator.
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.
void * allocator_reallocate_aligned(allocator *alloc, void *original, usize new_size, usize alignment)
Rellocate aligned memory.
Central header providing allocator functionality.
allocator_type
Available allocator types.
void(* allocator_impl_free_all_fn)(struct allocator *alloc)
Give all allocated memory back to the allocator.
void(* allocator_impl_free_fn)(struct allocator *alloc, void *mem, usize size)
Give memory back to the allocator.
void(* allocator_impl_destroy_fn)(struct allocator *alloc)
Destroy the allocator.
b8(* allocator_impl_create_fn)(struct allocator *alloc, usize size)
Create a specific allocator.
void *(* allocator_impl_allocate_fn)(struct allocator *alloc, usize size)
Allocate memory.
void * memory_copy(void *dst, const void *src, usize size)
Copy memory.
void * memory_aligned_address(void *addr, usize alignment)
Calculate properly aligned memory block address.
void * memory_zero(void *mem, usize size)
Zero out memory.
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define KB_MIN(x, y)
Ternary to get the minimum of two numbers.
#define KB_NULL
Value of an invalid ptr (nullptr).
b8 free_list_allocator_create(allocator *alloc, usize size)
Function signatures for free list based allocator implementation.
b8 linear_allocator_create(allocator *alloc, usize size)
Function signatures for linear allocator implementation.
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
#define KB_ERROR(...)
Log entry with error log level.
#define KB_ASSERT_DEBUG(expr,...)
Perform runtime debug assertion and log failures.
allocator_impl_allocate_fn allocate
Allocator's allocate implementation.
allocator_impl_free_fn free
Allocator's free implementation.
allocator_impl_destroy_fn destroy
Allocator's destroy implementation.
allocator_impl_free_all_fn free_all
Allocator's free_all implementation.
Central allocator structure.
void * state
Type specific state of the allocator.
allocator_impl impl
Type specific implementation of the allocator.
b8 system_allocator_create(allocator *alloc, usize size)
Function signatures for system allocator implementation.