#include "shared/shared.h"
#include "buddy_allocator.h"
#include <assert.h>
Go to the source code of this file.
|
static size_t | _align (size_t value, size_t alignment) |
|
static int32_t | uint_log2 (uint64_t x) |
|
static AllocatorFreeListItem * | allocate_list_item (BuddyAllocator *allocator) |
|
static void | free_list_item (BuddyAllocator *allocator, AllocatorFreeListItem *item) |
|
static void | write_free_block_to_list (BuddyAllocator *allocator, uint32_t level, uint32_t block_index) |
|
static uint32_t | get_level_offset (BuddyAllocator *allocator, uint32_t level) |
|
static uint32_t | get_next_level_offset (BuddyAllocator *allocator, uint32_t level, uint32_t offset) |
|
void | subdivide_block (BuddyAllocator *allocator, uint32_t src_level, uint32_t dst_level) |
|
qboolean | merge_blocks (BuddyAllocator *allocator, uint32_t level, uint32_t block_index) |
|
void | remove_block_from_free_list (BuddyAllocator *allocator, uint32_t level, uint32_t block_index) |
|
BuddyAllocator * | create_buddy_allocator (uint64_t capacity, uint64_t block_size) |
|
BAResult | buddy_allocator_allocate (BuddyAllocator *allocator, uint64_t size, uint64_t alignment, uint64_t *offset) |
|
void | buddy_allocator_free (BuddyAllocator *allocator, uint64_t offset, uint64_t size) |
|
void | destroy_buddy_allocator (BuddyAllocator *allocator) |
|
◆ AllocatorBlockState
◆ AllocatorFreeListItem
◆ BuddyAllocator
◆ AllocatorBlockState
Enumerator |
---|
BLOCK_NONE | |
BLOCK_FREE | |
BLOCK_SPLIT | |
BLOCK_ALLOCATED | |
Definition at line 23 of file buddy_allocator.c.
◆ _align()
static size_t _align |
( |
size_t |
value, |
|
|
size_t |
alignment |
|
) |
| |
|
inlinestatic |
◆ allocate_list_item()
◆ 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()
◆ free_list_item()
◆ get_level_offset()
static uint32_t get_level_offset |
( |
BuddyAllocator * |
allocator, |
|
|
uint32_t |
level |
|
) |
| |
|
inlinestatic |
◆ get_next_level_offset()
static uint32_t get_next_level_offset |
( |
BuddyAllocator * |
allocator, |
|
|
uint32_t |
level, |
|
|
uint32_t |
offset |
|
) |
| |
|
inlinestatic |
◆ merge_blocks()
qboolean merge_blocks |
( |
BuddyAllocator * |
allocator, |
|
|
uint32_t |
level, |
|
|
uint32_t |
block_index |
|
) |
| |
Definition at line 190 of file buddy_allocator.c.
199 const uint32_t buddy_block_index = (block_index % 2) == 0 ? block_index + 1 : block_index - 1;
207 const uint32_t next_level_block_index = block_index / 2;
Referenced by buddy_allocator_free().
◆ remove_block_from_free_list()
void remove_block_from_free_list |
( |
BuddyAllocator * |
allocator, |
|
|
uint32_t |
level, |
|
|
uint32_t |
block_index |
|
) |
| |
◆ subdivide_block()
void subdivide_block |
( |
BuddyAllocator * |
allocator, |
|
|
uint32_t |
src_level, |
|
|
uint32_t |
dst_level |
|
) |
| |
◆ uint_log2()
static int32_t uint_log2 |
( |
uint64_t |
x | ) |
|
|
inlinestatic |
◆ write_free_block_to_list()
static void write_free_block_to_list |
( |
BuddyAllocator * |
allocator, |
|
|
uint32_t |
level, |
|
|
uint32_t |
block_index |
|
) |
| |
|
inlinestatic |