Commit 537b260349
Changed files (1)
std
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