This commit is contained in:
Judah Caruso 2026-02-19 11:48:16 -07:00
parent c841ece42e
commit 6247cc7f84
3 changed files with 33 additions and 33 deletions

View file

@ -4,7 +4,7 @@
typedef enum { arena__alloc, arena__reset, arena__save, arena__restore, arena__release } arena__action; typedef enum { arena__alloc, arena__reset, arena__save, arena__restore, arena__release } arena__action;
typedef closure(void*, arena__action, ureg, ureg, void*, const char*, sreg) Arena; typedef closure(void*, arena__action, uregister, uregister, void*, const char*, sregister) Arena;
#define arena_new(A, T) cast(T*, A(arena__alloc, sizeof(T), alignof(T), 0, __FILE__, __LINE__)) #define arena_new(A, T) cast(T*, A(arena__alloc, sizeof(T), alignof(T), 0, __FILE__, __LINE__))
#define arena_make(A, C, T) cast(T*, A(arena__alloc, sizeof(T) * (C), alignof(T), 0, __FILE__, __LINE__)) #define arena_make(A, C, T) cast(T*, A(arena__alloc, sizeof(T) * (C), alignof(T), 0, __FILE__, __LINE__))
@ -14,13 +14,13 @@ typedef closure(void*, arena__action, ureg, ureg, void*, const char*, sreg) Aren
#define arena_restore(A, SP) A(arena__restore, 0, 0, (SP), __FILE__, __LINE__) #define arena_restore(A, SP) A(arena__restore, 0, 0, (SP), __FILE__, __LINE__)
// tiny macro to make arenas easier to declare // tiny macro to make arenas easier to declare
#define $(a1, a2, a3, a4, a5, a6) ^void* (arena__action a1, ureg a2, ureg a3, void* a4, const char* a5, sreg a6) #define $(a1, a2, a3, a4, a5, a6) ^void* (arena__action a1, uregister a2, uregister a3, void* a4, const char* a5, sregister a6)
static Arena static Arena
Static(u8* data, ureg count) { Static(uint8* data, uregister count) {
memset(data, 0, count); memset(data, 0, count);
capture ureg offset = 0; capture uregister offset = 0;
return persist($(action, size, align, savepoint, _2, _3) { return persist($(action, size, align, savepoint, _2, _3) {
switch (action) { switch (action) {
default: { default: {
@ -43,12 +43,12 @@ Static(u8* data, ureg count) {
case arena__save: { case arena__save: {
assert(savepoint != NULL && "Static: savepoint was null"); assert(savepoint != NULL && "Static: savepoint was null");
*cast(ureg*, savepoint) = offset; *cast(uregister*, savepoint) = offset;
} break; } break;
case arena__restore: { case arena__restore: {
assert(savepoint != NULL && "Static: savepoint was null"); assert(savepoint != NULL && "Static: savepoint was null");
offset = *cast(ureg*, savepoint); offset = *cast(uregister*, savepoint);
} break; } break;
} }
@ -57,7 +57,7 @@ Static(u8* data, ureg count) {
} }
static Arena static Arena
Linear(ureg max_memory) { Linear(uregister max_memory) {
void* ptr = malloc(max_memory); void* ptr = malloc(max_memory);
Arena backing = Static(ptr, max_memory); Arena backing = Static(ptr, max_memory);
return persist($(action, size, align, savepoint, file, line) { return persist($(action, size, align, savepoint, file, line) {
@ -89,7 +89,7 @@ Logger(Arena arena) {
static Arena static Arena
Region(Arena arena) { Region(Arena arena) {
capture ureg savepoint = 0; capture uregister savepoint = 0;
arena_save(arena, &savepoint); arena_save(arena, &savepoint);
return persist($(action, size, align, _, file, line) { return persist($(action, size, align, _, file, line) {

View file

@ -1,23 +1,23 @@
#include <stdint.h> #include <stdint.h>
typedef uint8_t u8; typedef uint8_t uint8;
typedef uint16_t u16; typedef uint16_t uint16;
typedef uint32_t u32; typedef uint32_t uint32;
typedef uint64_t u64; typedef uint64_t uint64;
typedef int8_t s8; typedef int8_t sint8;
typedef int16_t s16; typedef int16_t sint16;
typedef int32_t s32; typedef int32_t sint32;
typedef int64_t s64; typedef int64_t sint64;
typedef float f32; typedef float float32;
typedef double f64; typedef double float64;
typedef uintptr_t uptr; typedef uintptr_t upointer;
typedef intptr_t sptr; typedef intptr_t spointer;
typedef uptr ureg; typedef upointer uregister;
typedef sptr sreg; typedef spointer sregister;
#define auto __auto_type #define auto __auto_type
@ -32,8 +32,8 @@ typedef sptr sreg;
#include <Block.h> #include <Block.h>
#define capture __block // allows a variable to be mutably captured by a closure (will be moved to the heap if needed) #define capture __block // allows a variable to be mutably captured by a closure (will be moved to the heap if needed)
#define persist Block_copy // moves a closure to the heap (reference counted) so it can persist outside of its local stack frame #define persist Block_copy // moves a closure to the heap (reference counted) so it can persist outside of its local stack frame
#define unpersist Block_release // frees a closure (or decrements its reference count) #define unpersist Block_release // frees a closure (or decrements its reference count)
typedef closure(void, void) __scope_exit; typedef closure(void, void) __scope_exit;

View file

@ -59,7 +59,7 @@ init(void) {
}); });
printf("x = %d (before)\n", x); printf("x = %d (before)\n", x);
for (ureg i = 0; i < 10; i += 1) { for (uregister i = 0; i < 10; i += 1) {
inc(); inc();
} }
printf("x = %d (after)\n", x); printf("x = %d (after)\n", x);
@ -67,19 +67,19 @@ init(void) {
auto arena = Logger(Linear(1024 * 1024)); auto arena = Logger(Linear(1024 * 1024));
scope_exit { arena_release(arena); }; scope_exit { arena_release(arena); };
sreg* a = arena_new(arena, sreg); sregister* a = arena_new(arena, sregister);
sreg* b = arena_new(arena, sreg); sregister* b = arena_new(arena, sregister);
{ {
auto region = Region(arena); auto region = Region(arena);
scope_exit { arena_reset(region); }; scope_exit { arena_reset(region); };
arena_make(region, 10, sreg); arena_make(region, 10, sregister);
arena_make(region, 10, f32); arena_make(region, 10, float32);
arena_make(region, 10, bool); arena_make(region, 10, bool);
} }
sreg* c = arena_new(arena, sreg); sregister* c = arena_new(arena, sregister);
*a = 1024; *a = 1024;
*b = 2048; *b = 2048;
@ -91,9 +91,9 @@ init(void) {
arena_reset(arena); arena_reset(arena);
c = arena_new(arena, sreg); c = arena_new(arena, sregister);
b = arena_new(arena, sreg); b = arena_new(arena, sregister);
a = arena_new(arena, sreg); a = arena_new(arena, sregister);
printf("%zu (%p)\n", *a, a); printf("%zu (%p)\n", *a, a);
printf("%zu (%p)\n", *b, b); printf("%zu (%p)\n", *b, b);