| ... | ... | @@ -1,62 +0,0 @@ |
| 1 | | //! Deprecated. Thread safety should be built into each Allocator instance |
| 2 | | //! directly rather than trying to do this "composable allocators" thing. |
| 3 | | const ThreadSafeAllocator = @This(); |
| 4 | | |
| 5 | | const std = @import("../std.zig"); |
| 6 | | const Io = std.Io; |
| 7 | | const Allocator = std.mem.Allocator; |
| 8 | | |
| 9 | | child_allocator: Allocator, |
| 10 | | io: Io, |
| 11 | | mutex: Io.Mutex = .init, |
| 12 | | |
| 13 | | pub fn allocator(self: *ThreadSafeAllocator) Allocator { |
| 14 | | return .{ |
| 15 | | .ptr = self, |
| 16 | | .vtable = &.{ |
| 17 | | .alloc = alloc, |
| 18 | | .resize = resize, |
| 19 | | .remap = remap, |
| 20 | | .free = free, |
| 21 | | }, |
| 22 | | }; |
| 23 | | } |
| 24 | | |
| 25 | | fn alloc(ctx: *anyopaque, n: usize, alignment: std.mem.Alignment, ra: usize) ?[*]u8 { |
| 26 | | const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx)); |
| 27 | | const io = self.io; |
| 28 | | self.mutex.lockUncancelable(io); |
| 29 | | defer self.mutex.unlock(io); |
| 30 | | |
| 31 | | return self.child_allocator.rawAlloc(n, alignment, ra); |
| 32 | | } |
| 33 | | |
| 34 | | fn resize(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool { |
| 35 | | const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx)); |
| 36 | | const io = self.io; |
| 37 | | |
| 38 | | self.mutex.lockUncancelable(io); |
| 39 | | defer self.mutex.unlock(io); |
| 40 | | |
| 41 | | return self.child_allocator.rawResize(buf, alignment, new_len, ret_addr); |
| 42 | | } |
| 43 | | |
| 44 | | fn remap(context: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, return_address: usize) ?[*]u8 { |
| 45 | | const self: *ThreadSafeAllocator = @ptrCast(@alignCast(context)); |
| 46 | | const io = self.io; |
| 47 | | |
| 48 | | self.mutex.lockUncancelable(io); |
| 49 | | defer self.mutex.unlock(io); |
| 50 | | |
| 51 | | return self.child_allocator.rawRemap(memory, alignment, new_len, return_address); |
| 52 | | } |
| 53 | | |
| 54 | | fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void { |
| 55 | | const self: *ThreadSafeAllocator = @ptrCast(@alignCast(ctx)); |
| 56 | | const io = self.io; |
| 57 | | |
| 58 | | self.mutex.lockUncancelable(io); |
| 59 | | defer self.mutex.unlock(io); |
| 60 | | |
| 61 | | return self.child_allocator.rawFree(buf, alignment, ret_addr); |
| 62 | | } |