mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
Co-authored-by: 190n <7763597+190n@users.noreply.github.com> Co-authored-by: Zack Radisic <56137411+zackradisic@users.noreply.github.com> Co-authored-by: pfg <pfg@pfg.pw> Co-authored-by: pfgithub <6010774+pfgithub@users.noreply.github.com> Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
54 lines
1.5 KiB
Zig
54 lines
1.5 KiB
Zig
const bun = @import("root").bun;
|
|
const std = @import("std");
|
|
|
|
/// Single allocation only.
|
|
///
|
|
pub const MaxHeapAllocator = struct {
|
|
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 this = bun.cast(*MaxHeapAllocator, ptr);
|
|
this.array_list.items.len = 0;
|
|
this.array_list.ensureTotalCapacity(len) catch return null;
|
|
this.array_list.items.len = len;
|
|
return this.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(this: *MaxHeapAllocator) void {
|
|
this.array_list.items.len = 0;
|
|
}
|
|
|
|
pub fn deinit(this: *MaxHeapAllocator) void {
|
|
this.array_list.deinit();
|
|
}
|
|
|
|
const vtable = std.mem.Allocator.VTable{
|
|
.alloc = &alloc,
|
|
.free = &free,
|
|
.resize = &resize,
|
|
.remap = &std.mem.Allocator.noRemap,
|
|
};
|
|
pub fn init(this: *MaxHeapAllocator, allocator: std.mem.Allocator) std.mem.Allocator {
|
|
this.array_list = .init(allocator);
|
|
|
|
return std.mem.Allocator{
|
|
.ptr = this,
|
|
.vtable = &vtable,
|
|
};
|
|
}
|
|
};
|