2024-03-05 07:41:42 -08:00
|
|
|
/*
|
|
|
|
The default arena allocator Odin provides does fragmented resizes even for the last most allocated block getting resized.
|
|
|
|
This is an alternative to alleviates that.
|
2024-05-04 08:47:21 -07:00
|
|
|
|
|
|
|
TODO(Ed): Implement?
|
2024-03-05 07:41:42 -08:00
|
|
|
*/
|
|
|
|
package sectr
|
|
|
|
|
2024-03-06 19:40:45 -08:00
|
|
|
import "core:mem"
|
|
|
|
|
|
|
|
// Initialize a sub-section of our virtual memory as a sub-arena
|
2024-03-20 19:34:18 -07:00
|
|
|
sub_arena_init :: proc( address : ^byte, size : int ) -> ( ^ Arena) {
|
2024-03-06 19:40:45 -08:00
|
|
|
Arena :: mem.Arena
|
|
|
|
|
|
|
|
arena_size :: size_of( Arena)
|
|
|
|
sub_arena := cast( ^ Arena ) address
|
|
|
|
mem_slice := slice_ptr( ptr_offset( address, arena_size), size )
|
|
|
|
arena_init( sub_arena, mem_slice )
|
|
|
|
return sub_arena
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(Ed) : Once this is done (ArenaFixed), rename to just Arena as we're not going to use the core implementation
|
|
|
|
|
2024-03-05 07:41:42 -08:00
|
|
|
ArenaFixedHeader :: struct {
|
|
|
|
data : []byte,
|
|
|
|
offset : uint,
|
|
|
|
peak_used : uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
ArenaFixed :: struct {
|
|
|
|
using header : ^ArenaFixedHeader,
|
|
|
|
}
|
|
|
|
|
|
|
|
arena_fixed_init :: proc( backing : []byte ) -> (arena : ArenaFixed) {
|
|
|
|
header_size := size_of(ArenaFixedHeader)
|
|
|
|
|
|
|
|
verify(len(backing) >= (header_size + Kilobyte), "Attempted to init an arena with less than kilobyte of memory...")
|
|
|
|
|
|
|
|
arena.header = cast(^ArenaFixedHeader) raw_data(backing)
|
|
|
|
using arena.header
|
|
|
|
data_ptr := cast([^]byte) (cast( [^]ArenaFixedHeader) arena.header)[ 1:]
|
|
|
|
data = slice_ptr( data_ptr, len(backing) - header_size )
|
|
|
|
offset = 0
|
|
|
|
peak_used = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
arena_fixed_allocator_proc :: proc(
|
|
|
|
allocator_data : rawptr,
|
|
|
|
mode : AllocatorMode,
|
|
|
|
size : int,
|
|
|
|
alignment : int,
|
|
|
|
old_memory : rawptr,
|
|
|
|
old_size : int,
|
|
|
|
location := #caller_location
|
|
|
|
) -> ([]byte, AllocatorError)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return nil, .Out_Of_Memory
|
|
|
|
}
|
|
|
|
|