Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ziglang/zig
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 29c756abba20
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 91636f1e8cc1
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jul 14, 2018

  1. Verified

    This commit was signed with the committer’s verified signature.
    brandonkelly Brandon Kelly
    Copy the full SHA
    8be6c98 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature.
    brandonkelly Brandon Kelly
    Copy the full SHA
    c021a44 View commit details
  3. Merge pull request #1237 from BarabasGitHub/fix-reallocating-from-0

    Fix aligned reallocation
    andrewrk authored Jul 14, 2018
    Copy the full SHA
    91636f1 View commit details
Showing with 36 additions and 2 deletions.
  1. +35 −1 std/heap.zig
  2. +1 −1 std/mem.zig
36 changes: 35 additions & 1 deletion std/heap.zig
Original file line number Diff line number Diff line change
@@ -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,19 +463,21 @@ 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);
}

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?
2 changes: 1 addition & 1 deletion std/mem.zig
Original file line number Diff line number Diff line change
@@ -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);