mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
* Define a generic allocator interface to enable static polymorphism for allocators (see `GenericAllocator` in `src/allocators.zig`). Note that `std.mem.Allocator` itself is considered a generic allocator. * Add utilities to `bun.allocators` for working with generic allocators. * Add a new namespace, `bun.memory`, with basic utilities for working with memory and objects (`create`, `destroy`, `initDefault`, `deinit`). * Add `bun.DefaultAllocator`, a zero-sized generic allocator type whose `allocator` method simply returns `bun.default_allocator`. * Implement the generic allocator interface in `AllocationScope` and `MimallocArena`. * Improve `bun.threading.GuardedValue` (now `bun.threading.Guarded`). * Improve `bun.safety.AllocPtr` (now `bun.safety.CheckedAllocator`). (For internal tracking: fixes STAB-1085, STAB-1086, STAB-1087, STAB-1088, STAB-1089, STAB-1090, STAB-1091)
49 lines
1.4 KiB
Zig
49 lines
1.4 KiB
Zig
//! A nullable allocator the same size as `std.mem.Allocator`.
|
|
|
|
const NullableAllocator = @This();
|
|
|
|
ptr: *anyopaque = undefined,
|
|
// Utilize the null pointer optimization on the vtable instead of
|
|
// the regular `ptr` because `ptr` may be undefined.
|
|
vtable: ?*const std.mem.Allocator.VTable = null,
|
|
|
|
pub inline fn init(allocator: ?std.mem.Allocator) NullableAllocator {
|
|
return if (allocator) |a| .{
|
|
.ptr = a.ptr,
|
|
.vtable = a.vtable,
|
|
} else .{};
|
|
}
|
|
|
|
pub inline fn isNull(this: NullableAllocator) bool {
|
|
return this.vtable == null;
|
|
}
|
|
|
|
pub inline fn isWTFAllocator(this: NullableAllocator) bool {
|
|
return bun.String.isWTFAllocator(this.get() orelse return false);
|
|
}
|
|
|
|
pub inline fn get(this: NullableAllocator) ?std.mem.Allocator {
|
|
return if (this.vtable) |vt| std.mem.Allocator{ .ptr = this.ptr, .vtable = vt } else null;
|
|
}
|
|
|
|
pub fn free(this: *const NullableAllocator, bytes: []const u8) void {
|
|
if (this.get()) |allocator| {
|
|
if (bun.String.isWTFAllocator(allocator)) {
|
|
// avoid calling `std.mem.Allocator.free` as it sets the memory to undefined
|
|
allocator.rawFree(@constCast(bytes), .@"1", 0);
|
|
return;
|
|
}
|
|
|
|
allocator.free(bytes);
|
|
}
|
|
}
|
|
|
|
comptime {
|
|
if (@sizeOf(NullableAllocator) != @sizeOf(std.mem.Allocator)) {
|
|
@compileError("Expected the sizes to be the same.");
|
|
}
|
|
}
|
|
|
|
const bun = @import("bun");
|
|
const std = @import("std");
|