Skip to content

Commit 09d19a0

Browse files
committed
std: Add a test for allocations requesting at least N bytes
1 parent fe64ae5 commit 09d19a0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/std/heap.zig

+7
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,13 @@ pub fn testAllocator(base_allocator: *mem.Allocator) !void {
919919
const zero_bit_ptr = try allocator.create(u0);
920920
zero_bit_ptr.* = 0;
921921
allocator.destroy(zero_bit_ptr);
922+
923+
const oversize = try allocator.allocAdvanced(u32, null, 5, .at_least);
924+
testing.expect(oversize.len >= 5);
925+
for (oversize) |*item| {
926+
item.* = 0xDEADBEEF;
927+
}
928+
allocator.free(oversize);
922929
}
923930

924931
pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void {

lib/std/heap/arena_allocator.zig

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,17 @@ pub const ArenaAllocator = struct {
8282
return result;
8383
}
8484

85+
const bigger_buf_size = @sizeOf(BufNode) + new_end_index;
8586
// Try to grow the buffer in-place
8687
if (self.child_allocator.resizeFn(
8788
self.child_allocator,
8889
cur_node.data,
8990
@alignOf(BufNode),
90-
new_end_index,
91+
bigger_buf_size,
9192
1,
9293
@returnAddress(),
9394
)) |new_size| {
94-
assert(new_size >= new_end_index);
95+
assert(new_size >= bigger_buf_size);
9596
} else |err| switch (err) {
9697
error.OutOfMemory => {
9798
// Allocate a new node if that's not possible

0 commit comments

Comments
 (0)