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 {...@@ -1061,6 +1061,7 @@ test {
1061 _ = ArenaAllocator;1061 _ = ArenaAllocator;
1062 _ = GeneralPurposeAllocator;1062 _ = GeneralPurposeAllocator;
1063 _ = FixedBufferAllocator;1063 _ = FixedBufferAllocator;
1064 _ = ThreadSafeAllocator;
1064 if (builtin.target.isWasm()) {1065 if (builtin.target.isWasm()) {
1065 _ = WasmAllocator;1066 _ = WasmAllocator;
1066 }1067 }
lib/std/heap/ThreadSafeAllocator.zig+16-6
...@@ -9,35 +9,45 @@ pub fn allocator(self: *ThreadSafeAllocator) Allocator {...@@ -9,35 +9,45 @@ pub fn allocator(self: *ThreadSafeAllocator) Allocator {
9 .vtable = &.{9 .vtable = &.{
10 .alloc = alloc,10 .alloc = alloc,
11 .resize = resize,11 .resize = resize,
12 .remap = remap,
12 .free = free,13 .free = free,
13 },14 },
14 };15 };
15}16}
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 {
18 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));19 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
19 self.mutex.lock();20 self.mutex.lock();
20 defer self.mutex.unlock();21 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);
23}24}
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 {
26 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));27 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
2728
28 self.mutex.lock();29 self.mutex.lock();
29 defer self.mutex.unlock();30 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);
32}33}
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 {
35 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));45 const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx));
3646
37 self.mutex.lock();47 self.mutex.lock();
38 defer self.mutex.unlock();48 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);
41}51}
4252
43const std = @import("../std.zig");53const std = @import("../std.zig");