This repository has been archived on 2025-02-04. You can view files and clone it, but cannot push or open issues or pull requests.
helpless/oogabooga/base.c

93 lines
1.9 KiB
C
Raw Normal View History

#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(); }