2024-02-08 07:50:36 -08:00
|
|
|
// TODO(Ed) : Move this to a grime package problably
|
2024-01-21 10:35:52 -08:00
|
|
|
package sectr
|
|
|
|
|
2024-01-21 20:38:02 -08:00
|
|
|
import "core:fmt"
|
|
|
|
import "core:mem"
|
|
|
|
import "core:mem/virtual"
|
|
|
|
import "core:runtime"
|
2024-01-29 22:29:48 -08:00
|
|
|
import "core:os"
|
2024-01-21 20:38:02 -08:00
|
|
|
|
2024-01-29 22:29:48 -08:00
|
|
|
// Initialize a sub-section of our virtual memory as a sub-arena
|
2024-01-29 23:27:45 -08:00
|
|
|
sub_arena_init :: proc( address : ^ byte, size : int ) -> ( ^ Arena) {
|
2024-01-29 22:29:48 -08:00
|
|
|
Arena :: mem.Arena
|
|
|
|
|
|
|
|
arena_size :: size_of( Arena)
|
|
|
|
sub_arena := cast( ^ Arena ) address
|
2024-01-29 23:27:45 -08:00
|
|
|
mem_slice := slice_ptr( ptr_offset( address, arena_size), size )
|
|
|
|
arena_init( sub_arena, mem_slice )
|
2024-01-29 22:29:48 -08:00
|
|
|
return sub_arena
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to get the the beginning of memory after a slice
|
|
|
|
memory_after :: proc( slice : []byte ) -> ( ^ byte) {
|
2024-01-29 23:27:45 -08:00
|
|
|
return ptr_offset( & slice[0], len(slice) )
|
2024-01-29 22:29:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since this is a prototype, all memory is always tracked. No arena is is interfaced directly.
|
|
|
|
TrackedAllocator :: struct {
|
2024-01-29 23:27:45 -08:00
|
|
|
backing : Arena,
|
|
|
|
internals : Arena,
|
|
|
|
tracker : Tracking_Allocator,
|
2024-01-29 22:29:48 -08:00
|
|
|
}
|
|
|
|
|
2024-01-29 23:27:45 -08:00
|
|
|
tracked_allocator :: proc ( self : ^ TrackedAllocator ) -> Allocator {
|
|
|
|
return tracking_allocator( & self.tracker )
|
2024-01-29 22:29:48 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 19:33:53 -08:00
|
|
|
// TODO(Ed): These allocators are bad... not sure why.
|
2024-01-29 22:54:37 -08:00
|
|
|
tracked_allocator_init :: proc( size, internals_size : int, allocator := context.allocator ) -> TrackedAllocator
|
2024-01-29 22:29:48 -08:00
|
|
|
{
|
|
|
|
result : TrackedAllocator
|
|
|
|
|
2024-01-29 23:27:45 -08:00
|
|
|
arena_size :: size_of( Arena)
|
|
|
|
backing_size := size + arena_size
|
|
|
|
internals_size := internals_size + arena_size
|
|
|
|
raw_size := backing_size + internals_size
|
2024-01-29 22:29:48 -08:00
|
|
|
|
2024-01-29 23:27:45 -08:00
|
|
|
raw_mem, raw_mem_code := alloc( raw_size, mem.DEFAULT_ALIGNMENT, allocator )
|
2024-02-08 19:33:53 -08:00
|
|
|
verify( raw_mem_code != mem.Allocator_Error.None, "Failed to allocate memory for the TrackingAllocator" )
|
|
|
|
|
|
|
|
backing_slice := slice_ptr( cast( ^ byte) raw_mem, backing_size )
|
|
|
|
internals_slice := slice_ptr( memory_after( backing_slice), internals_size )
|
|
|
|
|
|
|
|
arena_init( & result.backing, backing_slice )
|
|
|
|
arena_init( & result.internals, internals_slice )
|
2024-01-29 22:29:48 -08:00
|
|
|
|
|
|
|
backing_allocator := arena_allocator( & result.backing )
|
|
|
|
internals_allocator := arena_allocator( & result.internals )
|
2024-01-29 23:27:45 -08:00
|
|
|
tracking_allocator_init( & result.tracker, backing_allocator, internals_allocator )
|
2024-02-08 19:33:53 -08:00
|
|
|
{
|
|
|
|
tracker_arena := cast(^Arena) result.tracker.backing.data
|
|
|
|
arena_len := len( tracker_arena.data )
|
|
|
|
verify( arena_len != len(result.backing.data), "BAD SIZE ON TRACKER'S ARENA" )
|
|
|
|
}
|
2024-01-29 22:29:48 -08:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
tracked_allocator_init_vmem :: proc( vmem : [] byte, internals_size : int ) -> ^ TrackedAllocator
|
|
|
|
{
|
|
|
|
arena_size :: size_of( Arena)
|
|
|
|
tracking_allocator_size :: size_of( Tracking_Allocator )
|
|
|
|
backing_size := len(vmem) - internals_size
|
|
|
|
raw_size := backing_size + internals_size
|
|
|
|
|
2024-02-08 19:33:53 -08:00
|
|
|
verify( backing_size < 0 || len(vmem) < raw_size, "Provided virtual memory slice is not large enough to hold the TrackedAllocator" )
|
2024-01-29 22:29:48 -08:00
|
|
|
|
|
|
|
result := cast( ^ TrackedAllocator) & vmem[0]
|
|
|
|
result_slice := slice_ptr( & vmem[0], tracking_allocator_size )
|
|
|
|
|
|
|
|
backing_slice := slice_ptr( memory_after( result_slice ), backing_size )
|
|
|
|
internals_slice := slice_ptr( memory_after( backing_slice), internals_size )
|
|
|
|
backing := & result.backing
|
|
|
|
internals := & result.internals
|
|
|
|
arena_init( backing, backing_slice )
|
|
|
|
arena_init( internals, internals_slice )
|
|
|
|
|
2024-01-29 23:27:45 -08:00
|
|
|
tracking_allocator_init( & result.tracker, arena_allocator( backing ), arena_allocator( internals ) )
|
2024-01-29 22:29:48 -08:00
|
|
|
return result
|
2024-01-21 10:35:52 -08:00
|
|
|
}
|