From 7828456b30829e68a959f74e86d02e737d590cc2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 12 Feb 2016 02:23:22 -0700 Subject: [PATCH] std: delete malloc and free later we'll add a full featured allocator instead of this --- CMakeLists.txt | 1 - doc/targets.md | 6 ------ std/mem.zig | 22 ---------------------- std/syscall.zig | 10 ++++++---- test/run_tests.cpp | 19 ------------------- 5 files changed, 6 insertions(+), 52 deletions(-) delete mode 100644 std/mem.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 6feb456fae..efe9802f2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,7 +140,6 @@ set(ZIG_STD_SRC "${CMAKE_SOURCE_DIR}/std/syscall.zig" "${CMAKE_SOURCE_DIR}/std/errno.zig" "${CMAKE_SOURCE_DIR}/std/rand.zig" - "${CMAKE_SOURCE_DIR}/std/mem.zig" "${CMAKE_SOURCE_DIR}/std/math.zig" ) diff --git a/doc/targets.md b/doc/targets.md index de465959c9..fe1e388bb0 100644 --- a/doc/targets.md +++ b/doc/targets.md @@ -12,10 +12,4 @@ Write the target-specific code in std.zig. Update the C integer types to be the correct size for the target. -Add the conditional compilation code for the page size global. It is hardcoded -for each target. - -Make sure that parseh sends the correct command line parameters to libclang for -the given target. - Make sure that `c_long_double` codegens the correct floating point value. diff --git a/std/mem.zig b/std/mem.zig deleted file mode 100644 index a10a6c9569..0000000000 --- a/std/mem.zig +++ /dev/null @@ -1,22 +0,0 @@ -import "syscall.zig"; -import "std.zig"; -import "errno.zig"; - -pub fn malloc(bytes: isize) -> ?&u8 { - if (bytes > 4096) { - %%stderr.printf("TODO alloc sizes > 4096B\n"); - return null; - } - - const result = mmap(isize(0), 4096, MMAP_PROT_READ|MMAP_PROT_WRITE, MMAP_MAP_ANON|MMAP_MAP_SHARED, -1, 0); - - if (-4096 < result && result <= 0) { - null - } else { - (&u8)(result) - } -} - -pub fn free(ptr: &u8) { - munmap(isize(ptr), 4096); -} diff --git a/std/syscall.zig b/std/syscall.zig index 669fb2b8c0..00a44832e3 100644 --- a/std/syscall.zig +++ b/std/syscall.zig @@ -250,12 +250,14 @@ fn i386_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isi [arg6] "{ebp}" (arg6)) } -pub fn mmap(address: isize, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize { - syscall6(SYS_mmap, address, length, prot, flags, fd, offset) +pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize { + // TODO ability to cast maybe pointer to isize + const addr = if (const unwrapped ?= address) isize(unwrapped) else 0; + syscall6(SYS_mmap, addr, length, prot, flags, fd, offset) } -pub fn munmap(address: isize, length: isize) -> isize { - syscall2(SYS_munmap, address, length) +pub fn munmap(address: &u8, length: isize) -> isize { + syscall2(SYS_munmap, isize(address), length) } pub fn read(fd: isize, buf: &u8, count: isize) -> isize { diff --git a/test/run_tests.cpp b/test/run_tests.cpp index b372473eec..e7d696a34c 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1023,25 +1023,6 @@ pub fn main(args: [][]u8) -> %void { } )SOURCE", "OK\n"); - add_simple_case("malloc and free", R"SOURCE( -import "mem.zig"; -import "std.zig"; - -pub fn main(args: [][]u8) -> %void { - var ptr = malloc(1) ?? unreachable{}; - - *ptr = 6; - - if (*ptr != 6) { - %%stdout.printf("BAD\n"); - } - - free(ptr); - - %%stdout.printf("OK\n"); -} - )SOURCE", "OK\n"); - add_simple_case("store member function in variable", R"SOURCE( import "std.zig"; struct Foo {