authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-05 19:38:50-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2017-11-05 19:38:50-05:00
log52a29928628f042da6cb39b3c1f77af0bcb7dcd7
tree72665f75f2b000ddd6434343643f1db1d1aa1173
parent4cc9fe90a8a3c0bce803bf9fffd66477da9e37d0
parent48c8181886e783aecb32c2c9ca9e2af1e39fd1bf
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #587 from scurest/c_alloc_redeclaration_of_mem

Fix #585

1 files changed, 12 insertions(+), 4 deletions(-)

std/heap.zig+12-4
...@@ -17,8 +17,8 @@ pub var c_allocator = Allocator {...@@ -17,8 +17,8 @@ pub var c_allocator = Allocator {
17};17};
1818
19fn cAlloc(self: &Allocator, n: usize, alignment: usize) -> %[]u8 {19fn cAlloc(self: &Allocator, n: usize, alignment: usize) -> %[]u8 {
20 if (c.malloc(usize(n))) |mem| {20 if (c.malloc(usize(n))) |buf| {
21 @ptrCast(&u8, mem)[0..n]21 @ptrCast(&u8, buf)[0..n]
22 } else {22 } else {
23 error.OutOfMemory23 error.OutOfMemory
24 }24 }
...@@ -29,8 +29,8 @@ fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: usize)...@@ -29,8 +29,8 @@ fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: usize)
29 old_mem[0..new_size]29 old_mem[0..new_size]
30 } else {30 } else {
31 const old_ptr = @ptrCast(&c_void, old_mem.ptr);31 const old_ptr = @ptrCast(&c_void, old_mem.ptr);
32 if (c.realloc(old_ptr, usize(new_size))) |mem| {32 if (c.realloc(old_ptr, usize(new_size))) |buf| {
33 @ptrCast(&u8, mem)[0..new_size]33 @ptrCast(&u8, buf)[0..new_size]
34 } else {34 } else {
35 error.OutOfMemory35 error.OutOfMemory
36 }36 }
...@@ -136,6 +136,14 @@ pub const IncrementingAllocator = struct {...@@ -136,6 +136,14 @@ pub const IncrementingAllocator = struct {
136 }136 }
137};137};
138138
139test "c_allocator" {
140 if (builtin.link_libc) {
141 var slice = c_allocator.alloc(u8, 50) %% return;
142 defer c_allocator.free(slice);
143 slice = c_allocator.realloc(u8, slice, 100) %% return;
144 }
145}
146
139test "IncrementingAllocator" {147test "IncrementingAllocator" {
140 const total_bytes = 100 * 1024 * 1024;148 const total_bytes = 100 * 1024 * 1024;
141 var inc_allocator = %%IncrementingAllocator.init(total_bytes);149 var inc_allocator = %%IncrementingAllocator.init(total_bytes);