1
0
Fork 0

Added: simple memory alignment mechanism

Browse Source
This commit is contained in:
rhuibertsjr 2024-04-26 17:23:37 +02:00
parent 304f9a1d0b
commit 4cc6fce555
3 changed files with 9 additions and 4 deletions

View File

@ -37,8 +37,9 @@ internal void *
arena_allocate(Arena *arena, u64 size) arena_allocate(Arena *arena, u64 size)
{ {
Arena *current = arena->current; Arena *current = arena->current;
u64 pos_mem = current->offset; u64 pos_mem =
u64 pos_new = current->offset + size; memory_align_power_of_two(current->offset, ARENA_DEFAULT_ALIGNMENT);
u64 pos_new = current->offset + size;
if (current->size < pos_new && current->growable) if (current->size < pos_new && current->growable)
{ {
@ -58,7 +59,8 @@ arena_allocate(Arena *arena, u64 size)
sll_stack_push(arena->current, new_memory_bock, prev); sll_stack_push(arena->current, new_memory_bock, prev);
current = new_memory_bock; current = new_memory_bock;
pos_mem = current->offset; pos_mem =
memory_align_power_of_two(current->offset, ARENA_DEFAULT_ALIGNMENT);
pos_new = current->offset + size; pos_new = current->offset + size;
} }

View File

@ -2,6 +2,7 @@
#define ARENA_H #define ARENA_H
#define ARENA_INITIAL_COMMIT_SIZE sizeof(struct Arena) #define ARENA_INITIAL_COMMIT_SIZE sizeof(struct Arena)
#define ARENA_DEFAULT_ALIGNMENT 8
#ifndef ARENA_DEFAULT_RESERVE_SIZE #ifndef ARENA_DEFAULT_RESERVE_SIZE
# define ARENA_DEFAULT_RESERVE_SIZE KB(4) # define ARENA_DEFAULT_RESERVE_SIZE KB(4)
@ -19,7 +20,7 @@ struct Arena {
}; };
STATIC_ASSERT(ARENA_INITIAL_COMMIT_SIZE <= ARENA_DEFAULT_RESERVE_SIZE, STATIC_ASSERT(ARENA_INITIAL_COMMIT_SIZE <= ARENA_DEFAULT_RESERVE_SIZE,
arena_default_allocation_size); arena_default_allocation_size);
//= rhjr: arenas //= rhjr: arenas

View File

@ -39,6 +39,8 @@ typedef uint8_t b64;
//= rhjr: memory helpers //= rhjr: memory helpers
#define memory_align_power_of_two(x,b) (((x) + (b) - 1)&(~((b) - 1)))
#define memory_zero(s,z) memset((s), 0, (z)) #define memory_zero(s,z) memset((s), 0, (z))
#define memory_zero_struct(s) memory_zero((s), sizeof(*(s))) #define memory_zero_struct(s) memory_zero((s), sizeof(*(s)))