mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.heap.ThreadSafeAllocator: update to new Allocator API
This commit is contained in:
parent
36e9b0f026
commit
1a6d87d699
2 changed files with 17 additions and 6 deletions
|
|
@ -1061,6 +1061,7 @@ test {
|
|||
_ = ArenaAllocator;
|
||||
_ = GeneralPurposeAllocator;
|
||||
_ = FixedBufferAllocator;
|
||||
_ = ThreadSafeAllocator;
|
||||
if (builtin.target.isWasm()) {
|
||||
_ = WasmAllocator;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,35 +9,45 @@ pub fn allocator(self: *ThreadSafeAllocator) Allocator {
|
|||
.vtable = &.{
|
||||
.alloc = alloc,
|
||||
.resize = resize,
|
||||
.remap = remap,
|
||||
.free = free,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn alloc(ctx: *anyopaque, n: usize, log2_ptr_align: u8, ra: usize) ?[*]u8 {
|
||||
fn alloc(ctx: *anyopaque, n: usize, alignment: std.mem.Alignment, ra: usize) ?[*]u8 {
|
||||
const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
return self.child_allocator.rawAlloc(n, log2_ptr_align, ra);
|
||||
return self.child_allocator.rawAlloc(n, alignment, ra);
|
||||
}
|
||||
|
||||
fn resize(ctx: *anyopaque, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
|
||||
fn resize(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
|
||||
const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
|
||||
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
return self.child_allocator.rawResize(buf, log2_buf_align, new_len, ret_addr);
|
||||
return self.child_allocator.rawResize(buf, alignment, new_len, ret_addr);
|
||||
}
|
||||
|
||||
fn free(ctx: *anyopaque, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
|
||||
fn remap(context: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, return_address: usize) ?[*]u8 {
|
||||
const self: *ThreadSafeAllocator = @ptrCast(@alignCast(context));
|
||||
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
return self.child_allocator.rawRemap(memory, alignment, new_len, return_address);
|
||||
}
|
||||
|
||||
fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
||||
const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
|
||||
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
return self.child_allocator.rawFree(buf, log2_buf_align, ret_addr);
|
||||
return self.child_allocator.rawFree(buf, alignment, ret_addr);
|
||||
}
|
||||
|
||||
const std = @import("../std.zig");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue