Improve default temp_allocator; make nil loggers do nothing; improve mem.Scratch_Allocator behaviour

This commit is contained in:
gingerBill
2020-10-02 16:06:55 +01:00
parent a65553293f
commit 6eeb12a986
4 changed files with 195 additions and 130 deletions
+6
View File
@@ -123,6 +123,9 @@ panicf :: proc(fmt_str: string, args: ..any, location := #caller_location) -> !
log :: proc(level: Level, args: ..any, sep := " ", location := #caller_location) { log :: proc(level: Level, args: ..any, sep := " ", location := #caller_location) {
logger := context.logger; logger := context.logger;
if logger.procedure == nil {
return;
}
if level < logger.lowest_level { if level < logger.lowest_level {
return; return;
} }
@@ -132,6 +135,9 @@ log :: proc(level: Level, args: ..any, sep := " ", location := #caller_location)
logf :: proc(level: Level, fmt_str: string, args: ..any, location := #caller_location) { logf :: proc(level: Level, fmt_str: string, args: ..any, location := #caller_location) {
logger := context.logger; logger := context.logger;
if logger.procedure == nil {
return;
}
if level < logger.lowest_level { if level < logger.lowest_level {
return; return;
} }
+95 -56
View File
@@ -109,104 +109,141 @@ end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
Scratch_Allocator :: struct { Scratch_Allocator :: struct {
data: []byte, data: []byte,
curr_offset: int, curr_offset: int,
prev_offset: int, prev_allocation: rawptr,
backup_allocator: Allocator, backup_allocator: Allocator,
leaked_allocations: [dynamic]rawptr, leaked_allocations: [dynamic]rawptr,
default_to_default_allocator: bool,
} }
scratch_allocator_init :: proc(scratch: ^Scratch_Allocator, data: []byte, backup_allocator := context.allocator) { scratch_allocator_init :: proc(s: ^Scratch_Allocator, size: int, backup_allocator := context.allocator) {
scratch.data = data; s.data = make_aligned([]byte, size, 2*align_of(rawptr), backup_allocator);
scratch.curr_offset = 0; s.curr_offset = 0;
scratch.prev_offset = 0; s.prev_allocation = nil;
scratch.backup_allocator = backup_allocator; s.backup_allocator = backup_allocator;
s.leaked_allocations.allocator = backup_allocator;
} }
scratch_allocator_destroy :: proc(using scratch: ^Scratch_Allocator) { scratch_allocator_destroy :: proc(s: ^Scratch_Allocator) {
if scratch == nil { if s == nil {
return; return;
} }
for ptr in leaked_allocations { for ptr in s.leaked_allocations {
free(ptr, backup_allocator); free(ptr, s.backup_allocator);
} }
delete(leaked_allocations); delete(s.leaked_allocations);
delete(data, backup_allocator); delete(s.data, s.backup_allocator);
scratch^ = {}; s^ = {};
} }
scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr { old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
scratch := (^Scratch_Allocator)(allocator_data); s := (^Scratch_Allocator)(allocator_data);
if scratch.data == nil { if s.data == nil {
DEFAULT_SCRATCH_BACKING_SIZE :: 1<<22; DEFAULT_BACKING_SIZE :: 1<<22;
if !(context.allocator.procedure != scratch_allocator_proc && if !(context.allocator.procedure != scratch_allocator_proc &&
context.allocator.data != allocator_data) { context.allocator.data != allocator_data) {
panic("cyclic initialization of the scratch allocator with itself"); panic("cyclic initialization of the scratch allocator with itself");
} }
scratch_allocator_init(scratch, make([]byte, 1<<22)); scratch_allocator_init(s, DEFAULT_BACKING_SIZE);
} }
size := size;
switch mode { switch mode {
case .Alloc: case .Alloc:
size = align_forward_int(size, alignment);
switch { switch {
case scratch.curr_offset+size <= len(scratch.data): case s.curr_offset+size <= len(s.data):
offset := align_forward_uintptr(uintptr(scratch.curr_offset), uintptr(alignment)); start := uintptr(raw_data(s.data));
ptr := &scratch.data[offset]; ptr := start + uintptr(s.curr_offset);
zero(ptr, size); ptr = align_forward_uintptr(ptr, uintptr(alignment));
scratch.prev_offset = int(offset); zero(rawptr(ptr), size);
scratch.curr_offset = int(offset) + size;
return ptr; s.prev_allocation = rawptr(ptr);
case size <= len(scratch.data): offset := int(ptr - start);
offset := align_forward_uintptr(uintptr(0), uintptr(alignment)); s.curr_offset = offset + size;
ptr := &scratch.data[offset]; return rawptr(ptr);
zero(ptr, size);
scratch.prev_offset = int(offset); case size <= len(s.data):
scratch.curr_offset = int(offset) + size; start := uintptr(raw_data(s.data));
return ptr; ptr := align_forward_uintptr(start, uintptr(alignment));
zero(rawptr(ptr), size);
s.prev_allocation = rawptr(ptr);
offset := int(ptr - start);
s.curr_offset = offset + size;
return rawptr(ptr);
} }
// TODO(bill): Should leaks be notified about? Should probably use a logging system that is built into the context system a := s.backup_allocator;
a := scratch.backup_allocator;
if a.procedure == nil { if a.procedure == nil {
a = context.allocator; a = context.allocator;
scratch.backup_allocator = a; s.backup_allocator = a;
} }
ptr := alloc(size, alignment, a, loc); ptr := alloc(size, alignment, a, loc);
if scratch.leaked_allocations == nil { if s.leaked_allocations == nil {
scratch.leaked_allocations = make([dynamic]rawptr, a); s.leaked_allocations = make([dynamic]rawptr, a);
}
append(&s.leaked_allocations, ptr);
if logger := context.logger; logger.lowest_level <= .Warning {
if logger.procedure != nil {
logger.procedure(logger.data, .Warning, "mem.Scratch_Allocator resorted to backup_allocator" , logger.options, loc);
}
} }
append(&scratch.leaked_allocations, ptr);
return ptr; return ptr;
case .Free: case .Free:
last_ptr := rawptr(&scratch.data[scratch.prev_offset]); start := uintptr(raw_data(s.data));
if old_memory == last_ptr { end := start + uintptr(len(s.data));
full_size := scratch.curr_offset - scratch.prev_offset; old_ptr := uintptr(old_memory);
scratch.curr_offset = scratch.prev_offset;
zero(last_ptr, full_size); if s.prev_allocation == old_memory {
s.curr_offset = int(uintptr(s.prev_allocation) - uintptr(start));
s.prev_allocation = nil;
return nil; return nil;
} }
// NOTE(bill): It's scratch memory, don't worry about freeing
if start <= old_ptr && old_ptr < end {
// NOTE(bill): Cannot free this pointer but it is valid
return nil;
}
if len(s.leaked_allocations) != 0 {
for ptr, i in s.leaked_allocations {
if ptr == old_memory {
free(ptr, s.backup_allocator);
ordered_remove(&s.leaked_allocations, i);
return nil;
}
}
}
panic("invalid pointer passed to default_temp_allocator");
case .Free_All: case .Free_All:
scratch.curr_offset = 0; s.curr_offset = 0;
scratch.prev_offset = 0; s.prev_allocation = nil;
for ptr in scratch.leaked_allocations { for ptr in s.leaked_allocations {
free(ptr, scratch.backup_allocator); free(ptr, s.backup_allocator);
} }
clear(&scratch.leaked_allocations); clear(&s.leaked_allocations);
case .Resize: case .Resize:
last_ptr := rawptr(&scratch.data[scratch.prev_offset]); begin := uintptr(raw_data(s.data));
if old_memory == last_ptr && len(scratch.data)-scratch.prev_offset >= size { end := begin + uintptr(len(s.data));
scratch.curr_offset = scratch.prev_offset+size; old_ptr := uintptr(old_memory);
if begin <= old_ptr && old_ptr < end && old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size;
return old_memory; return old_memory;
} }
return scratch_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc); ptr := scratch_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, flags, loc);
copy(ptr, old_memory, old_size);
scratch_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, flags, loc);
return ptr;
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory); set := (^Allocator_Mode_Set)(old_memory);
@@ -219,19 +256,21 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
return nil; return nil;
} }
return nil; return nil;
} }
scratch_allocator :: proc(scratch: ^Scratch_Allocator) -> Allocator { scratch_allocator :: proc(allocator: ^Scratch_Allocator) -> Allocator {
return Allocator{ return Allocator{
procedure = scratch_allocator_proc, procedure = scratch_allocator_proc,
data = scratch, data = allocator,
}; };
} }
Stack_Allocation_Header :: struct { Stack_Allocation_Header :: struct {
prev_offset: int, prev_offset: int,
padding: int, padding: int,
@@ -941,7 +980,7 @@ small_allocator :: proc(s: ^$S/Small_Allocator, backing := context.allocator) ->
p := rawptr(s.curr); p := rawptr(s.curr);
s.curr += uintptr(size); s.curr += uintptr(size);
return p; return mem_zero(p, size);
case .Free: case .Free:
// NOP // NOP
+2 -2
View File
@@ -525,8 +525,8 @@ __init_context :: proc "contextless" (c: ^Context) {
} }
@builtin @builtin
init_global_temporary_allocator :: proc(data: []byte, backup_allocator := context.allocator) { init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
default_temp_allocator_init(&global_default_temp_allocator_data, data, backup_allocator); default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator);
} }
+84 -64
View File
@@ -27,127 +27,147 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR || ODIN_OS == "freestanding" {
} }
DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 1<<22);
Default_Temp_Allocator :: struct { Default_Temp_Allocator :: struct {
data: []byte, data: []byte,
curr_offset: int, curr_offset: int,
prev_offset: int, prev_allocation: rawptr,
backup_allocator: Allocator, backup_allocator: Allocator,
leaked_allocations: [dynamic]rawptr, leaked_allocations: [dynamic]rawptr,
} }
default_temp_allocator_init :: proc(allocator: ^Default_Temp_Allocator, data: []byte, backup_allocator := context.allocator) { default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {
allocator.data = data; s.data = make_aligned([]byte, size, 2*align_of(rawptr), backup_allocator);
allocator.curr_offset = 0; s.curr_offset = 0;
allocator.prev_offset = 0; s.prev_allocation = nil;
allocator.backup_allocator = backup_allocator; s.backup_allocator = backup_allocator;
allocator.leaked_allocations.allocator = backup_allocator; s.leaked_allocations.allocator = backup_allocator;
} }
default_temp_allocator_destroy :: proc(using allocator: ^Default_Temp_Allocator) { default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
if allocator == nil { if s == nil {
return; return;
} }
for ptr in leaked_allocations { for ptr in s.leaked_allocations {
free(ptr, backup_allocator); free(ptr, s.backup_allocator);
} }
delete(leaked_allocations); delete(s.leaked_allocations);
delete(data, backup_allocator); delete(s.data, s.backup_allocator);
allocator^ = {}; s^ = {};
} }
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr { old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
allocator := (^Default_Temp_Allocator)(allocator_data); s := (^Default_Temp_Allocator)(allocator_data);
if allocator.data == nil { if s.data == nil {
DEFAULT_SCRATCH_BACKING_SIZE :: 1<<22;
a := context.allocator; a := context.allocator;
if !(context.allocator.procedure != default_temp_allocator_proc && if !(context.allocator.procedure != default_temp_allocator_proc &&
context.allocator.data != allocator_data) { context.allocator.data != allocator_data) {
a = default_allocator(); a = default_allocator();
} }
default_temp_allocator_init(allocator, make([]byte, DEFAULT_SCRATCH_BACKING_SIZE, a), a); default_temp_allocator_init(s, DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, a);
} }
size := size;
switch mode { switch mode {
case .Alloc: case .Alloc:
size = align_forward_int(size, alignment);
switch { switch {
case allocator.curr_offset+size <= len(allocator.data): case s.curr_offset+size <= len(s.data):
offset := align_forward_uintptr(uintptr(allocator.curr_offset), uintptr(alignment)); start := uintptr(raw_data(s.data));
ptr := &allocator.data[offset]; ptr := start + uintptr(s.curr_offset);
mem_zero(ptr, size); ptr = align_forward_uintptr(ptr, uintptr(alignment));
allocator.prev_offset = int(offset); mem_zero(rawptr(ptr), size);
allocator.curr_offset = int(offset) + size;
return ptr; s.prev_allocation = rawptr(ptr);
case size <= len(allocator.data): offset := int(ptr - start);
offset := align_forward_uintptr(uintptr(0), uintptr(alignment)); s.curr_offset = offset + size;
ptr := &allocator.data[offset]; return rawptr(ptr);
mem_zero(ptr, size);
allocator.prev_offset = int(offset); case size <= len(s.data):
allocator.curr_offset = int(offset) + size; start := uintptr(raw_data(s.data));
return ptr; ptr := align_forward_uintptr(start, uintptr(alignment));
mem_zero(rawptr(ptr), size);
s.prev_allocation = rawptr(ptr);
offset := int(ptr - start);
s.curr_offset = offset + size;
return rawptr(ptr);
} }
// TODO(bill): Should leaks be notified about? Should probably use a logging system that is built into the context system a := s.backup_allocator;
a := allocator.backup_allocator;
if a.procedure == nil { if a.procedure == nil {
a = context.allocator; a = context.allocator;
allocator.backup_allocator = a; s.backup_allocator = a;
} }
ptr := mem_alloc(size, alignment, a, loc); ptr := mem_alloc(size, alignment, a, loc);
if allocator.leaked_allocations == nil { if s.leaked_allocations == nil {
allocator.leaked_allocations = make([dynamic]rawptr, a); s.leaked_allocations = make([dynamic]rawptr, a);
}
append(&s.leaked_allocations, ptr);
// TODO(bill): Should leaks be notified about?
if logger := context.logger; logger.lowest_level <= .Warning {
if logger.procedure != nil {
logger.procedure(logger.data, .Warning, "default temp allocator resorted to backup_allocator" , logger.options, loc);
}
} }
append(&allocator.leaked_allocations, ptr);
return ptr; return ptr;
case .Free: case .Free:
if len(allocator.data) == 0 { start := uintptr(raw_data(s.data));
return nil; end := start + uintptr(len(s.data));
} old_ptr := uintptr(old_memory);
last_ptr := rawptr(&allocator.data[allocator.prev_offset]);
if old_memory == last_ptr { if s.prev_allocation == old_memory {
allocator.curr_offset = allocator.prev_offset; s.curr_offset = int(uintptr(s.prev_allocation) - uintptr(start));
return nil; s.prev_allocation = nil;
} else {
#no_bounds_check start, end := &allocator.data[0], &allocator.data[allocator.curr_offset];
if start <= old_memory && old_memory < end {
// NOTE(bill): Cannot free this pointer
return nil; return nil;
} }
if len(allocator.leaked_allocations) != 0 { if start <= old_ptr && old_ptr < end {
for ptr, i in allocator.leaked_allocations { // NOTE(bill): Cannot free this pointer but it is valid
return nil;
}
if len(s.leaked_allocations) != 0 {
for ptr, i in s.leaked_allocations {
if ptr == old_memory { if ptr == old_memory {
free(ptr, allocator.backup_allocator); free(ptr, s.backup_allocator);
ordered_remove(&allocator.leaked_allocations, i); ordered_remove(&s.leaked_allocations, i);
return nil; return nil;
} }
} }
} }
} panic("invalid pointer passed to default_temp_allocator");
// NOTE(bill): It's a temporary memory, don't worry about freeing
case .Free_All: case .Free_All:
allocator.curr_offset = 0; s.curr_offset = 0;
allocator.prev_offset = 0; s.prev_allocation = nil;
for ptr in allocator.leaked_allocations { for ptr in s.leaked_allocations {
free(ptr, allocator.backup_allocator); free(ptr, s.backup_allocator);
} }
clear(&allocator.leaked_allocations); clear(&s.leaked_allocations);
case .Resize: case .Resize:
last_ptr := #no_bounds_check rawptr(&allocator.data[allocator.prev_offset]); begin := uintptr(raw_data(s.data));
if old_memory == last_ptr && len(allocator.data)-allocator.prev_offset >= size { end := begin + uintptr(len(s.data));
allocator.curr_offset = allocator.prev_offset+size; old_ptr := uintptr(old_memory);
if begin <= old_ptr && old_ptr < end && old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size;
return old_memory; return old_memory;
} }
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, .Alloc, size, alignment, old_memory, old_size, flags, loc);
mem_copy(ptr, old_memory, old_size); mem_copy(ptr, old_memory, old_size);
default_temp_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, flags, loc);
return ptr; return ptr;
case .Query_Features: case .Query_Features: