19 #include "shared/shared.h"
47 static inline size_t _align(
size_t value,
size_t alignment);
48 static inline int32_t
uint_log2(uint64_t x);
61 const uint32_t base_level =
uint_log2(block_size);
62 const uint32_t level_num =
uint_log2(capacity) - base_level + 1;
64 uint32_t block_num = 0;
65 for (uint32_t i = 0; i < level_num; i++)
66 block_num += 1 << ((level_num - 1) - i);
68 const size_t alignment = 16;
72 const size_t block_state_size =
_align(block_num *
sizeof(uint8_t), alignment);
73 char* memory = malloc(allocator_size + free_list_array_size + free_item_buffer_size + block_state_size);
78 allocator->
memory = memory;
81 allocator->
block_states = (uint8_t*)(memory + allocator_size + free_list_array_size + free_item_buffer_size);
85 memset(allocator->
block_states, 0, block_num *
sizeof(uint8_t));
87 for (uint32_t i = 0; i < block_num; i++)
106 const uint64_t block_size = (uint64_t)(1 << (
level + allocator->
base_level));
107 const uint64_t alignment_size = block_size % alignment;
109 assert(alignment_size == 0);
110 if (alignment_size != 0)
139 const uint64_t block_size = (uint64_t)(1 << (
level + allocator->
base_level));
157 assert(dst_level < src_level);
165 const uint32_t previous_level_block_offset =
get_level_offset(allocator, src_level - 1);
166 const uint32_t current_level_block_offset =
get_next_level_offset(allocator, src_level - 1, previous_level_block_offset);
186 if (src_level - 1 != dst_level)
207 const uint32_t next_level_block_index =
block_index / 2;
222 assert(item != NULL);
225 prev_next = &item->
next;
227 assert(item != NULL);
230 *prev_next = item->
next;
234 static inline size_t _align(
size_t value,
size_t alignment)
236 return (value + alignment - 1) / alignment * alignment;
271 for (int32_t i = 0; i <
level; i++)
272 offset += 1 << ((allocator->
level_num - 1) - i);
279 return offset + (1 << ((allocator->
level_num - 1) - prev_level));