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: 4cc9fe90a8a3
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 52a29928628f
Choose a head ref
  • 3 commits
  • 1 file changed
  • 2 contributors

Commits on Nov 5, 2017

  1. add test for c_allocator

    scurest committed Nov 5, 2017
    Copy the full SHA
    bd6f8d9 View commit details
  2. Copy the full SHA
    48c8181 View commit details

Commits on Nov 6, 2017

  1. Copy the full SHA
    52a2992 View commit details
Showing with 12 additions and 4 deletions.
  1. +12 −4 std/heap.zig
16 changes: 12 additions & 4 deletions std/heap.zig
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@ pub var c_allocator = Allocator {
};

fn cAlloc(self: &Allocator, n: usize, alignment: usize) -> %[]u8 {
if (c.malloc(usize(n))) |mem| {
@ptrCast(&u8, mem)[0..n]
if (c.malloc(usize(n))) |buf| {
@ptrCast(&u8, buf)[0..n]
} else {
error.OutOfMemory
}
@@ -29,8 +29,8 @@ fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: usize)
old_mem[0..new_size]
} else {
const old_ptr = @ptrCast(&c_void, old_mem.ptr);
if (c.realloc(old_ptr, usize(new_size))) |mem| {
@ptrCast(&u8, mem)[0..new_size]
if (c.realloc(old_ptr, usize(new_size))) |buf| {
@ptrCast(&u8, buf)[0..new_size]
} else {
error.OutOfMemory
}
@@ -136,6 +136,14 @@ pub const IncrementingAllocator = struct {
}
};

test "c_allocator" {
if (builtin.link_libc) {
var slice = c_allocator.alloc(u8, 50) %% return;
defer c_allocator.free(slice);
slice = c_allocator.realloc(u8, slice, 100) %% return;
}
}

test "IncrementingAllocator" {
const total_bytes = 100 * 1024 * 1024;
var inc_allocator = %%IncrementingAllocator.init(total_bytes);