authorgravatar for tetralux@teknik.ioTetralux <tetralux@teknik.io> 2019-09-16 01:35:01+00:00
committergravatar for tetralux@teknik.ioTetralux <tetralux@teknik.io> 2019-09-16 02:40:14+00:00
log537b26034925bf7452db2ff4cd6b29de92d77471
tree004198adb257c426abbdb13ac85256e6d98b49ef
parent8223aca09bc93279a88d1439d6dfc437144ec578
signaturelock-open Commit is signed but in an unrecognized format.

Add FixedBufferAllocator.reset


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

std/heap.zig+24
...@@ -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 inline fn reset(self: *FixedBufferAllocator) void {
485 self.end_index = 0;
486 }
483};487};
484488
485// FIXME: Exposed LLVM intrinsics is a bug489// 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}
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
778test "FixedBufferAllocator Reuse memory on realloc" {802test "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 memory804 // check if we re-use the memory