Add mem.Allocator_Mode.Query_Features, mem.Allocator_Mode_Set, mem.query_features`

This commit is contained in:
gingerBill
2020-08-16 22:07:40 +01:00
parent 1f571f48e5
commit 033b46def8
5 changed files with 92 additions and 1 deletions
+18
View File
@@ -10,9 +10,15 @@ Allocator_Mode :: enum byte {
Free, Free,
Free_All, Free_All,
Resize, Resize,
Query_Features,
} }
*/ */
Allocator_Mode_Set :: runtime.Allocator_Mode_Set;
/*
Allocator_Mode_Set :: distinct bit_set[Allocator_Mode];
*/
Allocator_Proc :: runtime.Allocator_Proc; Allocator_Proc :: runtime.Allocator_Proc;
/* /*
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode, Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
@@ -63,6 +69,18 @@ resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEF
return allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc); return allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc);
} }
query_features :: proc(allocator: Allocator, loc := #caller_location) -> Allocator_Mode_Set {
if allocator.procedure != nil {
set: Allocator_Mode_Set;
res := allocator.procedure(allocator.data, Allocator_Mode.Query_Features, 0, 0, &set, 0, 0, loc);
if res == &set {
return set;
}
}
return nil;
}
delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) { delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) {
free(raw_data(str), allocator, loc); free(raw_data(str), allocator, loc);
+56
View File
@@ -74,6 +74,13 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Resize: case .Resize:
return default_resize_align(old_memory, old_size, size, alignment, arena_allocator(arena)); return default_resize_align(old_memory, old_size, size, alignment, arena_allocator(arena));
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
@@ -197,6 +204,13 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
return old_memory; return old_memory;
} }
return scratch_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc); return scratch_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc);
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
@@ -348,6 +362,13 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
} }
return old_memory; return old_memory;
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
@@ -468,6 +489,13 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
ptr := raw_alloc(s, size, align); ptr := raw_alloc(s, size, align);
copy(ptr, old_memory, min(old_size, size)); copy(ptr, old_memory, min(old_size, size));
return ptr; return ptr;
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
@@ -519,6 +547,13 @@ dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
ptr := dynamic_pool_alloc(pool, size); ptr := dynamic_pool_alloc(pool, size);
copy(ptr, old_memory, old_size); copy(ptr, old_memory, old_size);
return ptr; return ptr;
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
} }
@@ -654,6 +689,13 @@ panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
} }
case .Free_All: case .Free_All:
panic("mem: panic allocator, .Free_All called"); panic("mem: panic allocator, .Free_All called");
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Query_Features};
}
return set;
} }
return nil; return nil;
@@ -701,6 +743,13 @@ alloca_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
// Do nothing // Do nothing
case .Free_All: case .Free_All:
// Do nothing // Do nothing
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
} }
@@ -774,6 +823,13 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, si
if data.clear_on_free_all { if data.clear_on_free_all {
clear_map(&data.allocation_map); clear_map(&data.allocation_map);
} }
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return result; return result;
+7
View File
@@ -194,6 +194,13 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
return aligned_alloc(size, alignment); return aligned_alloc(size, alignment);
} }
return aligned_resize(old_memory, old_size, size, alignment); return aligned_resize(old_memory, old_size, size, alignment);
case .Query_Features:
set := (^mem.Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;
+4 -1
View File
@@ -251,8 +251,11 @@ Allocator_Mode :: enum byte {
Free, Free,
Free_All, Free_All,
Resize, Resize,
Query_Features,
} }
Allocator_Mode_Set :: distinct bit_set[Allocator_Mode];
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode, Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, location: Source_Code_Location = #caller_location) -> rawptr; old_memory: rawptr, old_size: int, flags: u64 = 0, location: Source_Code_Location = #caller_location) -> rawptr;
@@ -263,7 +266,7 @@ Allocator :: struct {
// Logging stuff // Logging stuff
Logger_Level :: enum { Logger_Level :: enum uint {
Debug = 0, Debug = 0,
Info = 10, Info = 10,
Warning = 20, Warning = 20,
+7
View File
@@ -132,6 +132,13 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
ptr := default_temp_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc); ptr := default_temp_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc);
mem_copy(ptr, old_memory, old_size); mem_copy(ptr, old_memory, old_size);
return ptr; return ptr;
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory);
if set != nil {
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
}
return set;
} }
return nil; return nil;