diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-12-23 00:29:39 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-12-23 00:57:56 -0500 |
| commit | 39c7bd24e4f768b23074b8634ac637b175b7639f (patch) | |
| tree | e3454d346c9d37abb9bb56847d3743eeb6cfa4e8 /std | |
| parent | 760b307e8a8fcbb31fc1f2abb170ef7399aa917e (diff) | |
| download | zig-39c7bd24e4f768b23074b8634ac637b175b7639f.tar.gz zig-39c7bd24e4f768b23074b8634ac637b175b7639f.zip | |
port most of main.cpp to self hosted compiler
Diffstat (limited to 'std')
| -rw-r--r-- | std/buffer.zig | 5 | ||||
| -rw-r--r-- | std/c/index.zig | 1 | ||||
| -rw-r--r-- | std/heap.zig | 15 | ||||
| -rw-r--r-- | std/os/index.zig | 33 |
4 files changed, 46 insertions, 8 deletions
diff --git a/std/buffer.zig b/std/buffer.zig index 267d9b6353..4f5d281f48 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -139,6 +139,11 @@ pub const Buffer = struct { %return self.resize(m.len); mem.copy(u8, self.list.toSlice(), m); } + + /// For passing to C functions. + pub fn ptr(self: &const Buffer) -> &u8 { + return self.list.items.ptr; + } }; test "simple Buffer" { diff --git a/std/c/index.zig b/std/c/index.zig index 2ac867ee71..e04b990633 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -48,3 +48,4 @@ pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) -> c_int; pub extern "c" fn malloc(usize) -> ?&c_void; pub extern "c" fn realloc(&c_void, usize) -> ?&c_void; pub extern "c" fn free(&c_void); +pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) -> c_int; diff --git a/std/heap.zig b/std/heap.zig index 605eddb40b..ec447c1aa8 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -10,7 +10,8 @@ const Allocator = mem.Allocator; error OutOfMemory; -pub var c_allocator = Allocator { +pub const c_allocator = &c_allocator_state; +var c_allocator_state = Allocator { .allocFn = cAlloc, .reallocFn = cRealloc, .freeFn = cFree, @@ -24,15 +25,13 @@ fn cAlloc(self: &Allocator, n: usize, alignment: u29) -> %[]u8 { } fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 { - if (new_size <= old_mem.len) { + const old_ptr = @ptrCast(&c_void, old_mem.ptr); + if (c.realloc(old_ptr, new_size)) |buf| { + return @ptrCast(&u8, buf)[0..new_size]; + } else if (new_size <= old_mem.len) { return old_mem[0..new_size]; } else { - const old_ptr = @ptrCast(&c_void, old_mem.ptr); - if (c.realloc(old_ptr, usize(new_size))) |buf| { - return @ptrCast(&u8, buf)[0..new_size]; - } else { - return error.OutOfMemory; - } + return error.OutOfMemory; } } diff --git a/std/os/index.zig b/std/os/index.zig index 09109c3242..8e79eda40b 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -1543,6 +1543,39 @@ pub fn openSelfExe() -> %io.File { } } +/// Get the directory path that contains the current executable. +/// Caller owns returned memory. +pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 { + switch (builtin.os) { + Os.linux => { + // If the currently executing binary has been deleted, + // the file path looks something like `/a/b/c/exe (deleted)` + // This path cannot be opened, but it's valid for determining the directory + // the executable was in when it was run. + const full_exe_path = %return readLink(allocator, "/proc/self/exe"); + %defer allocator.free(full_exe_path); + const dir = path.dirname(full_exe_path); + return allocator.shrink(u8, full_exe_path, dir.len); + }, + Os.windows => { + @panic("TODO windows std.os.selfExeDirPath"); + //buf_resize(out_path, 256); + //for (;;) { + // DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path)); + // if (copied_amt <= 0) { + // return ErrorFileNotFound; + // } + // if (copied_amt < buf_len(out_path)) { + // buf_resize(out_path, copied_amt); + // return 0; + // } + // buf_resize(out_path, buf_len(out_path) * 2); + //} + }, + else => @compileError("unimplemented: std.os.selfExeDirPath for " ++ @tagName(builtin.os)), + } +} + pub fn isTty(handle: FileHandle) -> bool { if (is_windows) { return windows_util.windowsIsTty(handle); |
