| ... | @@ -480,6 +480,10 @@ pub const FixedBufferAllocator = struct { | ... | @@ -480,6 +480,10 @@ pub const FixedBufferAllocator = struct { |
| 480 | fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { | 480 | fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { |
| 481 | return old_mem[0..new_size]; | 481 | return old_mem[0..new_size]; |
| 482 | } | 482 | } |
| | 483 | |
| | 484 | pub fn reset(self: *FixedBufferAllocator) void { |
| | 485 | self.end_index = 0; |
| | 486 | } |
| 483 | }; | 487 | }; |
| 484 | | 488 | |
| 485 | // FIXME: Exposed LLVM intrinsics is a bug | 489 | // FIXME: Exposed LLVM intrinsics is a bug |
| ... | @@ -775,6 +779,26 @@ test "FixedBufferAllocator" { | ... | @@ -775,6 +779,26 @@ test "FixedBufferAllocator" { |
| 775 | try testAllocatorAlignedShrink(&fixed_buffer_allocator.allocator); | 779 | try testAllocatorAlignedShrink(&fixed_buffer_allocator.allocator); |
| 776 | } | 780 | } |
| 777 | | 781 | |
| | 782 | test "FixedBufferAllocator.reset" { |
| | 783 | var buf: [8]u8 align(@alignOf(usize)) = undefined; |
| | 784 | var fba = FixedBufferAllocator.init(buf[0..]); |
| | 785 | |
| | 786 | const X = 0xeeeeeeeeeeeeeeee; |
| | 787 | const Y = 0xffffffffffffffff; |
| | 788 | |
| | 789 | var x = try fba.allocator.create(u64); |
| | 790 | x.* = X; |
| | 791 | testing.expectError(error.OutOfMemory, fba.allocator.create(u64)); |
| | 792 | |
| | 793 | fba.reset(); |
| | 794 | var y = try fba.allocator.create(u64); |
| | 795 | y.* = Y; |
| | 796 | |
| | 797 | // we expect Y to have overwritten X. |
| | 798 | testing.expect(x.* == y.*); |
| | 799 | testing.expect(y.* == Y); |
| | 800 | } |
| | 801 | |
| 778 | test "FixedBufferAllocator Reuse memory on realloc" { | 802 | test "FixedBufferAllocator Reuse memory on realloc" { |
| 779 | var small_fixed_buffer: [10]u8 = undefined; | 803 | var small_fixed_buffer: [10]u8 = undefined; |
| 780 | // check if we re-use the memory | 804 | // check if we re-use the memory |