Changed: Arena structure and API
This commit is contained in:
parent
792ddeb80d
commit
aa2642d46c
41
src/arena.c
41
src/arena.c
|
@ -1,40 +1,43 @@
|
|||
internal Arena *
|
||||
arena_initialize(u64 size)
|
||||
arena_initialize(u64 size, b8 growable)
|
||||
{
|
||||
Arena *result = 0;
|
||||
|
||||
void *backing_buffer = malloc(size);
|
||||
void *backing_buffer = platform_memory_reserve(size);
|
||||
|
||||
if (backing_buffer != NULL)
|
||||
{
|
||||
result = (Arena*) backing_buffer;
|
||||
platform_memory_commit(backing_buffer, ARENA_INITIAL_COMMIT_SIZE);
|
||||
|
||||
result->buffer = backing_buffer;
|
||||
result->offset = sizeof(Arena);
|
||||
result->size = sizeof(Arena) + size;
|
||||
result = (Arena*) backing_buffer;
|
||||
result->backing_buffer = backing_buffer;
|
||||
|
||||
// rhjr: immutable arena header
|
||||
result->base_pos = sizeof(Arena);
|
||||
result->commit_pos = sizeof(Arena);
|
||||
|
||||
result->growable = growable;
|
||||
result->size = size;
|
||||
}
|
||||
|
||||
// rhjr: arenas should be instantiated early on in the programs lifetime, that
|
||||
// is why this assertion will also be used in production.
|
||||
ASSERT(result != 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal Arena *
|
||||
arena_initialize_default()
|
||||
{
|
||||
Arena *result = arena_initialize(ARENA_DEFAULT_RESERVE_SIZE, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void *
|
||||
arena_allocate(Arena *arena, u64 size)
|
||||
{
|
||||
void *result = 0;
|
||||
|
||||
if (arena->offset + size <= arena->size)
|
||||
{
|
||||
result = (void*)((u64) arena->buffer + (u64) arena->offset);
|
||||
arena->offset += size;
|
||||
|
||||
memset(result, 0, size);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void
|
||||
arena_release(Arena *arena)
|
||||
{
|
||||
arena->offset = 0;
|
||||
}
|
||||
|
|
32
src/arena.h
32
src/arena.h
|
@ -1,19 +1,32 @@
|
|||
#ifndef ARENA_H
|
||||
#define ARENA_H
|
||||
|
||||
#define ARENA_INITIAL_COMMIT_SIZE sizeof(struct Arena)
|
||||
|
||||
#ifndef ARENA_DEFAULT_RESERVE_SIZE
|
||||
# define ARENA_DEFAULT_RESERVE_SIZE KB(4)
|
||||
#endif // ARENA_DEFAULT_RESERVE_SIZE
|
||||
|
||||
typedef struct Arena Arena;
|
||||
struct Arena {
|
||||
u64 *buffer;
|
||||
u64 offset;
|
||||
struct Arena *current;
|
||||
struct Arena *prev;
|
||||
u64 *backing_buffer;
|
||||
u64 base_pos;
|
||||
u64 commit_pos;
|
||||
u64 size;
|
||||
b8 growable;
|
||||
};
|
||||
|
||||
internal Arena * arena_initialize(u64 size);
|
||||
STATIC_ASSERT(ARENA_INITIAL_COMMIT_SIZE <= ARENA_DEFAULT_RESERVE_SIZE,
|
||||
arena_default_allocation_size);
|
||||
|
||||
internal void * arena_allocate(Arena *arena, u64 size);
|
||||
internal void arena_release(Arena *arena);
|
||||
//= rhjr: arenas
|
||||
|
||||
//= rhjr: arena helpers
|
||||
internal Arena * arena_initialize(u64 size, b8 growable);
|
||||
internal Arena * arena_initialize_default();
|
||||
|
||||
internal void * arena_allocate(Arena *arena, u64 size);
|
||||
|
||||
#define arena_push_array(arena, type, count) \
|
||||
(type*) arena_allocate((arena), sizeof(type) * (count))
|
||||
|
@ -21,11 +34,6 @@ internal void arena_release(Arena *arena);
|
|||
#define arena_push(arena, type) \
|
||||
(type*) arena_allocate((arena), sizeof(type))
|
||||
|
||||
//= rhjr: memory helpers
|
||||
|
||||
#define memory_zero(s,z) memset((s), 0, (z))
|
||||
#define memory_zero_struct(s) memory_zero((s), sizeof(*(s)))
|
||||
|
||||
#define memory_copy(dst, src, length) memmove((dst), (src), (length))
|
||||
internal void arena_release(Arena *arena);
|
||||
|
||||
#endif // ARENA_H
|
||||
|
|
16
src/base.h
16
src/base.h
|
@ -18,10 +18,26 @@ typedef int16_t i16;
|
|||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
typedef uint8_t b8;
|
||||
typedef uint8_t b16;
|
||||
typedef uint8_t b32;
|
||||
typedef uint8_t b64;
|
||||
|
||||
#define KB(b) ((b) << 10)
|
||||
#define MB(b) ((b) << 20)
|
||||
|
||||
#define Glue_(A,B) A##B
|
||||
#define Glue(A,B) Glue_(A,B)
|
||||
|
||||
#define STATEMENT(S) do{ S } while(0)
|
||||
#define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 0); } )
|
||||
#define STATIC_ASSERT(c,l) typedef u8 Glue(l,__LINE__) [(c)?1:-1]
|
||||
|
||||
//= rhjr: memory helpers
|
||||
|
||||
#define memory_zero(s,z) memset((s), 0, (z))
|
||||
#define memory_zero_struct(s) memory_zero((s), sizeof(*(s)))
|
||||
|
||||
#define memory_copy(dst, src, length) memmove((dst), (src), (length))
|
||||
|
||||
#endif // BASE_H
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
#include "base.h"
|
||||
#include "arena.h"
|
||||
#include "win32_platform.h"
|
||||
|
||||
#include "arena.c"
|
||||
#include "win32_platform.c"
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
Arena *arena = arena_initialize_default();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user