Browse Source

Add `*with_allocator` procedures to `mem`

gingerBill 7 years ago
parent
commit
fad3947e26
2 changed files with 26 additions and 11 deletions
  1. 25 10
      core/mem/alloc.odin
  2. 1 1
      src/parser.cpp

+ 25 - 10
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);
 	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) {
 free_ptr_with_allocator :: inline proc(a: Allocator, ptr: rawptr, loc := #caller_location) {
 	if ptr == nil do return;
 	if ptr == nil do return;
 	if a.procedure == nil do return;
 	if a.procedure == nil do return;
 	a.procedure(a.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
 	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);
 	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);
 	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) {
 delete_string :: proc(str: string, loc := #caller_location) {
@@ -79,13 +83,24 @@ new :: inline proc(T: type, loc := #caller_location) -> ^T {
 	ptr^ = T{};
 	ptr^ = T{};
 	return ptr;
 	return ptr;
 }
 }
-
 new_clone :: inline proc(data: $T, loc := #caller_location) -> ^T {
 new_clone :: inline proc(data: $T, loc := #caller_location) -> ^T {
 	ptr := (^T)(alloc(size_of(T), align_of(T), loc));
 	ptr := (^T)(alloc(size_of(T), align_of(T), loc));
 	ptr^ = data;
 	ptr^ = data;
 	return ptr;
 	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 {
 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);
 	if old_memory == nil do return alloc(new_size, alignment, loc);

+ 1 - 1
src/parser.cpp

@@ -4342,7 +4342,7 @@ skip:
 		gb_mutex_lock(&p->file_add_mutex);
 		gb_mutex_lock(&p->file_add_mutex);
 		defer (gb_mutex_unlock(&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) {
 		if (pkg->name.len == 0) {
 			pkg->name = file->package_name;
 			pkg->name = file->package_name;