Removed implicit assignments for container allocators in the Base and Core packages

This commit is contained in:
2025-01-12 21:41:15 -05:00
parent 37ec315a91
commit 5a7b8b463b
11 changed files with 77 additions and 40 deletions
+11
View File
@@ -8,6 +8,17 @@ There were additions made for quality of life reasons:
* I added support for 'monlithic packages' or 'uniform-across-subdirectories packages'. It allows me to organize the main package with sub-directories. (Could be flag instead but making it a file flag works well)
* Added the ability to debug using statements on structs (fields get dumped to the stack as ptr refs)
* They show up as `struct_name::field_name`
* Remove implicit assignments for container allocators in the Base and Core packages
* I did not enjoy bug hunting a memory corruption because I mistakenly didn't properly initialize a core container with their designated initiatizer: new, make, or init.
* Affects the following:
* base:runtime/core_builtin.doin:`_reserve_dynamic_array`, `_resize_dynamic_array`, `_shrink_dynamic_array`
* base:runtime/core_builtin_soa.odin:`make_soa_aligned`, `_reserve_soa`
* base:runtime/default_temp_allocator_arena.odin:`arena_alloc`
* base:runtime/dynamic_array_internal.odin:`__dynamic_array_reserve`, `__dynamic_array_shrink`
* base:runtime/dynamic_map_internal.odin:`map_shrink_dynamic`
* core:flags/internal_assignment.doin:`set_key_value`
* core:mem/tracking_allocator.odin:`tracking_allocator_proc`
* core:strings/intern.odin:`_intern_get_entry`
---
+12 -9
View File
@@ -786,9 +786,10 @@ _reserve_dynamic_array :: #force_inline proc(a: ^Raw_Dynamic_Array, size_of_elem
return nil
}
if a.allocator.procedure == nil {
a.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if a.allocator.procedure == nil {
// a.allocator = context.allocator
// }
assert(a.allocator.procedure != nil)
old_size := a.cap * size_of_elem
@@ -836,9 +837,10 @@ _resize_dynamic_array :: #force_inline proc(a: ^Raw_Dynamic_Array, size_of_elem,
return nil
}
if a.allocator.procedure == nil {
a.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if a.allocator.procedure == nil {
// a.allocator = context.allocator
// }
assert(a.allocator.procedure != nil)
old_size := a.cap * size_of_elem
@@ -900,9 +902,10 @@ _shrink_dynamic_array :: proc(a: ^Raw_Dynamic_Array, size_of_elem, align_of_elem
return
}
if a.allocator.procedure == nil {
a.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if a.allocator.procedure == nil {
// a.allocator = context.allocator
// }
assert(a.allocator.procedure != nil)
old_size := a.cap * size_of_elem
+9 -7
View File
@@ -102,10 +102,11 @@ make_soa_aligned :: proc($T: typeid/#soa[]$E, #any_int length, alignment: int, a
total_size = align_forward_int(total_size, max_align)
}
allocator := allocator
if allocator.procedure == nil {
allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// allocator := allocator
// if allocator.procedure == nil {
// allocator = context.allocator
// }
assert(allocator.procedure != nil)
new_bytes: []byte
@@ -215,9 +216,10 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo
return nil
}
if array.allocator.procedure == nil {
array.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if array.allocator.procedure == nil {
// array.allocator = context.allocator
// }
assert(array.allocator.procedure != nil)
footer := raw_soa_footer(array)
@@ -113,9 +113,10 @@ arena_alloc :: proc(arena: ^Arena, size, alignment: uint, loc := #caller_locatio
block_size := max(needed, arena.minimum_block_size)
if arena.backing_allocator.procedure == nil {
arena.backing_allocator = default_allocator()
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if arena.backing_allocator.procedure == nil {
// arena.backing_allocator = default_allocator()
// }
new_block := memory_block_alloc(arena.backing_allocator, block_size, alignment, loc) or_return
new_block.prev = arena.curr_block
+10 -6
View File
@@ -16,9 +16,11 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap:
// NOTE(tetra, 2020-01-26): We set the allocator before earlying-out below, because user code is usually written
// assuming that appending/reserving will set the allocator, if it is not already set.
if array.allocator.procedure == nil {
array.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if array.allocator.procedure == nil {
// array.allocator = context.allocator
// }
assert(array.allocator.procedure != nil)
if cap <= array.cap {
@@ -50,9 +52,11 @@ __dynamic_array_shrink :: proc(array_: rawptr, elem_size, elem_align: int, new_c
// NOTE(tetra, 2020-01-26): We set the allocator before earlying-out below, because user code is usually written
// assuming that appending/reserving will set the allocator, if it is not already set.
if array.allocator.procedure == nil {
array.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if array.allocator.procedure == nil {
// array.allocator = context.allocator
// }
assert(array.allocator.procedure != nil)
if new_cap > array.cap {
+10 -6
View File
@@ -587,9 +587,11 @@ map_reserve_dynamic :: #force_no_inline proc "odin" (#no_alias m: ^Raw_Map, #no_
return size_of(uintptr)*8 - 1 - z
}
if m.allocator.procedure == nil {
m.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if m.allocator.procedure == nil {
// m.allocator = context.allocator
// }
assert( m.allocator.procedure != nil)
new_capacity := new_capacity
old_capacity := uintptr(map_cap(m^))
@@ -642,9 +644,11 @@ map_reserve_dynamic :: #force_no_inline proc "odin" (#no_alias m: ^Raw_Map, #no_
@(require_results)
map_shrink_dynamic :: #force_no_inline proc "odin" (#no_alias m: ^Raw_Map, #no_alias info: ^Map_Info, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
if m.allocator.procedure == nil {
m.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if m.allocator.procedure == nil {
// m.allocator = context.allocator
// }
assert( m.allocator.procedure != nil)
// Cannot shrink the capacity if the number of items in the map would exceed
// one minus the current log2 capacity's resize threshold. That is the shrunk
+2
View File
@@ -692,6 +692,8 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header,
case reflect.Type_Info_Map:
raw_map := (^mem.Raw_Map)(v.data)
if raw_map.allocator.procedure == nil {
// Note(Ed) - Sectr Fork: I'm premmtively leaving this here incase it happens...
runtime.debug_trap()
raw_map.allocator = context.allocator
}
+2
View File
@@ -526,6 +526,8 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm
}
raw_map := (^mem.Raw_Map)(v.data)
if raw_map.allocator.procedure == nil {
// Note(Ed) - Sectr Fork: I'm premmtively leaving this here incase it happens...
runtime.debug_trap()
raw_map.allocator = p.allocator
}
+8 -3
View File
@@ -203,9 +203,14 @@ set_key_value :: proc(model: ^$T, parser: ^Parser, name, key, value: string) ->
elem_backing: []byte
value_ptr: rawptr
if raw_map.allocator.procedure == nil {
raw_map.allocator = context.allocator
} else {
assert(raw_map.allocator.procedure != nil)
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if raw_map.allocator.procedure == nil {
// raw_map.allocator = context.allocator
// }
// else
{
value_ptr = runtime.__dynamic_map_get(raw_map,
specific_type_info.map_info,
hash,
+5 -3
View File
@@ -232,9 +232,11 @@ tracking_allocator_proc :: proc(
}
result_ptr := raw_data(result)
if data.allocation_map.allocator.procedure == nil {
data.allocation_map.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if data.allocation_map.allocator.procedure == nil {
// data.allocation_map.allocator = context.allocator
// }
assert(data.allocation_map.allocator.procedure != nil)
switch mode {
case .Alloc, .Alloc_Non_Zeroed:
+4 -3
View File
@@ -105,9 +105,10 @@ Returns:
- err: An allocator error if one occured, `nil` otherwise
*/
_intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check {
if m.allocator.procedure == nil {
m.allocator = context.allocator
}
// Note(Ed) - Sectr Fork: Not a fan. (I want it to assert)
// if m.allocator.procedure == nil {
// m.allocator = context.allocator
// }
key_ptr, val_ptr, inserted := map_entry(&m.entries, text) or_return
if !inserted {