1
0
Fork 0

Compare commits

..

No commits in common. "d475a451a35198a2e128f0f0c1221b4c209c0644" and "55632d07eb170244cf6a0688ecd62b12d29918cc" have entirely different histories.

7 changed files with 31 additions and 114 deletions

View File

@ -6,8 +6,6 @@ if not exist ".\build" mkdir ".\build"
pushd .\build pushd .\build
set link= user32.lib cl /Zi ..\src\main.c /Feapp.exe /Od /nologo
cl /Zi /W4 ..\src\main.c /Feapp.exe /Od /nologo /link %link%
popd .\build popd .\build

View File

@ -1,43 +1,40 @@
internal Arena * internal Arena *
arena_initialize(u64 size, b8 growable) arena_initialize(u64 size)
{ {
Arena *result = 0; Arena *result = 0;
void *backing_buffer = platform_memory_reserve(size);
void *backing_buffer = malloc(size);
if (backing_buffer != NULL) if (backing_buffer != NULL)
{ {
platform_memory_commit(backing_buffer, ARENA_INITIAL_COMMIT_SIZE);
result = (Arena*) backing_buffer; result = (Arena*) backing_buffer;
result->backing_buffer = backing_buffer;
// rhjr: immutable arena header result->buffer = backing_buffer;
result->base_pos = sizeof(Arena); result->offset = sizeof(Arena);
result->commit_pos = sizeof(Arena); result->size = sizeof(Arena) + size;
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; return result;
} }
internal void * internal void *
arena_allocate(Arena *arena, u64 size) 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 internal void
arena_release(Arena *arena) arena_release(Arena *arena)
{ {
arena->offset = 0;
} }

View File

@ -1,32 +1,19 @@
#ifndef ARENA_H #ifndef ARENA_H
#define 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; typedef struct Arena Arena;
struct Arena { struct Arena {
struct Arena *current; u64 *buffer;
struct Arena *prev; u64 offset;
u64 *backing_buffer;
u64 base_pos;
u64 commit_pos;
u64 size; u64 size;
b8 growable;
}; };
STATIC_ASSERT(ARENA_INITIAL_COMMIT_SIZE <= ARENA_DEFAULT_RESERVE_SIZE, internal Arena * arena_initialize(u64 size);
arena_default_allocation_size);
//= rhjr: arenas internal void * arena_allocate(Arena *arena, u64 size);
internal void arena_release(Arena *arena);
internal Arena * arena_initialize(u64 size, b8 growable); //= rhjr: arena helpers
internal Arena * arena_initialize_default();
internal void * arena_allocate(Arena *arena, u64 size);
#define arena_push_array(arena, type, count) \ #define arena_push_array(arena, type, count) \
(type*) arena_allocate((arena), sizeof(type) * (count)) (type*) arena_allocate((arena), sizeof(type) * (count))
@ -34,6 +21,11 @@ internal void * arena_allocate(Arena *arena, u64 size);
#define arena_push(arena, type) \ #define arena_push(arena, type) \
(type*) arena_allocate((arena), sizeof(type)) (type*) arena_allocate((arena), sizeof(type))
internal void arena_release(Arena *arena); //= 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 // ARENA_H #endif // ARENA_H

View File

@ -18,26 +18,10 @@ typedef int16_t i16;
typedef int32_t i32; typedef int32_t i32;
typedef int64_t i64; 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 KB(b) ((b) << 10)
#define MB(b) ((b) << 20) #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 STATEMENT(S) do{ S } while(0)
#define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 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 #endif // BASE_H

View File

@ -1,13 +1,10 @@
#include "base.h" #include "base.h"
#include "arena.h" #include "arena.h"
#include "win32_platform.h"
#include "arena.c" #include "arena.c"
#include "win32_platform.c"
int int
main(void) main(void)
{ {
Arena *arena = arena_initialize_default();
return 0; return 0;
} }

View File

@ -1,34 +0,0 @@
internal void *
platform_memory_reserve(u64 size)
{
void *result = VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE);
return result;
}
internal b8
platform_memory_commit(void *ptr, u64 size)
{
b32 result = (VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE) != 0);
return result;
}
internal void
platform_memory_decommit(void *ptr, u64 size)
{
VirtualFree(ptr, size, MEM_DECOMMIT);
};
internal void
platform_memory_release(void *ptr, u64 size)
{
VirtualFree(ptr, 0, MEM_RELEASE);
}
internal u64
platform_get_page_size(void)
{
SYSTEM_INFO sysinfo = {0};
GetSystemInfo(&sysinfo);
return sysinfo.dwPageSize;
}

View File

@ -1,17 +0,0 @@
#ifndef WIN32_PLATFORM_H
#define WIN32_PLATFORM_H
#undef internal
#include <Windows.h>
#define internal static
//= rhjr: platform memory management
internal void * platform_memory_reserve(u64 size);
internal b8 platform_memory_commit(void *ptr, u64 size);
internal void platform_memory_decommit(void *ptr, u64 size);
internal void platform_memory_release(void *ptr, u64 size);
internal u64 platform_get_page_size(void);
#endif // WIN32_PLATFORM_H