Skip to content

Commit

Permalink
std: Add a test for allocations requesting at least N bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonBoy committed Sep 24, 2020
1 parent fe64ae5 commit 09d19a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,13 @@ pub fn testAllocator(base_allocator: *mem.Allocator) !void {
const zero_bit_ptr = try allocator.create(u0);
zero_bit_ptr.* = 0;
allocator.destroy(zero_bit_ptr);

const oversize = try allocator.allocAdvanced(u32, null, 5, .at_least);
testing.expect(oversize.len >= 5);
for (oversize) |*item| {
item.* = 0xDEADBEEF;
}
allocator.free(oversize);
}

pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void {
Expand Down
5 changes: 3 additions & 2 deletions lib/std/heap/arena_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,17 @@ pub const ArenaAllocator = struct {
return result;
}

const bigger_buf_size = @sizeOf(BufNode) + new_end_index;
// Try to grow the buffer in-place
if (self.child_allocator.resizeFn(
self.child_allocator,
cur_node.data,
@alignOf(BufNode),
new_end_index,
bigger_buf_size,
1,
@returnAddress(),
)) |new_size| {
assert(new_size >= new_end_index);
assert(new_size >= bigger_buf_size);
} else |err| switch (err) {
error.OutOfMemory => {
// Allocate a new node if that's not possible
Expand Down

0 comments on commit 09d19a0

Please sign in to comment.