authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-14 12:09:54-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-07-14 12:09:54-04:00
log91636f1e8cc197a310205724458a4e1154530720
tree8a8003e284bf8e02b421a54a656a2a0da7046f38
parent29c756abba20921b76ba0a2611dd56f0123fd68c
parentc021a445672fde506dbbcc331ca3b846f9b6e2a1
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1237 from BarabasGitHub/fix-reallocating-from-0

Fix aligned reallocation

2 files changed, 36 insertions(+), 2 deletions(-)

std/heap.zig+35-1
......@@ -442,6 +442,7 @@ test "DirectAllocator" {
442442
443443 const allocator = &direct_allocator.allocator;
444444 try testAllocator(allocator);
445 try testAllocatorAligned(allocator, 16);
445446 try testAllocatorLargeAlignment(allocator);
446447}
447448
......@@ -453,6 +454,7 @@ test "ArenaAllocator" {
453454 defer arena_allocator.deinit();
454455
455456 try testAllocator(&arena_allocator.allocator);
457 try testAllocatorAligned(&arena_allocator.allocator, 16);
456458 try testAllocatorLargeAlignment(&arena_allocator.allocator);
457459}
458460
......@@ -461,6 +463,7 @@ test "FixedBufferAllocator" {
461463 var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
462464
463465 try testAllocator(&fixed_buffer_allocator.allocator);
466 try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16);
464467 try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
465468}
466469
......@@ -468,12 +471,13 @@ test "ThreadSafeFixedBufferAllocator" {
468471 var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
469472
470473 try testAllocator(&fixed_buffer_allocator.allocator);
474 try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16);
471475 try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
472476}
473477
474478fn testAllocator(allocator: *mem.Allocator) !void {
475479 var slice = try allocator.alloc(*i32, 100);
476
480 assert(slice.len == 100);
477481 for (slice) |*item, i| {
478482 item.* = try allocator.create(@intCast(i32, i));
479483 }
......@@ -483,13 +487,43 @@ fn testAllocator(allocator: *mem.Allocator) !void {
483487 }
484488
485489 slice = try allocator.realloc(*i32, slice, 20000);
490 assert(slice.len == 20000);
486491 slice = try allocator.realloc(*i32, slice, 50);
492 assert(slice.len == 50);
487493 slice = try allocator.realloc(*i32, slice, 25);
494 assert(slice.len == 25);
495 slice = try allocator.realloc(*i32, slice, 0);
496 assert(slice.len == 0);
488497 slice = try allocator.realloc(*i32, slice, 10);
498 assert(slice.len == 10);
489499
490500 allocator.free(slice);
491501}
492502
503fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void {
504 // initial
505 var slice = try allocator.alignedAlloc(u8, alignment, 10);
506 assert(slice.len == 10);
507 // grow
508 slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
509 assert(slice.len == 100);
510 // shrink
511 slice = try allocator.alignedRealloc(u8, alignment, slice, 10);
512 assert(slice.len == 10);
513 // go to zero
514 slice = try allocator.alignedRealloc(u8, alignment, slice, 0);
515 assert(slice.len == 0);
516 // realloc from zero
517 slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
518 assert(slice.len == 100);
519 // shrink with shrink
520 slice = allocator.alignedShrink(u8, alignment, slice, 10);
521 assert(slice.len == 10);
522 // shrink to zero
523 slice = allocator.alignedShrink(u8, alignment, slice, 0);
524 assert(slice.len == 0);
525}
526
493527fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
494528 //Maybe a platform's page_size is actually the same as or
495529 // very near usize?
std/mem.zig+1-1
......@@ -74,7 +74,7 @@ pub const Allocator = struct {
7474
7575 pub fn alignedRealloc(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
7676 if (old_mem.len == 0) {
77 return self.alloc(T, n);
77 return self.alignedAlloc(T, alignment, n);
7878 }
7979 if (n == 0) {
8080 self.free(old_mem);