authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-04 23:01:24-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-06 14:23:23-08:00
log1a6d87d699a9e0867f3a50566dc72b3e2347d8d1
tree0d063c5234a3ee442cdccecf6e9dd3b682c3ba0f
parent36e9b0f0261a5421f22dbf17f513a8c2f685c5d3

std.heap.ThreadSafeAllocator: update to new Allocator API


2 files changed, 17 insertions(+), 6 deletions(-)

lib/std/heap.zig+1
......@@ -1061,6 +1061,7 @@ test {
10611061 _ = ArenaAllocator;
10621062 _ = GeneralPurposeAllocator;
10631063 _ = FixedBufferAllocator;
1064 _ = ThreadSafeAllocator;
10641065 if (builtin.target.isWasm()) {
10651066 _ = WasmAllocator;
10661067 }
lib/std/heap/ThreadSafeAllocator.zig+16-6
......@@ -9,35 +9,45 @@ pub fn allocator(self: *ThreadSafeAllocator) Allocator {
99 .vtable = &.{
1010 .alloc = alloc,
1111 .resize = resize,
12 .remap = remap,
1213 .free = free,
1314 },
1415 };
1516}
1617
17fn alloc(ctx: *anyopaque, n: usize, log2_ptr_align: u8, ra: usize) ?[*]u8 {
18fn alloc(ctx: *anyopaque, n: usize, alignment: std.mem.Alignment, ra: usize) ?[*]u8 {
1819 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
1920 self.mutex.lock();
2021 defer self.mutex.unlock();
2122
22 return self.child_allocator.rawAlloc(n, log2_ptr_align, ra);
23 return self.child_allocator.rawAlloc(n, alignment, ra);
2324}
2425
25fn resize(ctx: *anyopaque, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
26fn resize(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
2627 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
2728
2829 self.mutex.lock();
2930 defer self.mutex.unlock();
3031
31 return self.child_allocator.rawResize(buf, log2_buf_align, new_len, ret_addr);
32 return self.child_allocator.rawResize(buf, alignment, new_len, ret_addr);
3233}
3334
34fn free(ctx: *anyopaque, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
35fn remap(context: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, return_address: usize) ?[*]u8 {
36 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(context));
37
38 self.mutex.lock();
39 defer self.mutex.unlock();
40
41 return self.child_allocator.rawRemap(memory, alignment, new_len, return_address);
42}
43
44fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
3545 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
3646
3747 self.mutex.lock();
3848 defer self.mutex.unlock();
3949
40 return self.child_allocator.rawFree(buf, log2_buf_align, ret_addr);
50 return self.child_allocator.rawFree(buf, alignment, ret_addr);
4151}
4252
4353const std = @import("../std.zig");