| ... | ... | @@ -124,9 +124,9 @@ pub const Allocator = struct { |
| 124 | 124 | |
| 125 | 125 | fn AllocWithOptionsPayload(comptime Elem: type, comptime alignment: ?u29, comptime sentinel: ?Elem) type { |
| 126 | 126 | if (sentinel) |s| { |
| 127 | | return [:s]align(alignment orelse @alignOf(T)) Elem; |
| 127 | return [:s]align(alignment orelse @alignOf(Elem)) Elem; |
| 128 | 128 | } else { |
| 129 | | return []align(alignment orelse @alignOf(T)) Elem; |
| 129 | return []align(alignment orelse @alignOf(Elem)) Elem; |
| 130 | 130 | } |
| 131 | 131 | } |
| 132 | 132 | |
| ... | ... | @@ -281,6 +281,22 @@ pub const Allocator = struct { |
| 281 | 281 | } |
| 282 | 282 | }; |
| 283 | 283 | |
| 284 | var failAllocator = Allocator { |
| 285 | .reallocFn = failAllocatorRealloc, |
| 286 | .shrinkFn = failAllocatorShrink, |
| 287 | }; |
| 288 | fn failAllocatorRealloc(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { |
| 289 | return error.OutOfMemory; |
| 290 | } |
| 291 | fn failAllocatorShrink(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { |
| 292 | @panic("failAllocatorShrink should never be called because it cannot allocate"); |
| 293 | } |
| 294 | |
| 295 | test "mem.Allocator basics" { |
| 296 | testing.expectError(error.OutOfMemory, failAllocator.alloc(u8, 1)); |
| 297 | testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0)); |
| 298 | } |
| 299 | |
| 284 | 300 | /// Copy all of source into dest at position 0. |
| 285 | 301 | /// dest.len must be >= source.len. |
| 286 | 302 | /// dest.ptr must be <= src.ptr. |