| ... | @@ -168,14 +168,35 @@ pub inline fn rawFree(a: Allocator, memory: []u8, alignment: Alignment, ret_addr | ... | @@ -168,14 +168,35 @@ pub inline fn rawFree(a: Allocator, memory: []u8, alignment: Alignment, ret_addr |
| 168 | /// Returns a pointer to undefined memory. | 168 | /// Returns a pointer to undefined memory. |
| 169 | /// Call `destroy` with the result to free the memory. | 169 | /// Call `destroy` with the result to free the memory. |
| 170 | pub fn create(a: Allocator, comptime T: type) Error!*T { | 170 | pub fn create(a: Allocator, comptime T: type) Error!*T { |
| | 171 | return a.createAdvancedWithRetAddr(T, null, @returnAddress()); |
| | 172 | } |
| | 173 | |
| | 174 | pub fn alignedCreate( |
| | 175 | self: Allocator, |
| | 176 | comptime T: type, |
| | 177 | /// null means naturally aligned |
| | 178 | comptime alignment: ?Alignment, |
| | 179 | ) Error!*align(if (alignment) |a| a.toByteUnits() else @alignOf(T)) T { |
| | 180 | return self.createAdvancedWithRetAddr(T, alignment, @returnAddress()); |
| | 181 | } |
| | 182 | |
| | 183 | pub inline fn createAdvancedWithRetAddr( |
| | 184 | self: Allocator, |
| | 185 | comptime T: type, |
| | 186 | /// null means naturally aligned |
| | 187 | comptime alignment: ?Alignment, |
| | 188 | return_address: usize, |
| | 189 | ) Error!*align(if (alignment) |a| a.toByteUnits() else @alignOf(T)) T { |
| 171 | if (@sizeOf(T) == 0) { | 190 | if (@sizeOf(T) == 0) { |
| 172 | const ptr = comptime std.mem.alignBackward(usize, math.maxInt(usize), @alignOf(T)); | 191 | const ptr = comptime std.mem.alignBackward(usize, math.maxInt(usize), @alignOf(T)); |
| 173 | return @ptrFromInt(ptr); | 192 | return @ptrFromInt(ptr); |
| 174 | } | 193 | } |
| 175 | const ptr: *T = @ptrCast(try a.allocBytesAligned(.of(T), @sizeOf(T), @returnAddress())); | 194 | const a: Alignment = alignment orelse comptime .of(T); |
| | 195 | const ptr: *align(a.toByteUnits()) T = @ptrCast(try self.allocBytesAligned(a, @sizeOf(T), return_address)); |
| 176 | return ptr; | 196 | return ptr; |
| 177 | } | 197 | } |
| 178 | | 198 | |
| | 199 | |
| 179 | /// `ptr` should be the return value of `create`, or otherwise | 200 | /// `ptr` should be the return value of `create`, or otherwise |
| 180 | /// have the same address and alignment property. | 201 | /// have the same address and alignment property. |
| 181 | pub fn destroy(self: Allocator, ptr: anytype) void { | 202 | pub fn destroy(self: Allocator, ptr: anytype) void { |