...
This commit is contained in:
@@ -4,10 +4,21 @@
|
||||
|
||||
Allocator :: struct {
|
||||
ctx: *void;
|
||||
alloc: (*void, s64) -> *void;
|
||||
free: (*void, *void) -> void;
|
||||
alloc_fn: (*void, s64) -> *void;
|
||||
free_fn: (*void, *void) -> void;
|
||||
}
|
||||
|
||||
allocator_alloc :: (a: Allocator, size: s64) -> *void {
|
||||
a.alloc_fn(a.ctx, size);
|
||||
}
|
||||
|
||||
allocator_dealloc :: (a: Allocator, ptr: *void) {
|
||||
a.free_fn(a.ctx, ptr);
|
||||
}
|
||||
|
||||
alloc :: ufcs allocator_alloc;
|
||||
dealloc :: ufcs allocator_dealloc;
|
||||
|
||||
// --- GPA: general purpose allocator (malloc/free wrapper) ---
|
||||
|
||||
GPA :: struct {
|
||||
@@ -27,7 +38,7 @@ gpa_free :: (ctx: *void, ptr: *void) {
|
||||
}
|
||||
|
||||
gpa_create :: (gpa: *GPA) -> Allocator {
|
||||
Allocator.{ ctx = gpa, alloc = gpa_alloc, free = gpa_free };
|
||||
Allocator.{ ctx = gpa, alloc_fn = gpa_alloc, free_fn = gpa_free };
|
||||
}
|
||||
|
||||
// --- Arena: multi-chunk bump allocator (Zig-inspired) ---
|
||||
@@ -47,7 +58,7 @@ arena_add_chunk :: (a: *Arena, min_size: s64) {
|
||||
prev_cap := if a.first != null then a.first.cap else 0;
|
||||
needed := min_size + 16 + 16;
|
||||
len := (prev_cap + needed) * 3 / 2;
|
||||
raw := a.parent.alloc(a.parent.ctx, len);
|
||||
raw := a.parent.alloc(len);
|
||||
chunk : *ArenaChunk = xx raw;
|
||||
chunk.next = a.first;
|
||||
chunk.cap = len;
|
||||
@@ -82,7 +93,7 @@ arena_create :: (a: *Arena, parent: Allocator, size: s64) -> Allocator {
|
||||
a.end_index = 0;
|
||||
a.parent = parent;
|
||||
arena_add_chunk(a, size);
|
||||
Allocator.{ ctx = a, alloc = arena_alloc, free = arena_free };
|
||||
Allocator.{ ctx = a, alloc_fn = arena_alloc, free_fn = arena_free };
|
||||
}
|
||||
|
||||
arena_reset :: (a: *Arena) {
|
||||
@@ -91,7 +102,7 @@ arena_reset :: (a: *Arena) {
|
||||
it := a.first.next;
|
||||
while it != null {
|
||||
next := it.next;
|
||||
a.parent.free(a.parent.ctx, it);
|
||||
a.parent.dealloc(it);
|
||||
it = next;
|
||||
}
|
||||
a.first.next = null;
|
||||
@@ -103,7 +114,7 @@ arena_deinit :: (a: *Arena) {
|
||||
it := a.first;
|
||||
while it != null {
|
||||
next := it.next;
|
||||
a.parent.free(a.parent.ctx, it);
|
||||
a.parent.dealloc(it);
|
||||
it = next;
|
||||
}
|
||||
a.first = null;
|
||||
@@ -136,7 +147,7 @@ buf_create :: (b: *BufAlloc, buf: [*]u8, len: s64) -> Allocator {
|
||||
b.buf = buf;
|
||||
b.len = len;
|
||||
b.pos = 0;
|
||||
Allocator.{ ctx = b, alloc = buf_alloc, free = buf_free };
|
||||
Allocator.{ ctx = b, alloc_fn = buf_alloc, free_fn = buf_free };
|
||||
}
|
||||
|
||||
buf_reset :: (b: *BufAlloc) {
|
||||
|
||||
@@ -31,8 +31,12 @@ context : Context = ---;
|
||||
|
||||
// --- Slice & string allocation ---
|
||||
|
||||
context_alloc :: (size: s64) -> *void {
|
||||
if context.allocator.ctx != null then context.allocator.alloc(size) else malloc(size);
|
||||
}
|
||||
|
||||
cstring :: (size: s64) -> string {
|
||||
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
|
||||
raw := context_alloc(size + 1);
|
||||
memset(raw, 0, size + 1);
|
||||
s : string = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -41,7 +45,7 @@ cstring :: (size: s64) -> string {
|
||||
}
|
||||
|
||||
alloc_slice :: ($T: Type, count: s64) -> []T {
|
||||
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
|
||||
raw := context_alloc(count * size_of(T));
|
||||
memset(raw, 0, count * size_of(T));
|
||||
s : []T = ---;
|
||||
s.ptr = xx raw;
|
||||
|
||||
Reference in New Issue
Block a user