diff options
-rw-r--r-- | .vscode/launch.json | 12 | ||||
-rw-r--r-- | src/memory_allocator.zig | 34 |
2 files changed, 25 insertions, 21 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json index ada02f41a..d478eff36 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -133,6 +133,18 @@ "BUN_DEBUG_QUIET_LOGS": "1" } }, + { + "type": "lldb", + "request": "launch", + "name": "bun install", + "program": "bun-debug", + "args": ["install"], + "cwd": "${workspaceFolder}", + "console": "internalConsole", + "env": { + "BUN_DEBUG_QUIET_LOGS": "1" + } + }, { "type": "lldb", diff --git a/src/memory_allocator.zig b/src/memory_allocator.zig index 273b2aacf..ad207c7cf 100644 --- a/src/memory_allocator.zig +++ b/src/memory_allocator.zig @@ -71,7 +71,7 @@ const CAllocator = struct { } } - return if (ptr) |p| @ptrCast([*]u8, p) else null; + return @ptrCast(?[*]u8, ptr); } fn alignedAllocSize(ptr: [*]u8) usize { @@ -122,12 +122,19 @@ const ZAllocator = struct { fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 { if (comptime FeatureFlags.log_allocations) std.debug.print("Malloc: {d}\n", .{len}); - var ptr = if (mi_malloc_satisfies_alignment(alignment, len)) - mimalloc.mi_zalloc(len) + var ptr = if (mimalloc.canUseAlignedAlloc(len, alignment)) + mimalloc.mi_zalloc_aligned(len, alignment) else - mimalloc.mi_zalloc_aligned(len, alignment); + mimalloc.mi_zalloc(len); - return @ptrCast([*]u8, ptr orelse null); + if (comptime Environment.allow_assert) { + const usable = mimalloc.mi_malloc_usable_size(ptr); + if (usable < len) { + std.debug.panic("mimalloc: allocated size is too small: {d} < {d}", .{ usable, len }); + } + } + + return @ptrCast(?[*]u8, ptr); } fn alignedAllocSize(ptr: [*]u8) usize { @@ -151,22 +158,7 @@ const ZAllocator = struct { return false; } - fn free( - _: *anyopaque, - buf: []u8, - buf_align: u8, - _: usize, - ) void { - // mi_free_size internally just asserts the size - // so it's faster if we don't pass that value through - // but its good to have that assertion - if (comptime Environment.allow_assert) { - assert(mimalloc.mi_is_in_heap_region(buf.ptr)); - mimalloc.mi_free_size_aligned(buf.ptr, buf.len, buf_align); - } else { - mimalloc.mi_free(buf.ptr); - } - } + const free = mimalloc_free; }; pub const z_allocator = Allocator{ |