diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/zisp/gc/meta_alloc.zig | 54 |
1 files changed, 19 insertions, 35 deletions
diff --git a/src/zisp/gc/meta_alloc.zig b/src/zisp/gc/meta_alloc.zig index eb29028..8937a81 100644 --- a/src/zisp/gc/meta_alloc.zig +++ b/src/zisp/gc/meta_alloc.zig @@ -163,14 +163,14 @@ fn mmap(size: usize) []align(4096) u8 { } /// Must call this once to initialize the slabs. -pub fn init() void { +export fn init() void { slabs = @ptrCast(mmap(SIZES.len * SLAB_LEN * 8)); } /// Allocate a slot of the given size, which must be a power of two and greater /// than or equal to the smallest size class. If it's greater than the largest /// size class, this will fall back to an mmap() syscall. -pub fn alloc(size: usize) []u8 { +export fn alloc(size: usize) [*]u8 { // Can't allocate size that isn't power of two. std.debug.assert(@popCount(size) == 1); @@ -181,10 +181,23 @@ pub fn alloc(size: usize) []u8 { if (sci < SIZES.len) { @branchHint(.likely); const slot_ptr = alloc_size_class(sci, size); - const ptr: [*]u8 = @ptrCast(slot_ptr); - return ptr[0..size]; + return @ptrCast(slot_ptr); } else { - return mmap(size); + return mmap(size).ptr; + } +} + +/// Free memory that was returned by alloc(). +export fn free(size: usize, ptr: [*]u8) void { + std.debug.assert(@popCount(size) == 1); + std.debug.assert(size >= SIZES[0]); + + const sci = getSizeClassIndex(size); + if (sci < SIZES.len) { + @branchHint(.likely); + free_size_class(sci, @ptrCast(@alignCast(ptr))); + } else { + std.posix.munmap(@alignCast(ptr[0..size])); } } @@ -285,29 +298,12 @@ fn alloc_fresh( return slab[old_wm..]; } -/// Free memory that was returned by alloc(). -pub fn free(slot: []u8) void { - std.debug.assert(@popCount(slot.len) == 1); - std.debug.assert(slot.len >= SIZES[0]); - - const sci = getSizeClassIndex(slot.len); - if (sci < SIZES.len) { - @branchHint(.likely); - free_size_class(sci, slot); - } else { - std.posix.munmap(@alignCast(slot)); - } -} - -fn free_size_class(sci: u8, slot: []u8) void { +fn free_size_class(sci: u8, ptr: [*]u64) void { std.debug.assert(sci < SIZES.len); const tl = &tl_slab_infos[sci]; const slab: [*]u64 = &slabs[sci]; - - const ptr: [*]u64 = @ptrCast(@alignCast(slot.ptr)); const idx: u32 = @intCast(ptr - slab); - const res_n = reserveCountForSizeClassIndex(sci); // Still some space in the local free slot cache. @@ -357,15 +353,3 @@ fn free_into_fl( ) orelse break; } } - -export fn alloc_(size: usize) [*]u8 { - return alloc(size).ptr; -} - -export fn free_(ptr: [*]u8, len: usize) void { - return free(ptr[0..len]); -} - -export fn init_() void { - init(); -} |
