From 16eaa17ed988e8a04cc75d8aae3cdea4bd56ad9d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 8 Jun 2021 11:20:39 +0100 Subject: [PATCH] Fix `-target:js_wasm32` for `core:runtime` --- core/os/os_js_wasm32.odin | 5 +- core/runtime/default_allocators_general.odin | 30 +++++++++ core/runtime/default_allocators_nil.odin | 17 +++++ core/runtime/default_allocators_windows.odin | 39 +++++++++++ ....odin => default_temporary_allocator.odin} | 65 ------------------- core/runtime/os_specific_any.odin | 1 + 6 files changed, 90 insertions(+), 67 deletions(-) create mode 100644 core/runtime/default_allocators_general.odin create mode 100644 core/runtime/default_allocators_nil.odin create mode 100644 core/runtime/default_allocators_windows.odin rename core/runtime/{default_allocators.odin => default_temporary_allocator.odin} (74%) diff --git a/core/os/os_js_wasm32.odin b/core/os/os_js_wasm32.odin index 683e4a501..b70bdda1a 100644 --- a/core/os/os_js_wasm32.odin +++ b/core/os/os_js_wasm32.odin @@ -35,8 +35,9 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Errn close :: proc(fd: Handle) -> Errno { return 0; } - - +seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) { + return 0, 0; +} current_thread_id :: proc "contextless" () -> int { return 0; } diff --git a/core/runtime/default_allocators_general.odin b/core/runtime/default_allocators_general.odin new file mode 100644 index 000000000..8f598b7a1 --- /dev/null +++ b/core/runtime/default_allocators_general.odin @@ -0,0 +1,30 @@ +//+build !windows +//+build !freestanding +//+build !js +package runtime + +when ODIN_DEFAULT_TO_NIL_ALLOCATOR { + // mem.nil_allocator reimplementation + + default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, + size, alignment: int, + old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { + return nil, .None; + } + + default_allocator :: proc() -> Allocator { + return Allocator{ + procedure = default_allocator_proc, + data = nil, + }; + } +} else { + // TODO(bill): reimplement these procedures in the os_specific stuff + import "core:os" + + default_allocator_proc :: os.heap_allocator_proc; + + default_allocator :: proc() -> Allocator { + return os.heap_allocator(); + } +} diff --git a/core/runtime/default_allocators_nil.odin b/core/runtime/default_allocators_nil.odin new file mode 100644 index 000000000..4e1d7a1d3 --- /dev/null +++ b/core/runtime/default_allocators_nil.odin @@ -0,0 +1,17 @@ +//+build freestanding, js +package runtime + +// mem.nil_allocator reimplementation + +default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, + size, alignment: int, + old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { + return nil, .None; +} + +default_allocator :: proc() -> Allocator { + return Allocator{ + procedure = default_allocator_proc, + data = nil, + }; +} diff --git a/core/runtime/default_allocators_windows.odin b/core/runtime/default_allocators_windows.odin new file mode 100644 index 000000000..64b2dd23e --- /dev/null +++ b/core/runtime/default_allocators_windows.odin @@ -0,0 +1,39 @@ +//+build windows +package runtime + +default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, + size, alignment: int, + old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { + switch mode { + case .Alloc: + return _windows_default_alloc(size, alignment); + + case .Free: + _windows_default_free(old_memory); + + case .Free_All: + // NOTE(tetra): Do nothing. + + case .Resize: + return _windows_default_resize(old_memory, old_size, size, alignment); + + case .Query_Features: + set := (^Allocator_Mode_Set)(old_memory); + if set != nil { + set^ = {.Alloc, .Free, .Resize, .Query_Features}; + } + return nil, nil; + + case .Query_Info: + return nil, nil; + } + + return nil, nil; +} + +default_allocator :: proc() -> Allocator { + return Allocator{ + procedure = default_allocator_proc, + data = nil, + }; +} diff --git a/core/runtime/default_allocators.odin b/core/runtime/default_temporary_allocator.odin similarity index 74% rename from core/runtime/default_allocators.odin rename to core/runtime/default_temporary_allocator.odin index 86fce47ae..f5bad2a7d 100644 --- a/core/runtime/default_allocators.odin +++ b/core/runtime/default_temporary_allocator.odin @@ -1,70 +1,5 @@ package runtime -when ODIN_DEFAULT_TO_NIL_ALLOCATOR || ODIN_OS == "freestanding" || ODIN_OS == "js" { - // mem.nil_allocator reimplementation - - default_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, - size, alignment: int, - old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { - return nil, .None; - } - - default_allocator :: proc() -> Allocator { - return Allocator{ - procedure = default_allocator_proc, - data = nil, - }; - } - -} else when ODIN_OS == "windows" { - default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, - size, alignment: int, - old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) { - switch mode { - case .Alloc: - return _windows_default_alloc(size, alignment); - - case .Free: - _windows_default_free(old_memory); - - case .Free_All: - // NOTE(tetra): Do nothing. - - case .Resize: - return _windows_default_resize(old_memory, old_size, size, alignment); - - case .Query_Features: - set := (^Allocator_Mode_Set)(old_memory); - if set != nil { - set^ = {.Alloc, .Free, .Resize, .Query_Features}; - } - return nil, nil; - - case .Query_Info: - return nil, nil; - } - - return nil, nil; - } - - default_allocator :: proc() -> Allocator { - return Allocator{ - procedure = default_allocator_proc, - data = nil, - }; - } - -} else { - // TODO(bill): reimplement these procedures in the os_specific stuff - import "core:os" - - default_allocator_proc :: os.heap_allocator_proc; - - default_allocator :: proc() -> Allocator { - return os.heap_allocator(); - } -} - @(private) byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> []byte { return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)}; diff --git a/core/runtime/os_specific_any.odin b/core/runtime/os_specific_any.odin index 9c2686661..5a8fd2e94 100644 --- a/core/runtime/os_specific_any.odin +++ b/core/runtime/os_specific_any.odin @@ -1,5 +1,6 @@ //+build !freestanding //+build !windows +//+build !js package runtime import "core:os"