From fad3947e26266ada585f6879652fccf7c142e044 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 5 Aug 2018 23:40:19 +0100 Subject: [PATCH] Add `*with_allocator` procedures to `mem` --- core/mem/alloc.odin | 35 +++++++++++++++++++++++++---------- src/parser.cpp | 2 +- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 68789c41a..7cdccdf14 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -21,29 +21,33 @@ Allocator :: struct { -alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr { - a := context.allocator; +alloc_with_allocator :: inline proc(a: Allocator, size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr { return a.procedure(a.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc); } +alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr { + return alloc_with_allocator(context.allocator, size, alignment, loc); +} free_ptr_with_allocator :: inline proc(a: Allocator, ptr: rawptr, loc := #caller_location) { if ptr == nil do return; if a.procedure == nil do return; a.procedure(a.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc); } +free :: inline proc(ptr: rawptr, loc := #caller_location) do free_ptr_with_allocator(context.allocator, ptr, loc); -free :: inline proc(ptr: rawptr, loc := #caller_location) do free_ptr_with_allocator(context.allocator, ptr); - -free_all :: inline proc(loc := #caller_location) { - a := context.allocator; +free_all_with_allocator :: inline proc(a: Allocator, loc := #caller_location) { a.procedure(a.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc); } +free_all :: inline proc(loc := #caller_location) { + free_all_with_allocator(context.allocator, loc); +} - -resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr { - a := context.allocator; +resize_with_allocator :: inline proc(a: Allocator, ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr { return a.procedure(a.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc); } +resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr { + return resize_with_allocator(context.allocator, ptr, old_size, new_size, alignment, loc); +} delete_string :: proc(str: string, loc := #caller_location) { @@ -79,13 +83,24 @@ new :: inline proc(T: type, loc := #caller_location) -> ^T { ptr^ = T{}; return ptr; } - new_clone :: inline proc(data: $T, loc := #caller_location) -> ^T { ptr := (^T)(alloc(size_of(T), align_of(T), loc)); ptr^ = data; return ptr; } +new_with_allocator :: inline proc(a: Allocator, T: type, loc := #caller_location) -> ^T { + ptr := (^T)(alloc_with_allocator(a, size_of(T), align_of(T), loc)); + ptr^ = T{}; + return ptr; +} + +new_clone_with_allocator :: inline proc(a: Allocator, data: $T, loc := #caller_location) -> ^T { + ptr := (^T)(alloc_with_allocator(a, size_of(T), align_of(T), loc)); + ptr^ = data; + return ptr; +} + default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr { if old_memory == nil do return alloc(new_size, alignment, loc); diff --git a/src/parser.cpp b/src/parser.cpp index a1f8ff1e1..3edc8ba57 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4342,7 +4342,7 @@ skip: gb_mutex_lock(&p->file_add_mutex); defer (gb_mutex_unlock(&p->file_add_mutex)); - array_add(&pkg->files, file); + array_add(&file->pkg->files, file); if (pkg->name.len == 0) { pkg->name = file->package_name;