Add UMM/IMM type + minor fixes
This commit is contained in:
@@ -144,8 +144,6 @@ void arena_trim(Arena *arena)
|
||||
U32 aligned_pos = RoundUpToMultiple(block->first_free, arena->commit_size);
|
||||
if (aligned_pos < block->committed_end) {
|
||||
void *trim_start = &block->base[aligned_pos];
|
||||
// Note(nos): Should we unpoision ?
|
||||
//ASAN_UNPOISON_MEMORY_REGION(trim_start, block->committed_end);
|
||||
memory_decommit(trim_start, block->committed_end - aligned_pos);
|
||||
block->committed_end = aligned_pos;
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
typedef U32 ArenaFlags;
|
||||
typedef struct ArenaOptions ArenaOptions;
|
||||
enum {
|
||||
ArenaFlag_NoDecommit = 1, // operations which shrinks the arena does not decommit memory.
|
||||
ArenaFlag_NoDecommit = (1 << 0), // operations which shrinks the arena does not decommit memory.
|
||||
// (arena_trim() can still decommit when called explicitly)
|
||||
};
|
||||
struct ArenaOptions {
|
||||
|
||||
@@ -24,6 +24,8 @@ typedef uint8_t U8;
|
||||
typedef uint16_t U16;
|
||||
typedef uint32_t U32;
|
||||
typedef uint64_t U64;
|
||||
typedef uintptr_t UMM;
|
||||
typedef intptr_t IMM;
|
||||
typedef float F32;
|
||||
typedef double F64;
|
||||
#if defined(__FLT128_MANT_DIG__)
|
||||
|
||||
+1
-1
@@ -110,7 +110,7 @@ static INLINE void *marena_allocator_func(Allocator *allocator, AllocOperation o
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Assert(false);
|
||||
ShouldNotHappen;
|
||||
}
|
||||
|
||||
static INLINE Allocator marena_allocator(MArena *arena)
|
||||
|
||||
+3
-3
@@ -73,7 +73,7 @@ void *memory_allocate_and_commit(size_t size)
|
||||
void memory_free(void *mem, size_t size)
|
||||
{
|
||||
TRACEF(" memory_free size=%7zu mem=%p\n", size, mem);
|
||||
Assert(IsPageAligned((uintptr_t)mem));
|
||||
Assert(IsPageAligned((UMM)mem));
|
||||
Assert(IsPageAligned(size));
|
||||
int rc = munmap(mem, size);
|
||||
|
||||
@@ -93,7 +93,7 @@ void *memory_reserve(size_t size)
|
||||
|
||||
void memory_commit(void *mem, size_t size)
|
||||
{
|
||||
Assert(IsPageAligned((uintptr_t)mem));
|
||||
Assert(IsPageAligned((UMM)mem));
|
||||
Assert(IsPageAligned(size));
|
||||
TRACEF("memory_commit size=%7zu mem=%p\n", size, mem);
|
||||
|
||||
@@ -107,7 +107,7 @@ void memory_commit(void *mem, size_t size)
|
||||
void memory_decommit(void *mem, size_t size)
|
||||
{
|
||||
TRACEF("memory_decommit size=%7zu mem=%p\n", size, mem);
|
||||
Assert(IsPageAligned((uintptr_t)mem));
|
||||
Assert(IsPageAligned((UMM)mem));
|
||||
Assert(IsPageAligned(size));
|
||||
int rc;
|
||||
rc = mprotect(mem, size, PROT_NONE);
|
||||
|
||||
+1
-1
@@ -3,12 +3,12 @@
|
||||
APool *apool_create_ex(Allocator *allocator, const APoolCreateOptions *options)
|
||||
{
|
||||
Assert(IsPow2(options->element_align));
|
||||
Assert(options->n_reserved > 0 || options->flags & APoolFlag_AutoGrow);
|
||||
U32 element_align = options->element_align;
|
||||
if (element_align < alignof(APoolElement)) {
|
||||
element_align = alignof(APoolElement);
|
||||
}
|
||||
U32 element_size = AlignUp(options->element_size, options->element_align);
|
||||
|
||||
APool *pool = allocator_push_type(allocator, APool);
|
||||
Assert(pool != NULL);
|
||||
pool->element_align= element_align;
|
||||
|
||||
@@ -32,4 +32,12 @@ struct APoolCreateOptions {
|
||||
apool_create_ex(arena, &(APoolCreateOptions){sizeof(type), alignof(type), n_reserved, 0})
|
||||
APool *apool_create_ex(Allocator *allocator, const APoolCreateOptions *options);
|
||||
void *apool_alloc(APool *pool);
|
||||
INLINE void apool_allocz(APool *pool)
|
||||
{
|
||||
void *ptr = apool_alloc(pool);
|
||||
if (ptr != NULL) {
|
||||
MemoryZero(ptr, pool->element_size);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
void apool_free(APool *pool, void *element);
|
||||
|
||||
+4
-4
@@ -151,14 +151,14 @@ static void arena_test_4(void)
|
||||
U32 push_size = block_size - MemoryBlock_Header_Size;
|
||||
|
||||
void *ptr = arena_push(&arena, push_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)ptr, page_size));
|
||||
Assert(IsAlignedTo((UMM)ptr, page_size));
|
||||
MemoryZero(ptr, push_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", push_size, ptr);
|
||||
arena_dump(&arena);
|
||||
|
||||
// A second page-aligned push to make sure the cursor math still holds.
|
||||
ptr = arena_push(&arena, page_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)ptr, page_size));
|
||||
Assert(IsAlignedTo((UMM)ptr, page_size));
|
||||
MemoryZero(ptr, page_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", page_size, ptr);
|
||||
|
||||
@@ -185,14 +185,14 @@ static void arena_test_allocator_interface(void)
|
||||
U32 push_size = block_size - MemoryBlock_Header_Size;
|
||||
|
||||
void *ptr = allocator_push(&allocator, push_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)ptr, page_size));
|
||||
Assert(IsAlignedTo((UMM)ptr, page_size));
|
||||
MemoryZero(ptr, push_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", push_size, ptr);
|
||||
arena_dump(&arena);
|
||||
|
||||
// A second page-aligned push to make sure the cursor math still holds.
|
||||
ptr = allocator_push(&allocator, page_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)ptr, page_size));
|
||||
Assert(IsAlignedTo((UMM)ptr, page_size));
|
||||
MemoryZero(ptr, page_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", page_size, ptr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user