Compare commits
3 Commits
55632d07eb
...
d475a451a3
Author | SHA1 | Date | |
---|---|---|---|
rhuibertsjr | d475a451a3 | ||
rhuibertsjr | aa2642d46c | ||
rhuibertsjr | 792ddeb80d |
|
@ -6,6 +6,8 @@ if not exist ".\build" mkdir ".\build"
|
||||||
|
|
||||||
pushd .\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
|
popd .\build
|
||||||
|
|
41
src/arena.c
41
src/arena.c
|
@ -1,40 +1,43 @@
|
||||||
internal Arena *
|
internal Arena *
|
||||||
arena_initialize(u64 size)
|
arena_initialize(u64 size, b8 growable)
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
result = (Arena*) backing_buffer;
|
platform_memory_commit(backing_buffer, ARENA_INITIAL_COMMIT_SIZE);
|
||||||
|
|
||||||
result->buffer = backing_buffer;
|
result = (Arena*) backing_buffer;
|
||||||
result->offset = sizeof(Arena);
|
result->backing_buffer = backing_buffer;
|
||||||
result->size = sizeof(Arena) + size;
|
|
||||||
|
// 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;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
32
src/arena.h
32
src/arena.h
|
@ -1,19 +1,32 @@
|
||||||
#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 {
|
||||||
u64 *buffer;
|
struct Arena *current;
|
||||||
u64 offset;
|
struct Arena *prev;
|
||||||
|
u64 *backing_buffer;
|
||||||
|
u64 base_pos;
|
||||||
|
u64 commit_pos;
|
||||||
u64 size;
|
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);
|
//= rhjr: arenas
|
||||||
internal void arena_release(Arena *arena);
|
|
||||||
|
|
||||||
//= 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) \
|
#define arena_push_array(arena, type, count) \
|
||||||
(type*) arena_allocate((arena), sizeof(type) * (count))
|
(type*) arena_allocate((arena), sizeof(type) * (count))
|
||||||
|
@ -21,11 +34,6 @@ internal void arena_release(Arena *arena);
|
||||||
#define arena_push(arena, type) \
|
#define arena_push(arena, type) \
|
||||||
(type*) arena_allocate((arena), sizeof(type))
|
(type*) arena_allocate((arena), sizeof(type))
|
||||||
|
|
||||||
//= rhjr: memory helpers
|
internal void arena_release(Arena *arena);
|
||||||
|
|
||||||
#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
|
||||||
|
|
16
src/base.h
16
src/base.h
|
@ -18,10 +18,26 @@ 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
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
#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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
Loading…
Reference in New Issue
Block a user