From f541dd40db3cfe68ff6a6c2e9439328db1f8996c Mon Sep 17 00:00:00 2001 From: Zac Pierson Date: Tue, 21 Mar 2017 14:54:29 -0500 Subject: [PATCH] Fixed some memory leaks and made os_* use strings.odin --- core/os_linux.odin | 36 ++++++++++++++++-------------------- core/os_x.odin | 38 ++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/core/os_linux.odin b/core/os_linux.odin index 1ec48d340..7f440545a 100644 --- a/core/os_linux.odin +++ b/core/os_linux.odin @@ -58,24 +58,12 @@ unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) unix_dlclose :: proc(handle: rawptr) -> int #foreign dl "dlclose"; unix_dlerror :: proc() -> ^u8 #foreign dl "dlerror"; -to_c_str :: proc(str: string) -> ^u8 { - cstr := new_slice(byte, str.count+1); - copy(cstr, cast([]byte)str); - cstr[str.count] = 0; - return cstr.data; -} - -from_c_str :: proc(c_str: ^u8) -> string { - len := 0; - for s := c_str; s^ != 0; s += 1 { - len += 1; - } - return cast(string)slice_ptr(c_str, len); -} // TODO(zangent): Change this to just `open` when Bill fixes overloading. open_simple :: proc(path: string, mode: int) -> (Handle, Errno) { - handle := unix_open(to_c_str(path), mode); + cstr := strings.new_c_string(path); + handle := unix_open(cstr, mode); + free(cstr); if(handle == -1) { return 0, 1; } @@ -182,11 +170,13 @@ heap_free :: proc(ptr: rawptr) { } getenv :: proc(name: string) -> (string, bool) { - cstr: ^u8 = unix_getenv(to_c_str(name)); + path_str := strings.new_c_string(name); + cstr: ^u8 = unix_getenv(path_str); + free(path_str); if(cstr == nil) { return "", false; } - return from_c_str(cstr), true; + return strings.to_odin_string(cstr), true; } exit :: proc(code: int) { @@ -199,16 +189,22 @@ current_thread_id :: proc() -> int { } dlopen :: proc(filename: string, flags: int) -> rawptr #inline { - return unix_dlopen(to_c_str(filename), flags); + cstr := strings.new_c_string(filename); + handle := unix_dlopen(cstr, flags); + free(cstr); + return handle; } dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline { assert(handle != nil); - return unix_dlsym(handle, to_c_str(symbol)); + cstr := strings.new_c_string(symbol); + handle := unix_dlsym(handle, cstr); + free(cstr); + return handle; } dlclose :: proc(handle: rawptr) -> bool #inline { assert(handle != nil); return unix_dlclose(handle) == 0; } dlerror :: proc() -> string { - return from_c_str(unix_dlerror()); + return strings.to_odin_string(unix_dlerror()); } diff --git a/core/os_x.odin b/core/os_x.odin index be86e2252..bc24cca9e 100644 --- a/core/os_x.odin +++ b/core/os_x.odin @@ -1,4 +1,5 @@ #import "fmt.odin"; +#import "strings.odin"; Handle :: i32; File_Time :: u64; @@ -61,24 +62,12 @@ unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) unix_dlclose :: proc(handle: rawptr) -> int #foreign dl "dlclose"; unix_dlerror :: proc() -> ^u8 #foreign dl "dlerror"; -to_c_str :: proc(str: string) -> ^u8 { - cstr := new_slice(byte, str.count+1); - copy(cstr, cast([]byte)str); - cstr[str.count] = 0; - return cstr.data; -} - -from_c_str :: proc(c_str: ^u8) -> string { - len := 0; - for s := c_str; s^ != 0; s += 1 { - len += 1; - } - return cast(string)slice_ptr(c_str, len); -} // TODO(zangent): Change this to just `open` when Bill fixes overloading. open_simple :: proc(path: string, mode: int) -> (Handle, Errno) { - handle := unix_open(to_c_str(path), mode); + cstr := strings.new_c_string(path); + handle := unix_open(cstr, mode); + free(cstr); if(handle == -1) { return 0, 1; } @@ -184,11 +173,13 @@ heap_free :: proc(ptr: rawptr) #inline { } getenv :: proc(name: string) -> (string, bool) { - cstr: ^u8 = unix_getenv(to_c_str(name)); + path_str := strings.new_c_string(name); + cstr: ^u8 = unix_getenv(path_str); + free(path_str); if(cstr == nil) { return "", false; } - return from_c_str(cstr), true; + return strings.to_odin_string(cstr), true; } exit :: proc(code: int) #inline { @@ -202,16 +193,23 @@ current_thread_id :: proc() -> int { } dlopen :: proc(filename: string, flags: int) -> rawptr #inline { - return unix_dlopen(to_c_str(filename), flags); + cstr := strings.new_c_string(filename); + handle := unix_dlopen(cstr, flags); + free(cstr); + return handle; } dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline { assert(handle != nil); - return unix_dlsym(handle, to_c_str(symbol)); + cstr := strings.new_c_string(symbol); + handle := unix_dlsym(handle, cstr); + free(cstr); + return handle; } dlclose :: proc(handle: rawptr) -> bool #inline { assert(handle != nil); return unix_dlclose(handle) == 0; } dlerror :: proc() -> string { - return from_c_str(unix_dlerror()); + return strings.to_odin_string(unix_dlerror()); } +