mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-31 11:50:07 +00:00
[runtime] Add builtin shrink for dynamic arrays and maps
Asks the allocator to shrink the backing allocation to the current __length__, or a capacity of the user's choosing. Returns `(did_shrink: bool, err: mem.Allocator_Error)`. ``` shrink(&array) // shrinks to len(array) shrink(&array, N) // shrink to N capacity shrink(&map) // shrinks down to len(map) shrink(&map, N) // shrink to N capacity ```
This commit is contained in:
@@ -239,6 +239,16 @@ __dynamic_map_reserve :: proc(using header: Map_Header, cap: int, loc := #caller
|
||||
}
|
||||
}
|
||||
|
||||
__dynamic_map_shrink :: proc(using header: Map_Header, cap: int, loc := #caller_location) -> (did_shrink: bool) {
|
||||
c := context
|
||||
if m.entries.allocator.procedure != nil {
|
||||
c.allocator = m.entries.allocator
|
||||
}
|
||||
context = c
|
||||
|
||||
return __dynamic_array_shrink(&m.entries, entry_size, entry_align, cap, loc)
|
||||
}
|
||||
|
||||
__dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #caller_location) {
|
||||
#force_inline __dynamic_map_reserve(header, new_count, loc)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user