
- Some compile options that make sense for build.bat, and a build_release.bat - oogabooga: - Big buffer program_memory that always has same base address (goodbye serialisation problems) - Big buffer grows as needed, crash when out of memory - Very sexy platform abstraction & basic os call procedures - Heap allocator around the program memory. - Jaiyfication (context, context.allocator, push_allocator, temp allocator) - Some tests to make sure we ooga the right boogas
92 lines
1.9 KiB
C
92 lines
1.9 KiB
C
|
|
#include <stdint.h>
|
|
|
|
typedef uint8_t u8;
|
|
typedef uint16_t u16;
|
|
typedef uint32_t u32;
|
|
typedef uint64_t u64;
|
|
typedef int8_t s8;
|
|
typedef int16_t s16;
|
|
typedef int32_t s32;
|
|
typedef int64_t s64;
|
|
|
|
typedef float f32;
|
|
typedef double f64;
|
|
typedef f32 float32;
|
|
typedef f64 float64;
|
|
|
|
typedef u8 bool;
|
|
#define false 0
|
|
#define true 1
|
|
|
|
#define thread_local _Thread_local
|
|
|
|
#ifdef _MSC_VER
|
|
#define os_debug_break __debugbreak
|
|
#else
|
|
#error "Only msvc compiler supported at the moment";
|
|
#endif
|
|
|
|
#define assert(cond, ...) if (!(cond)) { printf("Assertion failed for condition: " #cond ". Message: " __VA_ARGS__); os_debug_break(); }
|
|
|
|
#ifndef max
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
|
#define min(a, b) ((a) < (b) ? (a) : (b))
|
|
#endif
|
|
|
|
#define cast(t) (t)
|
|
|
|
typedef struct Nothing {int nothing;} Nothing;
|
|
|
|
#ifndef CONTEXT_EXTRA
|
|
#define CONTEXT_EXTRA Nothing
|
|
#endif
|
|
|
|
typedef enum Allocator_Message {
|
|
ALLOCATOR_ALLOCATE,
|
|
ALLOCATOR_DEALLOCATE,
|
|
} Allocator_Message;
|
|
typedef void*(*Allocator_Proc)(u64, void*, Allocator_Message);
|
|
|
|
typedef struct Allocator {
|
|
Allocator_Proc proc;
|
|
void *data;
|
|
} Allocator;
|
|
|
|
typedef struct Context {
|
|
Allocator allocator;
|
|
|
|
CONTEXT_EXTRA extra;
|
|
} Context;
|
|
|
|
|
|
#define CONTEXT_STACK_MAX 512
|
|
thread_local Context context;
|
|
thread_local Context context_stack[CONTEXT_STACK_MAX];
|
|
thread_local u64 num_contexts = 0;
|
|
|
|
|
|
void* alloc(u64 size) { return context.allocator.proc(size, NULL, ALLOCATOR_ALLOCATE); }
|
|
void dealloc(void *p) { context.allocator.proc(0, p, ALLOCATOR_DEALLOCATE); }
|
|
|
|
|
|
|
|
void push_context(Context c) {
|
|
assert(num_contexts < CONTEXT_STACK_MAX, "Context stack overflow");
|
|
|
|
context_stack[num_contexts] = context;
|
|
context = c;
|
|
num_contexts += 1;
|
|
}
|
|
void pop_context() {
|
|
assert(num_contexts > 0, "No contexts to pop!");
|
|
num_contexts -= 1;
|
|
context = context_stack[num_contexts];
|
|
}
|
|
|
|
void push_allocator(Allocator a) {
|
|
Context c = context;
|
|
c.allocator = a;
|
|
push_context(c);
|
|
}
|
|
void pop_allocator() { pop_context(); }
|