mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 22:04:21 +00:00
std.heap.WasmAllocator: update to new Allocator API
This commit is contained in:
parent
82b5a1d313
commit
00b723dc9d
1 changed files with 17 additions and 7 deletions
|
|
@ -20,6 +20,7 @@ comptime {
|
||||||
pub const vtable: Allocator.VTable = .{
|
pub const vtable: Allocator.VTable = .{
|
||||||
.alloc = alloc,
|
.alloc = alloc,
|
||||||
.resize = resize,
|
.resize = resize,
|
||||||
|
.remap = remap,
|
||||||
.free = free,
|
.free = free,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -46,12 +47,11 @@ var frees: [size_class_count]usize = @splat(0);
|
||||||
/// For each big size class, points to the freed pointer.
|
/// For each big size class, points to the freed pointer.
|
||||||
var big_frees: [big_size_class_count]usize = @splat(0);
|
var big_frees: [big_size_class_count]usize = @splat(0);
|
||||||
|
|
||||||
fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*]u8 {
|
fn alloc(ctx: *anyopaque, len: usize, alignment: mem.Alignment, return_address: usize) ?[*]u8 {
|
||||||
_ = ctx;
|
_ = ctx;
|
||||||
_ = return_address;
|
_ = return_address;
|
||||||
// Make room for the freelist next pointer.
|
// Make room for the freelist next pointer.
|
||||||
const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align));
|
const actual_len = @max(len +| @sizeOf(usize), alignment.toByteUnits());
|
||||||
const actual_len = @max(len +| @sizeOf(usize), alignment);
|
|
||||||
const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
|
const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
|
||||||
const class = math.log2(slot_size) - min_class;
|
const class = math.log2(slot_size) - min_class;
|
||||||
if (class < size_class_count) {
|
if (class < size_class_count) {
|
||||||
|
|
@ -86,7 +86,7 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*
|
||||||
fn resize(
|
fn resize(
|
||||||
ctx: *anyopaque,
|
ctx: *anyopaque,
|
||||||
buf: []u8,
|
buf: []u8,
|
||||||
log2_buf_align: u8,
|
alignment: mem.Alignment,
|
||||||
new_len: usize,
|
new_len: usize,
|
||||||
return_address: usize,
|
return_address: usize,
|
||||||
) bool {
|
) bool {
|
||||||
|
|
@ -94,7 +94,7 @@ fn resize(
|
||||||
_ = return_address;
|
_ = return_address;
|
||||||
// We don't want to move anything from one size class to another, but we
|
// We don't want to move anything from one size class to another, but we
|
||||||
// can recover bytes in between powers of two.
|
// can recover bytes in between powers of two.
|
||||||
const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
|
const buf_align = alignment.toByteUnits();
|
||||||
const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
|
const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
|
||||||
const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
|
const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
|
||||||
const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
|
const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
|
||||||
|
|
@ -111,15 +111,25 @@ fn resize(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn remap(
|
||||||
|
context: *anyopaque,
|
||||||
|
memory: []u8,
|
||||||
|
alignment: mem.Alignment,
|
||||||
|
new_len: usize,
|
||||||
|
return_address: usize,
|
||||||
|
) ?[*]u8 {
|
||||||
|
return if (resize(context, memory, alignment, new_len, return_address)) memory.ptr else null;
|
||||||
|
}
|
||||||
|
|
||||||
fn free(
|
fn free(
|
||||||
ctx: *anyopaque,
|
ctx: *anyopaque,
|
||||||
buf: []u8,
|
buf: []u8,
|
||||||
log2_buf_align: u8,
|
alignment: mem.Alignment,
|
||||||
return_address: usize,
|
return_address: usize,
|
||||||
) void {
|
) void {
|
||||||
_ = ctx;
|
_ = ctx;
|
||||||
_ = return_address;
|
_ = return_address;
|
||||||
const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
|
const buf_align = alignment.toByteUnits();
|
||||||
const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
|
const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
|
||||||
const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
|
const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
|
||||||
const class = math.log2(slot_size) - min_class;
|
const class = math.log2(slot_size) - min_class;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue