zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 537b26034925bf7452db2ff4cd6b29de92d77471 (tree)
parent 8223aca09bc93279a88d1439d6dfc437144ec578
Author: Tetralux <tetralux@teknik.io>
Date:   Mon, 16 Sep 2019 01:35:01 +0000

Add FixedBufferAllocator.reset

Diffstat:
Mstd/heap.zig | 24++++++++++++++++++++++++
1 file changed, 24 insertions(+), 0 deletions(-)

diff --git a/std/heap.zig b/std/heap.zig @@ -480,6 +480,10 @@ pub const FixedBufferAllocator = struct { fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { return old_mem[0..new_size]; } + + pub inline fn reset(self: *FixedBufferAllocator) void { + self.end_index = 0; + } }; // FIXME: Exposed LLVM intrinsics is a bug @@ -775,6 +779,26 @@ test "FixedBufferAllocator" { try testAllocatorAlignedShrink(&fixed_buffer_allocator.allocator); } +test "FixedBufferAllocator.reset" { + var buf: [8]u8 align(@alignOf(usize)) = undefined; + var fba = FixedBufferAllocator.init(buf[0..]); + + const X = 0xeeeeeeeeeeeeeeee; + const Y = 0xffffffffffffffff; + + var x = try fba.allocator.create(u64); + x.* = X; + testing.expectError(error.OutOfMemory, fba.allocator.create(u64)); + + fba.reset(); + var y = try fba.allocator.create(u64); + y.* = Y; + + // we expect Y to have overwritten X. + testing.expect(x.* == y.*); + testing.expect(y.* == Y); +} + test "FixedBufferAllocator Reuse memory on realloc" { var small_fixed_buffer: [10]u8 = undefined; // check if we re-use the memory