authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-12 02:23:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-12 02:23:22-07:00
log7828456b30829e68a959f74e86d02e737d590cc2
tree783c37c7937a7895207180d59a25556956d853aa
parent592210a1739dfe3fe506260c11b8b5e5d8a5a715

std: delete malloc and free

later we'll add a full featured allocator instead of this

5 files changed, 6 insertions(+), 52 deletions(-)

CMakeLists.txt-1
......@@ -140,7 +140,6 @@ set(ZIG_STD_SRC
140140 "${CMAKE_SOURCE_DIR}/std/syscall.zig"
141141 "${CMAKE_SOURCE_DIR}/std/errno.zig"
142142 "${CMAKE_SOURCE_DIR}/std/rand.zig"
143 "${CMAKE_SOURCE_DIR}/std/mem.zig"
144143 "${CMAKE_SOURCE_DIR}/std/math.zig"
145144)
146145
doc/targets.md-6
......@@ -12,10 +12,4 @@ Write the target-specific code in std.zig.
1212
1313Update the C integer types to be the correct size for the target.
1414
15Add the conditional compilation code for the page size global. It is hardcoded
16for each target.
17
18Make sure that parseh sends the correct command line parameters to libclang for
19the given target.
20
2115Make sure that `c_long_double` codegens the correct floating point value.
std/mem.zig deleted-22
......@@ -1,22 +0,0 @@
1import "syscall.zig";
2import "std.zig";
3import "errno.zig";
4
5pub fn malloc(bytes: isize) -> ?&u8 {
6 if (bytes > 4096) {
7 %%stderr.printf("TODO alloc sizes > 4096B\n");
8 return null;
9 }
10
11 const result = mmap(isize(0), 4096, MMAP_PROT_READ|MMAP_PROT_WRITE, MMAP_MAP_ANON|MMAP_MAP_SHARED, -1, 0);
12
13 if (-4096 < result && result <= 0) {
14 null
15 } else {
16 (&u8)(result)
17 }
18}
19
20pub fn free(ptr: &u8) {
21 munmap(isize(ptr), 4096);
22}
std/syscall.zig+6-4
......@@ -250,12 +250,14 @@ fn i386_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isi
250250 [arg6] "{ebp}" (arg6))
251251}
252252
253pub fn mmap(address: isize, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
254 syscall6(SYS_mmap, address, length, prot, flags, fd, offset)
253pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
254 // TODO ability to cast maybe pointer to isize
255 const addr = if (const unwrapped ?= address) isize(unwrapped) else 0;
256 syscall6(SYS_mmap, addr, length, prot, flags, fd, offset)
255257}
256258
257pub fn munmap(address: isize, length: isize) -> isize {
258 syscall2(SYS_munmap, address, length)
259pub fn munmap(address: &u8, length: isize) -> isize {
260 syscall2(SYS_munmap, isize(address), length)
259261}
260262
261263pub fn read(fd: isize, buf: &u8, count: isize) -> isize {
test/run_tests.cpp-19
......@@ -1023,25 +1023,6 @@ pub fn main(args: [][]u8) -> %void {
10231023}
10241024 )SOURCE", "OK\n");
10251025
1026 add_simple_case("malloc and free", R"SOURCE(
1027import "mem.zig";
1028import "std.zig";
1029
1030pub fn main(args: [][]u8) -> %void {
1031 var ptr = malloc(1) ?? unreachable{};
1032
1033 *ptr = 6;
1034
1035 if (*ptr != 6) {
1036 %%stdout.printf("BAD\n");
1037 }
1038
1039 free(ptr);
1040
1041 %%stdout.printf("OK\n");
1042}
1043 )SOURCE", "OK\n");
1044
10451026 add_simple_case("store member function in variable", R"SOURCE(
10461027import "std.zig";
10471028struct Foo {