From 8be6c98ca63dac5cf5e769171bb72cd888efbbf4 Mon Sep 17 00:00:00 2001 From: Bas van den Berg Date: Sat, 14 Jul 2018 18:03:06 +0200 Subject: [PATCH 1/2] Create unit test that tests aligned reallocation. --- std/heap.zig | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/std/heap.zig b/std/heap.zig index ef22c8d0c54a..4f21ef5cd13c 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -442,6 +442,7 @@ test "DirectAllocator" { const allocator = &direct_allocator.allocator; try testAllocator(allocator); + try testAllocatorAligned(allocator, 16); try testAllocatorLargeAlignment(allocator); } @@ -453,6 +454,7 @@ test "ArenaAllocator" { defer arena_allocator.deinit(); try testAllocator(&arena_allocator.allocator); + try testAllocatorAligned(&arena_allocator.allocator, 16); try testAllocatorLargeAlignment(&arena_allocator.allocator); } @@ -461,6 +463,7 @@ test "FixedBufferAllocator" { var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); try testAllocator(&fixed_buffer_allocator.allocator); + try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16); try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); } @@ -468,12 +471,13 @@ test "ThreadSafeFixedBufferAllocator" { var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); try testAllocator(&fixed_buffer_allocator.allocator); + try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16); try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); } fn testAllocator(allocator: *mem.Allocator) !void { var slice = try allocator.alloc(*i32, 100); - + assert(slice.len == 100); for (slice) |*item, i| { item.* = try allocator.create(@intCast(i32, i)); } @@ -483,13 +487,43 @@ fn testAllocator(allocator: *mem.Allocator) !void { } slice = try allocator.realloc(*i32, slice, 20000); + assert(slice.len == 20000); slice = try allocator.realloc(*i32, slice, 50); + assert(slice.len == 50); slice = try allocator.realloc(*i32, slice, 25); + assert(slice.len == 25); + slice = try allocator.realloc(*i32, slice, 0); + assert(slice.len == 0); slice = try allocator.realloc(*i32, slice, 10); + assert(slice.len == 10); allocator.free(slice); } +fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void { + // initial + var slice = try allocator.alignedAlloc(u8, alignment, 10); + assert(slice.len == 10); + // grow + slice = try allocator.alignedRealloc(u8, alignment, slice, 100); + assert(slice.len == 100); + // shrink + slice = try allocator.alignedRealloc(u8, alignment, slice, 10); + assert(slice.len == 10); + // go to zero + slice = try allocator.alignedRealloc(u8, alignment, slice, 0); + assert(slice.len == 0); + // realloc from zero + slice = try allocator.alignedRealloc(u8, alignment, slice, 100); + assert(slice.len == 100); + // shrink with shrink + slice = allocator.alignedShrink(u8, alignment, slice, 10); + assert(slice.len == 10); + // shrink to zero + slice = allocator.alignedShrink(u8, alignment, slice, 0); + assert(slice.len == 0); +} + fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void { //Maybe a platform's page_size is actually the same as or // very near usize? From c021a445672fde506dbbcc331ca3b846f9b6e2a1 Mon Sep 17 00:00:00 2001 From: Bas van den Berg Date: Sat, 14 Jul 2018 18:05:05 +0200 Subject: [PATCH 2/2] Fix aligned reallocation from zero size. --- std/mem.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/mem.zig b/std/mem.zig index 32c17fcb26b4..2a5b0366a9af 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -74,7 +74,7 @@ pub const Allocator = struct { pub fn alignedRealloc(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T { if (old_mem.len == 0) { - return self.alloc(T, n); + return self.alignedAlloc(T, alignment, n); } if (n == 0) { self.free(old_mem);