Add *with_allocator procedures to mem

This commit is contained in:
gingerBill
2018-08-05 23:40:19 +01:00
parent d8e5b2d1a4
commit fad3947e26
2 changed files with 26 additions and 11 deletions

View File

@@ -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);

View File

@@ -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;