#include <stdint.h>
Go to the source code of this file.
◆ BuddyAllocator
◆ BAResult
Enumerator |
---|
BA_SUCCESS | |
BA_NOT_ENOUGH_MEMORY | |
BA_INVALID_ALIGNMENT | |
Definition at line 22 of file buddy_allocator.h.
◆ buddy_allocator_allocate()
BAResult buddy_allocator_allocate |
( |
BuddyAllocator * |
allocator, |
|
|
uint64_t |
size, |
|
|
uint64_t |
alignment, |
|
|
uint64_t * |
offset |
|
) |
| |
Definition at line 97 of file buddy_allocator.c.
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)
Referenced by allocate_device_memory().
◆ buddy_allocator_free()
◆ create_buddy_allocator()
BuddyAllocator* create_buddy_allocator |
( |
uint64_t |
capacity, |
|
|
uint64_t |
block_size |
|
) |
| |
Definition at line 59 of file buddy_allocator.c.
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++)
Referenced by create_sub_allocator().
◆ destroy_buddy_allocator()