1
0
Fork 0

Compare commits

..

3 Commits

Author SHA1 Message Date
rhuibertsjr d475a451a3 Added: Win32 libraries 2024-04-25 19:34:37 +02:00
rhuibertsjr aa2642d46c Changed: Arena structure and API 2024-04-25 19:34:00 +02:00
rhuibertsjr 792ddeb80d Added: Win32 platform layer 2024-04-25 19:33:44 +02:00
7 changed files with 115 additions and 32 deletions

View File

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

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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;
}

34
src/win32_platform.c Normal file
View File

@ -0,0 +1,34 @@
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;
}

17
src/win32_platform.h Normal file
View File

@ -0,0 +1,17 @@
#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