-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool_allocator.h
37 lines (27 loc) · 1003 Bytes
/
pool_allocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
enum PoolAllocatorResult{
Success=0x0,
NotEnoughMemory=-1,
UnalignedFree=-2,
OutOfRange=-3,
DoubleFree=-4
} ;
typedef enum PoolAllocatorResult PoolAllocatorResult;
typedef struct PoolAllocator{
char* buffer; //contiguous buffer managed by the system
int* free_list; //list of linked objects
int buffer_size; //size of the buffer in bytes
int size; //number of free blocks
int size_max; //maximum number of blocks
int item_size; //size of a block
int first_idx; //pointer to the first bucket
int bucket_size; // size of a bucket
} PoolAllocator;
PoolAllocatorResult PoolAllocator_init(PoolAllocator* allocator,
int item_size,
int num_items,
char* memory_block,
int memory_size);
void* PoolAllocator_getBlock(PoolAllocator* allocator);
PoolAllocatorResult PoolAllocator_releaseBlock(PoolAllocator* allocator, void* block);
const char* PoolAllocator_strerror(PoolAllocatorResult result);