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

FixedBufferAllocator.reset
This commit is contained in:
Andrew Kelley 2019-09-21 12:48:45 -04:00 committed by GitHub
commit 533aff3a6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 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