kiba-engine
allocator.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <kiba/defines.h>
11 
18 typedef enum allocator_type {
19  ALLOCATOR_LINEAR,
20  ALLOCATOR_FREE_LIST,
21  ALLOCATOR_SYSTEM,
23 
24 struct allocator;
25 
38 typedef b8 (*allocator_impl_create_fn)(struct allocator *alloc, usize size);
46 typedef void (*allocator_impl_destroy_fn)(struct allocator *alloc);
54 typedef void *(*allocator_impl_allocate_fn)(struct allocator *alloc, usize size);
62 typedef void (*allocator_impl_free_fn)(struct allocator *alloc, void *mem, usize size);
68 typedef void (*allocator_impl_free_all_fn)(struct allocator *alloc);
69 
71 typedef struct allocator_impl {
81 
87 typedef struct allocator {
91  void *state;
93 
102 KB_API b8 allocator_create(allocator *alloc, allocator_type type, usize size);
103 
114 KB_API b8 allocator_create_custom(allocator *alloc, allocator_impl_create_fn create_fn, usize size);
115 
123 KB_API void allocator_destroy(allocator *alloc);
124 
135 KB_API void *allocator_allocate(allocator *alloc, usize size);
136 
152 KB_API void *allocator_allocate_aligned(allocator *alloc, usize size, usize alignment);
153 
165 KB_API void *allocator_reallocate(allocator *alloc, void *original, usize new_size);
166 
185 KB_API void *allocator_reallocate_aligned(allocator *alloc, void *original, usize new_size, usize alignment);
186 
196 KB_API void allocator_free(allocator *alloc, void *mem);
197 
207 KB_API void allocator_free_all(allocator *alloc);
208 
218 KB_API void allocator_implementation(allocator *alloc,
222  allocator_impl_free_all_fn free_all);
KB_API void * allocator_reallocate_aligned(allocator *alloc, void *original, usize new_size, usize alignment)
Rellocate aligned memory.
Definition: allocator.c:111
allocator_type
Available allocator types.
Definition: allocator.h:18
void(* allocator_impl_free_all_fn)(struct allocator *alloc)
Give all allocated memory back to the allocator.
Definition: allocator.h:68
KB_API void * allocator_reallocate(allocator *alloc, void *original, usize new_size)
Reallocate memory to unaligned block.
Definition: allocator.c:107
KB_API b8 allocator_create(allocator *alloc, allocator_type type, usize size)
Create an allocator of a specific type.
Definition: allocator.c:66
KB_API void * allocator_allocate(allocator *alloc, usize size)
Allocate unaligned memory.
Definition: allocator.c:92
KB_API void allocator_destroy(allocator *alloc)
Destroy an allocator.
Definition: allocator.c:84
void(* allocator_impl_free_fn)(struct allocator *alloc, void *mem, usize size)
Give memory back to the allocator.
Definition: allocator.h:62
KB_API void * allocator_allocate_aligned(allocator *alloc, usize size, usize alignment)
Allocate aligned memory.
Definition: allocator.c:94
KB_API void allocator_free(allocator *alloc, void *mem)
Give back memory to the allocator.
Definition: allocator.c:134
KB_API void allocator_free_all(allocator *alloc)
Reset the allocator.
Definition: allocator.c:143
struct allocator allocator
Central allocator structure.
struct allocator_impl allocator_impl
Structure holding a specific allocator type's implementation.
void(* allocator_impl_destroy_fn)(struct allocator *alloc)
Destroy the allocator.
Definition: allocator.h:46
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.
Definition: allocator.c:79
b8(* allocator_impl_create_fn)(struct allocator *alloc, usize size)
Create a specific allocator.
Definition: allocator.h:38
void *(* allocator_impl_allocate_fn)(struct allocator *alloc, usize size)
Allocate memory.
Definition: allocator.h:54
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.
Definition: allocator.c:149
Global typedefs and macros.
Structure holding a specific allocator type's implementation.
Definition: allocator.h:71
allocator_impl_allocate_fn allocate
Allocator's allocate implementation.
Definition: allocator.h:75
allocator_impl_free_fn free
Allocator's free implementation.
Definition: allocator.h:77
allocator_impl_destroy_fn destroy
Allocator's destroy implementation.
Definition: allocator.h:73
allocator_impl_free_all_fn free_all
Allocator's free_all implementation.
Definition: allocator.h:79
Central allocator structure.
Definition: allocator.h:87
void * state
Type specific state of the allocator.
Definition: allocator.h:91
allocator_impl impl
Type specific implementation of the allocator.
Definition: allocator.h:89