Files
bun.sh/src/allocators/MaxHeapAllocator.zig
taylor.fish 712d5be741 Add safety checks to MultiArrayList and BabyList (#21063)
Ensure we aren't using multiple allocators with the same list by storing
a pointer to the allocator in debug mode only.

This check is stricter than the bare minimum necessary to prevent
illegal behavior, so CI may reveal certain uses that fail the checks but
don't cause IB. Most of these cases should probably be updated to comply
with the new requirements—we want these types' invariants to be clear.

(For internal tracking: fixes ENG-14987)
2025-07-25 18:12:21 -07:00

59 lines
1.4 KiB
Zig

//! Single allocation only.
const Self = @This();
array_list: std.ArrayListAligned(u8, @alignOf(std.c.max_align_t)),
fn alloc(ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 {
bun.assert(alignment.toByteUnits() <= @alignOf(std.c.max_align_t));
var self = bun.cast(*Self, ptr);
self.array_list.items.len = 0;
self.array_list.ensureTotalCapacity(len) catch return null;
self.array_list.items.len = len;
return self.array_list.items.ptr;
}
fn resize(_: *anyopaque, buf: []u8, _: std.mem.Alignment, new_len: usize, _: usize) bool {
_ = new_len;
_ = buf;
@panic("not implemented");
}
fn free(
_: *anyopaque,
_: []u8,
_: std.mem.Alignment,
_: usize,
) void {}
pub fn reset(self: *Self) void {
self.array_list.items.len = 0;
}
pub fn deinit(self: *Self) void {
self.array_list.deinit();
}
const vtable = std.mem.Allocator.VTable{
.alloc = &alloc,
.free = &free,
.resize = &resize,
.remap = &std.mem.Allocator.noRemap,
};
pub fn init(self: *Self, allocator: std.mem.Allocator) std.mem.Allocator {
self.array_list = .init(allocator);
return std.mem.Allocator{
.ptr = self,
.vtable = &vtable,
};
}
pub fn isInstance(allocator: std.mem.Allocator) bool {
return allocator.vtable == &vtable;
}
const bun = @import("bun");
const std = @import("std");