diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index dcb7db229..3cf2766bd 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -25,6 +25,7 @@ Allocator :: struct { alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr { if size == 0 do return nil; + if allocator.procedure == nil do return nil; return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc); } @@ -35,11 +36,15 @@ free :: inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_ } free_all :: inline proc(allocator := context.allocator, loc := #caller_location) { - allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc); + if allocator.procedure != nil { + allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc); + } } resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr { - assert(allocator.procedure != nil); + if allocator.procedure == nil { + return nil; + } if new_size == 0 { free(ptr, allocator, loc); return nil; diff --git a/core/os/os_essence.odin b/core/os/os_essence.odin index d2668d70d..0c61ce74d 100644 --- a/core/os/os_essence.odin +++ b/core/os/os_essence.odin @@ -71,7 +71,7 @@ stdin := Handle(-1); // Not implemented stdout := Handle(0); stderr := Handle(0); -current_thread_id :: proc() -> int { +current_thread_id :: proc "contextless" () -> int { return OSGetThreadID(Handle(0x1000)); } diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin index 92e1967d6..a8d241d65 100644 --- a/core/os/os_linux.odin +++ b/core/os/os_linux.odin @@ -245,7 +245,7 @@ exit :: proc(code: int) -> ! { _unix_exit(code); } -current_thread_id :: proc() -> int { +current_thread_id :: proc "contextless" () -> int { // return int(_unix_gettid()); return 0; } diff --git a/core/os/os_osx.odin b/core/os/os_osx.odin index c4c78cb3c..c86875b8f 100644 --- a/core/os/os_osx.odin +++ b/core/os/os_osx.odin @@ -260,7 +260,7 @@ exit :: inline proc(code: int) -> ! { } -current_thread_id :: proc() -> int { +current_thread_id :: proc "contextless" () -> int { // return int(_unix_gettid()); return 0; } diff --git a/core/os/os_windows.odin b/core/os/os_windows.odin index 57a864395..41409291b 100644 --- a/core/os/os_windows.odin +++ b/core/os/os_windows.odin @@ -251,7 +251,7 @@ exit :: proc(code: int) -> ! { -current_thread_id :: proc() -> int { +current_thread_id :: proc "contextless" () -> int { return int(win32.get_current_thread_id()); } diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 3fe66c4a5..976f34d4b 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -190,12 +190,11 @@ Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_L Context :: struct { allocator: mem.Allocator, temp_allocator: mem.Allocator, - thread_id: int, - - assertion_failure_proc: Assertion_Failure_Proc, - + assertion_failure_proc: Assertion_Failure_Proc, logger: log.Logger, + thread_id: int, + user_data: any, user_index: int, @@ -320,11 +319,17 @@ __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) { __init_context :: proc "contextless" (c: ^Context) { if c == nil do return; - c.allocator = os.heap_allocator(); - c.temp_allocator = mem.scratch_allocator(&global_scratch_allocator_data); - c.thread_id = os.current_thread_id(); + c.allocator.procedure = os.heap_allocator_proc; + c.allocator.data = nil; + + c.temp_allocator.procedure = mem.scratch_allocator_proc; + c.temp_allocator.data = &global_scratch_allocator_data; + + c.thread_id = os.current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call c.assertion_failure_proc = default_assertion_failure_proc; - c.logger = log.nil_logger(); + + c.logger.procedure = log.nil_logger_proc; + c.logger.data = nil; } @(builtin) diff --git a/src/ir.cpp b/src/ir.cpp index 5884b2d30..78236bd35 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1759,22 +1759,7 @@ irValue *ir_find_or_generate_context_ptr(irProcedure *proc) { irValue *c = ir_add_local_generated(proc, t_context); ir_push_context_onto_stack(proc, c); ir_emit_store(proc, c, ir_emit_load(proc, proc->module->global_default_context)); - - -#if 1 - Array args = {}; - ir_emit_store(proc, ir_emit_struct_ep(proc, c, 0), ir_emit_package_call(proc, "os", "heap_allocator", args)); - // 1 will be handled later - ir_emit_store(proc, ir_emit_struct_ep(proc, c, 2), ir_emit_package_call(proc, "os", "current_thread_id", args)); - ir_emit_store(proc, ir_emit_struct_ep(proc, c, 3), ir_get_package_value(proc->module, str_lit("runtime"), str_lit("default_assertion_failure_proc"))); - ir_emit_store(proc, ir_emit_struct_ep(proc, c, 4), ir_emit_package_call(proc, "log", "nil_logger", args)); - - array_init(&args, heap_allocator(), 1); - args[0] = ir_get_package_value(proc->module, str_lit("runtime"), str_lit("global_scratch_allocator_data")); - ir_emit_store(proc, ir_emit_struct_ep(proc, c, 1), ir_emit_package_call(proc, "mem", "scratch_allocator", args)); -#else ir_emit_init_context(proc, c); -#endif return c; } @@ -2178,7 +2163,11 @@ void ir_addr_store(irProcedure *proc, irAddr const &addr, irValue *value) { return; } } else if (addr.kind == irAddr_Context) { + irValue *old = ir_emit_load(proc, ir_find_or_generate_context_ptr(proc)); irValue *next = ir_add_local_generated(proc, t_context); + ir_emit_store(proc, next, old); + ir_push_context_onto_stack(proc, next); + if (addr.ctx.sel.index.count > 0) { irValue *lhs = ir_emit_deep_field_gep(proc, next, addr.ctx.sel); irValue *rhs = ir_emit_conv(proc, value, type_deref(ir_type(lhs))); @@ -2189,8 +2178,6 @@ void ir_addr_store(irProcedure *proc, irAddr const &addr, irValue *value) { ir_emit_store(proc, lhs, rhs); } - ir_push_context_onto_stack(proc, next); - return; }