aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-11-05 19:38:50 -0500
committerGitHub <noreply@github.com>2017-11-05 19:38:50 -0500
commit52a29928628f042da6cb39b3c1f77af0bcb7dcd7 (patch)
tree72665f75f2b000ddd6434343643f1db1d1aa1173
parent4cc9fe90a8a3c0bce803bf9fffd66477da9e37d0 (diff)
parent48c8181886e783aecb32c2c9ca9e2af1e39fd1bf (diff)
downloadzig-52a29928628f042da6cb39b3c1f77af0bcb7dcd7.tar.gz
zig-52a29928628f042da6cb39b3c1f77af0bcb7dcd7.zip
Merge pull request #587 from scurest/c_alloc_redeclaration_of_mem
Fix #585
-rw-r--r--std/heap.zig16
1 files changed, 12 insertions, 4 deletions
diff --git a/std/heap.zig b/std/heap.zig
index b654f28a74..d0bf8ab871 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -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);