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...@@ -140,7 +140,6 @@ set(ZIG_STD_SRC
140 "${CMAKE_SOURCE_DIR}/std/syscall.zig"140 "${CMAKE_SOURCE_DIR}/std/syscall.zig"
141 "${CMAKE_SOURCE_DIR}/std/errno.zig"141 "${CMAKE_SOURCE_DIR}/std/errno.zig"
142 "${CMAKE_SOURCE_DIR}/std/rand.zig"142 "${CMAKE_SOURCE_DIR}/std/rand.zig"
143 "${CMAKE_SOURCE_DIR}/std/mem.zig"
144 "${CMAKE_SOURCE_DIR}/std/math.zig"143 "${CMAKE_SOURCE_DIR}/std/math.zig"
145)144)
146145
doc/targets.md-6
...@@ -12,10 +12,4 @@ Write the target-specific code in std.zig....@@ -12,10 +12,4 @@ Write the target-specific code in std.zig.
1212
13Update the C integer types to be the correct size for the target.13Update 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
21Make sure that `c_long_double` codegens the correct floating point value.15Make 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...@@ -250,12 +250,14 @@ fn i386_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isi
250 [arg6] "{ebp}" (arg6))250 [arg6] "{ebp}" (arg6))
251}251}
252252
253pub fn mmap(address: isize, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {253pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
254 syscall6(SYS_mmap, address, length, prot, flags, fd, offset)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)
255}257}
256258
257pub fn munmap(address: isize, length: isize) -> isize {259pub fn munmap(address: &u8, length: isize) -> isize {
258 syscall2(SYS_munmap, address, length)260 syscall2(SYS_munmap, isize(address), length)
259}261}
260262
261pub fn read(fd: isize, buf: &u8, count: isize) -> isize {263pub fn read(fd: isize, buf: &u8, count: isize) -> isize {
test/run_tests.cpp-19
...@@ -1023,25 +1023,6 @@ pub fn main(args: [][]u8) -> %void {...@@ -1023,25 +1023,6 @@ pub fn main(args: [][]u8) -> %void {
1023}1023}
1024 )SOURCE", "OK\n");1024 )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
1045 add_simple_case("store member function in variable", R"SOURCE(1026 add_simple_case("store member function in variable", R"SOURCE(
1046import "std.zig";1027import "std.zig";
1047struct Foo {1028struct Foo {