authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-21 12:48:45-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-09-21 12:48:45-04:00
log533aff3a6a6a72ac09261c395c479570fe81d1d8
tree4edd5c70340b130ce5fd515509fb099f6d0e12f6
parent3377044ebeb562150b0c3127cdd6c1a07fbb0b74
parent86b6790b6a7fc7fb6b85d5e970259af60ddd0290
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3238 from Tetralux/fixed-allocator-reset

FixedBufferAllocator.reset

1 files changed, 24 insertions(+), 0 deletions(-)

std/heap.zig+24
......@@ -480,6 +480,10 @@ pub const FixedBufferAllocator = struct {
480480 fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
481481 return old_mem[0..new_size];
482482 }
483
484 pub fn reset(self: *FixedBufferAllocator) void {
485 self.end_index = 0;
486 }
483487};
484488
485489// FIXME: Exposed LLVM intrinsics is a bug
......@@ -775,6 +779,26 @@ test "FixedBufferAllocator" {
775779 try testAllocatorAlignedShrink(&fixed_buffer_allocator.allocator);
776780}
777781
782test "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
778802test "FixedBufferAllocator Reuse memory on realloc" {
779803 var small_fixed_buffer: [10]u8 = undefined;
780804 // check if we re-use the memory