|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <stdint.h> |
| 4 | + |
| 5 | +#define MEMORY_BLOCK_ALLOCATOR_MIN_BITS 3 |
| 6 | +#define MEMORY_BLOCK_ALLOCATOR_EXP 20 |
| 7 | +#define MEMORY_BLOCK_ALLOCATOR_MIN_SIZE (1 << MEMORY_BLOCK_ALLOCATOR_MIN_BITS) |
| 8 | +#define MEMORY_BLOCK_ALLOCATOR_MAX_SIZE \ |
| 9 | + (1 << (MEMORY_BLOCK_ALLOCATOR_MIN_BITS + MEMORY_BLOCK_ALLOCATOR_EXP)) |
| 10 | + |
| 11 | +struct block_allocator_pool { |
| 12 | + uint64_t allocate; |
| 13 | + uint64_t free; |
| 14 | + uint64_t borrow; |
| 15 | + |
| 16 | + void *free_list; |
| 17 | +}; |
| 18 | + |
| 19 | +typedef void *(*block_allocator_alloc_func)(size_t size, void *data); |
| 20 | + |
| 21 | +struct block_allocator { |
| 22 | + block_allocator_alloc_func alloc_func; |
| 23 | + void *alloc_func_data; |
| 24 | + struct block_allocator_pool pools[MEMORY_BLOCK_ALLOCATOR_EXP]; |
| 25 | +}; |
| 26 | + |
| 27 | +static inline int |
| 28 | +block_allocator_init( |
| 29 | + struct block_allocator *allocator, |
| 30 | + block_allocator_alloc_func alloc_func, |
| 31 | + void *alloc_func_data |
| 32 | + |
| 33 | +) { |
| 34 | + allocator->alloc_func = alloc_func; |
| 35 | + allocator->alloc_func_data = alloc_func_data; |
| 36 | + |
| 37 | + for (size_t pool_idx = 0; pool_idx < MEMORY_BLOCK_ALLOCATOR_EXP; |
| 38 | + ++pool_idx) { |
| 39 | + allocator->pools[pool_idx].allocate = 0; |
| 40 | + allocator->pools[pool_idx].free = 0; |
| 41 | + allocator->pools[pool_idx].borrow = 0; |
| 42 | + allocator->pools[pool_idx].free_list = NULL; |
| 43 | + } |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +static inline size_t |
| 49 | +block_allocator_pool_size( |
| 50 | + struct block_allocator *allocator, size_t pool_index |
| 51 | +) { |
| 52 | + (void)allocator; |
| 53 | + |
| 54 | + return 1 << (MEMORY_BLOCK_ALLOCATOR_MIN_BITS + pool_index); |
| 55 | +} |
| 56 | + |
| 57 | +static inline size_t |
| 58 | +block_allocator_pool_index(struct block_allocator *allocator, size_t size) { |
| 59 | + (void)allocator; |
| 60 | + |
| 61 | + size = (size << 1) - 1; |
| 62 | + size >>= MEMORY_BLOCK_ALLOCATOR_MIN_BITS; |
| 63 | + |
| 64 | + return sizeof(long long) * 8 - 1 - __builtin_clzll(size); |
| 65 | +} |
| 66 | + |
| 67 | +static inline void * |
| 68 | +block_allocator_pool_get( |
| 69 | + struct block_allocator *allocator, struct block_allocator_pool *pool |
| 70 | +) { |
| 71 | + (void)allocator; |
| 72 | + |
| 73 | + void *result = pool->free_list; |
| 74 | + pool->free_list = *(void **)pool->free_list; |
| 75 | + ++pool->allocate; |
| 76 | + --pool->free; |
| 77 | + return result; |
| 78 | +} |
| 79 | + |
| 80 | +static inline void |
| 81 | +block_allocator_pool_borrow( |
| 82 | + struct block_allocator *allocator, size_t pool_index |
| 83 | +) { |
| 84 | + // Get a memory chunk from parent pool |
| 85 | + struct block_allocator_pool *parent_pool = |
| 86 | + allocator->pools + pool_index + 1; |
| 87 | + void *data = block_allocator_pool_get(allocator, parent_pool); |
| 88 | + |
| 89 | + struct block_allocator_pool *pool = allocator->pools + pool_index; |
| 90 | + |
| 91 | + // Split the memory chunk into two piece and insert into free list |
| 92 | + size_t size = block_allocator_pool_size(allocator, pool_index); |
| 93 | + void *next_data = (void *)((uintptr_t)data + size); |
| 94 | + *(void **)data = next_data; |
| 95 | + *(void **)next_data = pool->free_list; |
| 96 | + pool->free_list = data; |
| 97 | + |
| 98 | + ++parent_pool->borrow; |
| 99 | + pool->free += 2; |
| 100 | +} |
| 101 | + |
| 102 | +static inline void * |
| 103 | +block_allocator_balloc(struct block_allocator *allocator, size_t size) { |
| 104 | + if (size < MEMORY_BLOCK_ALLOCATOR_MIN_SIZE) |
| 105 | + size = MEMORY_BLOCK_ALLOCATOR_MIN_SIZE; |
| 106 | + |
| 107 | + if (size > MEMORY_BLOCK_ALLOCATOR_MAX_SIZE) |
| 108 | + return NULL; |
| 109 | + |
| 110 | + size_t pool_index = block_allocator_pool_index(allocator, size); |
| 111 | + |
| 112 | + struct block_allocator_pool *pool = allocator->pools + pool_index; |
| 113 | + |
| 114 | + if (pool->free_list == NULL) { |
| 115 | + /* |
| 116 | + * Look for the first parent pool with free memory block |
| 117 | + * available and then recursively borrow memory block. |
| 118 | + */ |
| 119 | + size_t parent_pool_index = pool_index + 1; |
| 120 | + while (parent_pool_index < MEMORY_BLOCK_ALLOCATOR_EXP && |
| 121 | + allocator->pools[parent_pool_index].free_list == NULL) { |
| 122 | + ++parent_pool_index; |
| 123 | + } |
| 124 | + |
| 125 | + if (parent_pool_index == MEMORY_BLOCK_ALLOCATOR_EXP) { |
| 126 | + size_t alloc_size = block_allocator_pool_size( |
| 127 | + allocator, parent_pool_index - 1 |
| 128 | + ); |
| 129 | + |
| 130 | + void *data = allocator->alloc_func( |
| 131 | + alloc_size, allocator->alloc_func_data |
| 132 | + ); |
| 133 | + if (data == NULL) |
| 134 | + return NULL; |
| 135 | + |
| 136 | + struct block_allocator_pool *root = |
| 137 | + allocator->pools + MEMORY_BLOCK_ALLOCATOR_EXP - |
| 138 | + 1; |
| 139 | + ++root->free; |
| 140 | + root->free_list = data; |
| 141 | + *(void **)data = NULL; |
| 142 | + --parent_pool_index; |
| 143 | + } |
| 144 | + |
| 145 | + while (parent_pool_index-- > pool_index) { |
| 146 | + block_allocator_pool_borrow( |
| 147 | + allocator, parent_pool_index |
| 148 | + ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return block_allocator_pool_get(allocator, pool); |
| 153 | +} |
| 154 | + |
| 155 | +static inline void |
| 156 | +block_allocator_bfree( |
| 157 | + struct block_allocator *allocator, void *block, size_t size |
| 158 | +) { |
| 159 | + size_t pool_index = block_allocator_pool_index(allocator, size); |
| 160 | + struct block_allocator_pool *pool = allocator->pools + pool_index; |
| 161 | + |
| 162 | + *(void **)block = pool->free_list; |
| 163 | + pool->free_list = block; |
| 164 | + ++pool->free; |
| 165 | +} |
0 commit comments