use jc types

This commit is contained in:
Judah Caruso 2025-07-23 15:22:00 -06:00
parent 05995a6a06
commit 5cbcc10444
3 changed files with 23 additions and 17 deletions

View file

@ -4,7 +4,7 @@ make_crash_allocator :: () -> Allocator {
return .{ proc = crash_allocator_proc }; return .{ proc = crash_allocator_proc };
} }
crash_allocator_proc :: (mode: Allocator_Mode, size: s64, old_size: s64, old_memory: *void, allocator_data: *void) -> *void { crash_allocator_proc :: (mode: Allocator_Mode, size: s64, old_size: s64, old_memory: rawptr, allocator_data: rawptr) -> rawptr {
message: string; message: string;
if mode == { if mode == {
@ -24,12 +24,12 @@ crash_allocator_proc :: (mode: Allocator_Mode, size: s64, old_size: s64, old_mem
Arena :: struct { Arena :: struct {
memory: *void; memory: rawptr;
memory_size: u64; memory_size: u64;
offset: u64; offset: u64;
} }
init_arena :: (a: *Arena, memory: *void, size: u64) { init_arena :: (a: *Arena, memory: rawptr, size: u64) {
a.memory = memory; a.memory = memory;
a.memory_size = size; a.memory_size = size;
a.offset = 0; a.offset = 0;
@ -56,7 +56,7 @@ Extended_Allocator_Mode :: enum {
restore_save_point; // 'old_size' will be the dereferenced return value of 'save_point' restore_save_point; // 'old_size' will be the dereferenced return value of 'save_point'
} }
arena_allocator_proc :: (mode: Extended_Allocator_Mode, size: s64, old_size: s64, old_memory: *void, allocator_data: *void) -> *void { arena_allocator_proc :: (mode: Extended_Allocator_Mode, size: s64, old_size: s64, old_memory: rawptr, allocator_data: rawptr) -> rawptr {
arena := allocator_data.(*Arena); arena := allocator_data.(*Arena);
if mode == { if mode == {
case .request_memory; case .request_memory;
@ -89,7 +89,7 @@ arena_allocator_proc :: (mode: Extended_Allocator_Mode, size: s64, old_size: s64
return null; return null;
} }
arena_alloc :: (a: *Arena, count: int, alignment := mem.Default_Align, loc := #caller_location) -> *void { arena_alloc :: (a: *Arena, count: int, alignment := mem.Default_Align, loc := #caller_location) -> rawptr {
basic.assert(a.memory != null, "arena: not initialized", loc = loc); basic.assert(a.memory != null, "arena: not initialized", loc = loc);
basic.assert(mem.power_of_two(alignment)); basic.assert(mem.power_of_two(alignment));
@ -100,12 +100,14 @@ arena_alloc :: (a: *Arena, count: int, alignment := mem.Default_Align, loc := #c
basic.assert(a.offset + total_size <= a.memory_size, "arena: out of memory", loc = loc); basic.assert(a.offset + total_size <= a.memory_size, "arena: out of memory", loc = loc);
a.offset += total_size; a.offset += total_size;
return ptr.(*void); return ptr.(rawptr);
} }
#scope_file; #scope_file;
#import "jc";
mem :: #import "jc/memory"; mem :: #import "jc/memory";
meta :: #import "jc/meta"; meta :: #import "jc/meta";

View file

@ -3,19 +3,19 @@
Buffer :: struct { Buffer :: struct {
allocator: Allocator; allocator: Allocator;
data: [..]u8; data: [..]byte;
count: int; count: int;
} }
append :: inline (buf: *Buffer, ptr: *void, count: int) { append :: inline (buf: *Buffer, ptr: rawptr, size: int) {
inline mem.lazy_set_allocator(buf); inline mem.lazy_set_allocator(buf);
free_space := ensure_buffer_has_room(buf, count); free_space := ensure_buffer_has_room(buf, size);
memcpy(free_space, ptr, count); memcpy(free_space, ptr, size);
buf.count += count; buf.size += size;
} }
append :: inline (buf: *Buffer, data: []u8) { append :: inline (buf: *Buffer, data: []byte) {
append(buf, data.data, data.count); append(buf, data.data, data.count);
} }
@ -29,6 +29,8 @@ reset :: inline (buf: *Buffer) {
#scope_file; #scope_file;
#import "jc";
array :: #import "jc/array"; array :: #import "jc/array";
mem :: #import "jc/memory"; mem :: #import "jc/memory";
meta :: #import "jc/meta"; meta :: #import "jc/meta";

View file

@ -93,7 +93,7 @@ allocator_restore :: (save_point: int) {
allocator.proc(xx Extended_Allocator_Mode.restore_save_point, 0, save_point, null, allocator.data); allocator.proc(xx Extended_Allocator_Mode.restore_save_point, 0, save_point, null, allocator.data);
} }
request_memory :: (size: int, align := Default_Align) -> *void { request_memory :: (size: int, align := Default_Align) -> rawptr {
allocator := context.allocator; allocator := context.allocator;
aligned_size := align_to(size, align); aligned_size := align_to(size, align);
return allocator.proc(xx Extended_Allocator_Mode.request_memory, aligned_size.(int), 0, null, allocator.data); return allocator.proc(xx Extended_Allocator_Mode.request_memory, aligned_size.(int), 0, null, allocator.data);
@ -132,21 +132,21 @@ request_memory :: ($T: Type, $init := true) -> *T
return ptr; return ptr;
} }
release_memory :: inline (ptr: *void) { release_memory :: inline (ptr: rawptr) {
allocator := context.allocator; allocator := context.allocator;
allocator.proc(xx Extended_Allocator_Mode.release_memory, 0, 0, ptr, allocator.data); allocator.proc(xx Extended_Allocator_Mode.release_memory, 0, 0, ptr, allocator.data);
} }
release_memory :: inline (arr: []$T) { release_memory :: inline (arr: []$T) {
release_memory(arr.data.(*void)); release_memory(arr.data.(*rawptr));
} }
release_memory :: inline (arr: [..]$T) { release_memory :: inline (arr: [..]$T) {
release_memory(arr.data.(*void),, allocator = arr.allocator); release_memory(arr.data.(*rawptr),, allocator = arr.allocator);
} }
release_memory :: inline (str: string) { release_memory :: inline (str: string) {
release_memory(str.data.(*void)); release_memory(str.data.(*rawptr));
} }
// @todo(judah): why can't we use $T/struct{ allocator: Allocator }? // @todo(judah): why can't we use $T/struct{ allocator: Allocator }?
@ -181,6 +181,8 @@ lazy_set_allocator :: (array: *[..]$T, allocator := context.allocator) #expand {
#scope_file; #scope_file;
#import "jc";
meta :: #import "jc/meta"; meta :: #import "jc/meta";
basic :: #import "Basic"; // @future basic :: #import "Basic"; // @future