Improve default temp allocator; Fix filepath.abs behaviour on Windows

This commit is contained in:
gingerBill
2020-10-13 14:40:13 +01:00
parent 1b4bccbc94
commit fa33476438
5 changed files with 59 additions and 38 deletions

View File

@@ -124,7 +124,7 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
r, dot_dot := 0, 0;
if rooted {
lazy_buffer_append(out, '/');
lazy_buffer_append(out, SEPARATOR);
r, dot_dot = 1, 1;
}
@@ -144,7 +144,7 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
}
case !rooted:
if out.w > 0 {
lazy_buffer_append(out, '/');
lazy_buffer_append(out, SEPARATOR);
}
lazy_buffer_append(out, '.');
lazy_buffer_append(out, '.');
@@ -152,7 +152,7 @@ clean :: proc(path: string, allocator := context.allocator) -> string {
}
case:
if rooted && out.w != 1 || !rooted && out.w != 0 {
lazy_buffer_append(out, '/');
lazy_buffer_append(out, SEPARATOR);
}
for ; r < n && !is_separator(path[r]); r += 1 {
lazy_buffer_append(out, path[r]);

View File

@@ -49,14 +49,16 @@ is_abs :: proc(path: string) -> bool {
@(private)
full_path :: proc(name: string, allocator := context.allocator) -> (path: string, err: os.Errno) {
temp_full_path :: proc(name: string) -> (path: string, err: os.Errno) {
ta := context.temp_allocator;
name := name;
if name == "" {
name = ".";
}
p := win32.utf8_to_utf16(name, context.temp_allocator);
defer delete(p);
buf := make([dynamic]u16, 100, allocator);
p := win32.utf8_to_utf16(name, ta);
buf := make([dynamic]u16, 100, ta);
for {
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil);
if n == 0 {
@@ -64,7 +66,7 @@ full_path :: proc(name: string, allocator := context.allocator) -> (path: string
return "", os.Errno(win32.GetLastError());
}
if n <= u32(len(buf)) {
return win32.utf16_to_utf8(buf[:n]), os.ERROR_NONE;
return win32.utf16_to_utf8(buf[:n], ta), os.ERROR_NONE;
}
resize(&buf, len(buf)*2);
}
@@ -74,13 +76,13 @@ full_path :: proc(name: string, allocator := context.allocator) -> (path: string
abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
full_path, err := full_path(path, context.temp_allocator);
full_path, err := temp_full_path(path);
if err != 0 {
return "", false;
}
return clean(full_path, allocator), true;
p := clean(full_path, allocator);
return p, true;
}
split_list :: proc(path: string, allocator := context.allocator) -> []string {

View File

@@ -123,6 +123,10 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
return ptr;
case .Free:
if old_memory == nil {
return nil;
}
start := uintptr(raw_data(s.data));
end := start + uintptr(len(s.data));
old_ptr := uintptr(old_memory);
@@ -161,9 +165,11 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
begin := uintptr(raw_data(s.data));
end := begin + uintptr(len(s.data));
old_ptr := uintptr(old_memory);
if begin <= old_ptr && old_ptr < end && old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size;
return old_memory;
if old_memory == s.prev_allocation && old_ptr & uintptr(alignment)-1 == 0 {
if old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size;
return old_memory;
}
}
ptr := default_temp_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, flags, loc);
mem_copy(ptr, old_memory, old_size);

View File

@@ -2,15 +2,16 @@ package sys_windows
import "core:c"
c_char :: c.char;
c_int :: c.int;
c_uint :: c.uint;
c_long :: c.long;
c_char :: c.char;
c_int :: c.int;
c_uint :: c.uint;
c_long :: c.long;
c_longlong :: c.longlong;
c_ulong :: c.ulong;
c_ushort :: c.ushort;
size_t :: c.size_t;
wchar_t :: c.wchar_t;
c_ulong :: c.ulong;
c_short :: c.short;
c_ushort :: c.ushort;
size_t :: c.size_t;
wchar_t :: c.wchar_t;
DWORD :: c_ulong;
HANDLE :: distinct LPVOID;
@@ -36,6 +37,9 @@ DWORD_PTR :: ULONG_PTR;
ULONG :: c_ulong;
UCHAR :: BYTE;
PDWORD_PTR :: ^DWORD_PTR;
ATOM :: distinct WORD;
wstring :: ^WCHAR;
LPBOOL :: ^BOOL;
@@ -717,15 +721,15 @@ SYSTEM_INFO :: struct {
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_osversioninfoexw
OSVERSIONINFOEXW :: struct {
os_version_info_size: ULONG,
major_version: ULONG,
minor_version: ULONG,
build_number: ULONG,
platform_id : ULONG,
service_pack_string: [128]WCHAR,
service_pack_major: USHORT,
service_pack_minor: USHORT,
suite_mask: USHORT,
product_type: UCHAR,
reserved: UCHAR,
os_version_info_size: ULONG,
major_version: ULONG,
minor_version: ULONG,
build_number: ULONG,
platform_id : ULONG,
service_pack_string: [128]WCHAR,
service_pack_major: USHORT,
service_pack_minor: USHORT,
suite_mask: USHORT,
product_type: UCHAR,
reserved: UCHAR,
}

View File

@@ -1,12 +1,20 @@
package sys_windows
LOWORD :: inline proc "contextless" (x: DWORD) -> WORD {
return WORD(x & 0xffff);
}
HIWORD :: inline proc "contextless" (x: DWORD) -> WORD {
return WORD(x >> 16);
}
utf8_to_utf16 :: proc(s: string, allocator := context.temp_allocator) -> []u16 {
if len(s) < 1 {
return nil;
}
b := transmute([]byte)s;
cstr := &b[0];
cstr := raw_data(b);
n := MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, cstr, i32(len(s)), nil, 0);
if n == 0 {
return nil;
@@ -14,7 +22,7 @@ utf8_to_utf16 :: proc(s: string, allocator := context.temp_allocator) -> []u16 {
text := make([]u16, n+1, allocator);
n1 := MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, cstr, i32(len(s)), wstring(&text[0]), i32(n));
n1 := MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, cstr, i32(len(s)), raw_data(text), i32(n));
if n1 == 0 {
delete(text, allocator);
return nil;
@@ -34,7 +42,7 @@ utf8_to_wstring :: proc(s: string, allocator := context.temp_allocator) -> wstri
}
wstring_to_utf8 :: proc(s: wstring, N: int, allocator := context.temp_allocator) -> string {
if N == 0 {
if N <= 0 {
return "";
}
@@ -50,7 +58,8 @@ wstring_to_utf8 :: proc(s: wstring, N: int, allocator := context.temp_allocator)
// will not be null terminated, we therefore have to force it to be null terminated manually.
text := make([]byte, n+1 if N != -1 else n, allocator);
if n1 := WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, s, i32(N), &text[0], n, nil, nil); n1 == 0 {
n1 := WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, s, i32(N), raw_data(text), n, nil, nil);
if n1 == 0 {
delete(text, allocator);
return "";
}
@@ -69,6 +78,6 @@ utf16_to_utf8 :: proc(s: []u16, allocator := context.temp_allocator) -> string {
if len(s) == 0 {
return "";
}
return wstring_to_utf8(cast(wstring)&s[0], len(s), allocator);
return wstring_to_utf8(raw_data(s), len(s), allocator);
}