diff options
author | 2022-12-28 17:00:04 -0800 | |
---|---|---|
committer | 2022-12-28 17:00:04 -0800 | |
commit | 97ec866f8523b50f242f0873ca5e9796dce54ad2 (patch) | |
tree | 5fbd1c451951663b13056dddae1bd5fe4d9f5b66 /src | |
parent | ef3c9b7c6d6557f7cf50210f4a54187351d47ca8 (diff) | |
download | bun-97ec866f8523b50f242f0873ca5e9796dce54ad2.tar.gz bun-97ec866f8523b50f242f0873ca5e9796dce54ad2.tar.zst bun-97ec866f8523b50f242f0873ca5e9796dce54ad2.zip |
Fixes https://github.com/oven-sh/bun/issues/1677
Diffstat (limited to 'src')
-rw-r--r-- | src/memory_allocator.zig | 34 |
1 files changed, 13 insertions, 21 deletions
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{ |