core/mem/virtual
mem_virtual
Types
12Arena
Arena :: struct {
kind: Arena_Kind,
curr_block: ^Memory_Block,
total_used: uint,
total_reserved: uint,
default_commit_size: uint,
minimum_block_size: uint,
temp_count: uint,
mutex: sync.Mutex,
}SourceArena is a generalized arena allocator that supports 3 different variants.
Growing: A linked list of `Memory_Block`s allocated with virtual memory.
Static: A single `Memory_Block` allocated with virtual memory.
Buffer: A single `Memory_Block` created from a user provided []byte.Arena_Kind
Arena_Kind :: enum uint {
Growing = 0, // Chained memory blocks (singly linked list).
Static = 1, // Fixed reservation sized.
Buffer = 2, // Uses a fixed sized buffer.
}Sourceimport "base:sanitizer"
Arena_Temp
Arena_Temp :: struct {
arena: ^Arena,
block: ^Memory_Block,
used: uint,
}SourceAn Arena_Temp is a way to produce temporary watermarks to reset an arena to a previous state. All uses of an Arena_Temp must be handled by ending them with arena_temp_end or ignoring them with arena_temp_ignore.
Map_File_Error
Map_File_Error :: enum int {
None = 0,
Open_Failure = 1,
Stat_Failure = 2,
Negative_Size = 3,
Too_Large_Size = 4,
Map_Failure = 5,
}SourceMap_File_Flag
Map_File_Flag :: enum u32 {
Read = 0,
Write = 1,
}SourceMap_File_Flags
Map_File_Flags :: bit_set[Map_File_Flag; u32]SourceMemory_Block
Memory_Block :: struct {
prev: ^Memory_Block,
base: [^]u8,
used: uint,
committed: uint,
reserved: uint,
}SourceMemory_Block_Flag
Memory_Block_Flag :: enum u32 {
Overflow_Protection = 0,
}SourceMemory_Block_Flags
Memory_Block_Flags :: bit_set[Memory_Block_Flag; u32]SourcePlatform_Memory_Block
Platform_Memory_Block :: struct {
block: Memory_Block,
committed: uint,
reserved: uint,
}SourceProtect_Flag
Protect_Flag :: enum u32 {
Read = 0,
Write = 1,
Execute = 2,
}SourceProtect_Flags
Protect_Flags :: bit_set[Protect_Flag; u32]SourceConstants
5Allocator_Error
Allocator_Error :: Allocator_ErrorSourceDEFAULT_ARENA_GROWING_COMMIT_SIZE
DEFAULT_ARENA_GROWING_COMMIT_SIZE :: _ = 8*mem.MegabyteSourceDEFAULT_ARENA_GROWING_MINIMUM_BLOCK_SIZE
DEFAULT_ARENA_GROWING_MINIMUM_BLOCK_SIZE :: DEFAULT_ARENA_STATIC_COMMIT_SIZESourceDEFAULT_ARENA_STATIC_COMMIT_SIZE
DEFAULT_ARENA_STATIC_COMMIT_SIZE :: MegabyteSource1 MiB should be enough to start with
Protect_No_Access
Protect_No_Access :: Protect_Flags = Protect_Flags{}SourceProcedures
46_commit
_commit :: proc(data: rawptr, size: uint) -> (Allocator_Error)Source_decommit
_decommit :: proc(data: rawptr, size: uint)Source_map_file
_map_file :: proc(fd: uintptr, size: i64, flags: Map_File_Flags) -> (data: []u8, error: Map_File_Error)Source_protect
_protect :: proc(data: rawptr, size: uint, flags: Protect_Flags) -> (bool)Source_release
_release :: proc(data: rawptr, size: uint)Source_reserve
_reserve :: proc(size: uint, address_hint: uintptr) -> (data: []u8, err: Allocator_Error)Source_unmap_file
_unmap_file :: proc(data: []u8)Sourcealloc_from_memory_block
alloc_from_memory_block :: proc(block: ^Memory_Block, min_size: uint, alignment: uint, default_commit_size: uint) -> (data: []u8, err: Allocator_Error)Sourcearena_alloc
arena_alloc :: proc(arena: ^Arena, size: uint, alignment: uint, loc = #caller_location) -> (data: []u8, err: Allocator_Error)SourceAllocates memory from the provided arena.
arena_allocator
arena_allocator :: proc(arena: ^Arena) -> (mem.Allocator)SourceCreate an Allocator from the provided Arena
arena_allocator_proc
arena_allocator_proc :: proc(
allocator_data: rawptr,
mode: mem.Allocator_Mode,
size: int,
alignment: int,
old_memory: rawptr,
old_size: int,
location: _ = #caller_location,
) -> (data: []u8, err: Allocator_Error)SourceThe allocator procedure used by an Allocator produced by arena_allocator
arena_check_temp
arena_check_temp :: proc(arena: ^Arena, loc = #caller_location)SourceAsserts that all uses of Arena_Temp has been used by an Arena
arena_destroy
arena_destroy :: proc(arena: ^Arena, loc = #caller_location)SourceFrees all of the memory allocated by the arena and zeros all of the values of an arena. A buffer based arena does not delete the provided []byte bufffer.
arena_free_all
arena_free_all :: proc(arena: ^Arena, loc = #caller_location)SourceDeallocates all but the first memory block of the arena and resets the allocator's usage to 0.
arena_growing_bootstrap_new_by_name
arena_growing_bootstrap_new_by_name :: proc(T: typeid, field_name: string, minimum_block_size: uint) -> (ptr: ^T, err: Allocator_Error)SourceAbility to bootstrap allocate a struct with an arena within the struct itself using the growing variant strategy.
arena_growing_bootstrap_new_by_offset
arena_growing_bootstrap_new_by_offset :: proc(T: typeid, offset_to_arena: uintptr, minimum_block_size: uint) -> (ptr: ^T, err: Allocator_Error)SourceAbility to bootstrap allocate a struct with an arena within the struct itself using the growing variant strategy.
arena_growing_free_last_memory_block
arena_growing_free_last_memory_block :: proc(arena: ^Arena, loc = #caller_location)SourceFrees the last memory block of a Growing Arena
arena_init_buffer
arena_init_buffer :: proc(arena: ^Arena, buffer: []u8) -> (err: Allocator_Error)SourceInitialization of an Arena to be a .Buffer variant. A buffer arena contains single Memory_Block created from a user provided []byte.
arena_init_growing
arena_init_growing :: proc(arena: ^Arena, reserved: uint) -> (err: Allocator_Error)SourceInitialization of an Arena to be a .Growing variant. A growing arena is a linked list of Memory_Blocks allocated with virtual memory.
arena_init_static
arena_init_static :: proc(arena: ^Arena, reserved: uint, commit_size: uint) -> (err: Allocator_Error)SourceInitialization of an Arena to be a .Static variant. A static arena contains a single Memory_Block allocated with virtual memory.
arena_static_bootstrap_new_by_name
arena_static_bootstrap_new_by_name :: proc(T: typeid, field_name: string, reserved: uint) -> (ptr: ^T, err: Allocator_Error)SourceAbility to bootstrap allocate a struct with an arena within the struct itself using the static variant strategy.
arena_static_bootstrap_new_by_offset
arena_static_bootstrap_new_by_offset :: proc(T: typeid, offset_to_arena: uintptr, reserved: uint) -> (ptr: ^T, err: Allocator_Error)SourceAbility to bootstrap allocate a struct with an arena within the struct itself using the static variant strategy.
arena_static_reset_to
arena_static_reset_to :: proc(arena: ^Arena, pos: uint, loc = #caller_location) -> (bool)SourceResets the memory of a Static or Buffer arena to a specific position (offset) and zeroes the previously used memory.
arena_temp_begin
arena_temp_begin :: proc(arena: ^Arena, loc = #caller_location) -> (temp: Arena_Temp)SourceBegins the section of temporary arena memory.
arena_temp_end
arena_temp_end :: proc(temp: Arena_Temp, loc = #caller_location)SourceEnds the section of temporary arena memory by resetting the memory to the stored position.
arena_temp_ignore
arena_temp_ignore :: proc(temp: Arena_Temp, loc = #caller_location)SourceIgnore the use of a arena_temp_begin entirely by __not__ resetting to the stored position.
commit
commit :: proc(data: rawptr, size: uint) -> (Allocator_Error)Sourcedecommit
decommit :: proc(data: rawptr, size: uint)Sourcemake_aligned
make_aligned :: proc(arena: ^Arena, T: typeid, len: int, alignment: uint, loc = #caller_location) -> (Allocator_Error, T)Sourcemake_aligned allocates and initializes a slice. Like new, the second argument is a type, not a value. Unlike new, make's return value is the same as the type of its argument, not a pointer to it.
Note: Prefer using the procedure group make.
make_multi_pointer
make_multi_pointer :: proc(arena: ^Arena, T: typeid, len: int, loc = #caller_location) -> (Allocator_Error, T)Sourcemake_multi_pointer allocates and initializes a dynamic array. Like new, the second argument is a type, not a value. Unlike new, make's return value is the same as the type of its argument, not a pointer to it.
This is "similar" to doing raw_data(make([]E, len, allocator)).
Note: Prefer using the procedure group make.
make_slice
make_slice :: proc(arena: ^Arena, T: typeid, len: int, loc = #caller_location) -> (Allocator_Error, T)Sourcemake_slice allocates and initializes a slice. Like new, the second argument is a type, not a value. Unlike new, make's return value is the same as the type of its argument, not a pointer to it.
Note: Prefer using the procedure group make.
map_file_from_file
map_file_from_file :: proc(f: ^os.File, flags: Map_File_Flags) -> (data: []u8, error: Map_File_Error)Sourcemap_file_from_path
map_file_from_path :: proc(filename: string, flags: Map_File_Flags) -> (data: []u8, error: Map_File_Error)Sourcememory_block_alloc
memory_block_alloc :: proc(committed: uint, reserved: uint, alignment: uint, flags: Memory_Block_Flags) -> (block: ^Memory_Block, err: Allocator_Error)Sourcememory_block_dealloc
memory_block_dealloc :: proc(block_to_free: ^Memory_Block)Sourcenew
new :: proc(arena: ^Arena, T: typeid, loc = #caller_location) -> (ptr: ^T, err: Allocator_Error)SourceThe new procedure allocates memory for a type T from a virtual.Arena. The second argument is a type, not a value, and the value return is a pointer to a newly allocated value of that type using the specified allocator.
new_aligned
new_aligned :: proc(arena: ^Arena, T: typeid, alignment: uint, loc = #caller_location) -> (ptr: ^T, err: Allocator_Error)SourceThe new_aligned procedure allocates memory for a type T from a virtual.Arena with a specified alignment. The second argument is a type, not a value, and the value return is a pointer to a newly allocated value of that type using the specified allocator.
new_clone
new_clone :: proc(arena: ^Arena, data: T, loc = #caller_location) -> (ptr: ^T, err: Allocator_Error)SourceThe new_clone procedure allocates memory for a type T from a virtual.Arena. The second argument is a value that is to be copied to the allocated data. The value returned is a pointer to a newly allocated value of that type using the specified allocator.
platform_memory_alloc
platform_memory_alloc :: proc(to_commit: uint, to_reserve: uint) -> (block: ^Platform_Memory_Block, err: Allocator_Error)Sourceplatform_memory_commit
platform_memory_commit :: proc(block: ^Platform_Memory_Block, to_commit: uint) -> (err: Allocator_Error)Sourceplatform_memory_free
platform_memory_free :: proc(block: ^Platform_Memory_Block)Sourceprotect
protect :: proc(data: rawptr, size: uint, flags: Protect_Flags) -> (bool)Sourcerelease
release :: proc(data: rawptr, size: uint)Sourcereserve
reserve :: proc(size: uint, address_hint: uintptr = uintptr(0)) -> (data: []u8, err: Allocator_Error)Sourcereserve_and_commit
reserve_and_commit :: proc(size: uint) -> (data: []u8, err: Allocator_Error)Sourceunmap_file
unmap_file :: proc(data: []u8)SourceProcedure Groups
4arena_growing_bootstrap_new
arena_growing_bootstrap_new :: proc{arena_growing_bootstrap_new_by_offset, arena_growing_bootstrap_new_by_name}SourceAbility to bootstrap allocate a struct with an arena within the struct itself using the growing variant strategy.
arena_static_bootstrap_new
arena_static_bootstrap_new :: proc{arena_static_bootstrap_new_by_offset, arena_static_bootstrap_new_by_name}SourceAbility to bootstrap allocate a struct with an arena within the struct itself using the static variant strategy.
make
make :: proc{make_slice, make_multi_pointer}Sourcemap_file
map_file :: proc{map_file_from_path, map_file_from_file}Source