mirror of
https://github.com/ziglang/zig.git
synced 2026-02-18 07:18:38 +00:00
Merge pull request #3238 from Tetralux/fixed-allocator-reset
FixedBufferAllocator.reset
This commit is contained in:
commit
533aff3a6a
24
std/heap.zig
24
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 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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user