Add allocator interface
This commit is contained in:
+49
-5
@@ -2,6 +2,7 @@
|
||||
#include "arena.h"
|
||||
#include "mini_arena.h"
|
||||
#include "platform.h"
|
||||
#include "pool.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -163,6 +164,40 @@ static void arena_test_4(void)
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
static void arena_test_allocator_interface(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
U32 page_size = platform_page_size();
|
||||
ArenaOptions arena_params = {};
|
||||
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
||||
U32 commit_size = KB(64);
|
||||
arena_params.size = arena_size;
|
||||
arena_params.commit_size = commit_size;
|
||||
Arena arena;
|
||||
arena_init(&arena, &arena_params);
|
||||
Allocator allocator = arena_allocator(&arena);
|
||||
|
||||
// block_size is the page-aligned arena_size. Request nearly the whole block
|
||||
// but with page alignment, so the alignment gap (a full page) forces a grow.
|
||||
U32 block_size = arena.block_size;
|
||||
U32 push_size = block_size - MemoryBlock_Header_Size;
|
||||
|
||||
void *ptr = allocator_push(&allocator, push_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)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));
|
||||
MemoryZero(ptr, page_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", page_size, ptr);
|
||||
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
static void arena_test_decommit(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
@@ -192,6 +227,8 @@ static void arena_test_decommit(void)
|
||||
|
||||
void miniarena_test1(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
#define arena_size 4096
|
||||
MARENA_DEFINE(arena, arena_size, 8);
|
||||
int i;
|
||||
@@ -228,9 +265,12 @@ struct PoolTest{
|
||||
|
||||
void apool_test(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Arena arena;
|
||||
arena_init(&arena, ArenaOptions_Default);
|
||||
APool *pool = apool_create_ex(&arena, 16, sizeof(PoolTest), alignof(PoolTest));
|
||||
Allocator allocator = arena_allocator(&arena);
|
||||
APool *pool = apool_create_ex(&allocator, 16, sizeof(PoolTest), alignof(PoolTest));
|
||||
PoolTest *list = NULL;
|
||||
for (U32 i = 0; i < U32_Max; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
@@ -271,9 +311,13 @@ void apool_test(void)
|
||||
|
||||
void apool_test_autogrow(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Arena arena;
|
||||
arena_init(&arena, ArenaOptions_Default);
|
||||
APool *pool = apool_create(&arena, APOOL_AUTOGROW, PoolTest);
|
||||
Allocator allocator = arena_allocator(&arena);
|
||||
|
||||
APool *pool = apool_create(&allocator, APOOL_AUTOGROW, PoolTest);
|
||||
PoolTest *list = NULL;
|
||||
for (U32 i = 0; i < 4; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
@@ -319,9 +363,9 @@ int main(int argc, char *argv[])
|
||||
// arena_test_3();
|
||||
// arena_test_4();
|
||||
arena_test_decommit();
|
||||
|
||||
arena_test_allocator_interface();
|
||||
// miniarena_test1();
|
||||
// apool_test();
|
||||
// apool_test_autogrow();
|
||||
apool_test();
|
||||
apool_test_autogrow();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user