Memory management module

Contents

Consists of pointer arithmetic methods, virtual memory management and custom memory allocators.

Classes

struct zpl_virtual_memory
struct zpl_allocator
struct zpl_arena
struct zpl_temp_arena_memory
struct zpl_pool
struct zpl_allocation_header_ev
struct zpl_scratch_memory
struct zpl_stack_memory

Typedefs

using zpl_virtual_memory = struct zpl_virtual_memory
using zplAllocationType = enum zplAllocationType
using zpl_allocator_proc = void*(void*allocator_data, zplAllocationType type, zpl_isize size, zpl_isize alignment, void*old_memory, zpl_isize old_size, zpl_u64 flags)
using zpl_allocator = struct zpl_allocator
using zplAllocatorFlag = enum zplAllocatorFlag
using zpl_arena = struct zpl_arena
using zpl_temp_arena_memory = struct zpl_temp_arena_memory
using zpl_pool = struct zpl_pool
using zpl_allocation_header_ev = struct zpl_allocation_header_ev
using zpl_scratch_memory = struct zpl_scratch_memory
using zpl_stack_memory = struct zpl_stack_memory

Functions

auto zpl_is_power_of_two(zpl_isize x) -> zpl_b32
Checks if value is power of 2.
auto zpl_align_forward(void* ptr, zpl_isize alignment) -> void*
Aligns address to specified alignment.
auto zpl_pointer_add(void* ptr, zpl_isize bytes) -> void*
Moves pointer forward by bytes.
auto zpl_pointer_sub(void* ptr, zpl_isize bytes) -> void*
Moves pointer backward by bytes.
auto zpl_pointer_add_const(void const* ptr, zpl_isize bytes) -> void const *
Moves pointer forward by bytes.
auto zpl_pointer_sub_const(void const* ptr, zpl_isize bytes) -> void const *
Moves pointer backward by bytes.
auto zpl_pointer_diff(void const* begin, void const* end) -> zpl_isize
Calculates difference between two addresses.
auto zpl_zero_size(void* ptr, zpl_isize size) -> void
Clears up memory at location by specified size.
auto zpl_memcopy(void* dest, void const* source, zpl_isize size) -> void*
Copy non-overlapping memory from source to destination.
auto zpl_memmove(void* dest, void const* source, zpl_isize size) -> void*
Copy memory from source to destination.
auto zpl_memset(void* data, zpl_u8 byte_value, zpl_isize size) -> void*
Set constant value at memory location with specified size.
auto zpl_memcompare(void const* s1, void const* s2, zpl_isize size) -> zpl_i32
Compare two memory locations with specified size.
auto zpl_memswap(void* i, void* j, zpl_isize size) -> void
Swap memory contents between 2 locations with size.
auto zpl_memchr(void const* data, zpl_u8 byte_value, zpl_isize size) -> void const *
Search for a constant value within the size limit at memory location.
auto zpl_memrchr(void const* data, zpl_u8 byte_value, zpl_isize size) -> void const *
Search for a constant value within the size limit at memory location in backwards.
auto zpl_vm(void* data, zpl_isize size) -> zpl_virtual_memory
Initialize virtual memory from existing data.
auto zpl_vm_alloc(void* addr, zpl_isize size) -> zpl_virtual_memory
Allocate virtual memory at address with size.
auto zpl_vm_free(zpl_virtual_memory vm) -> zpl_b32
Release the virtual memory.
auto zpl_vm_trim(zpl_virtual_memory vm, zpl_isize lead_size, zpl_isize size) -> zpl_virtual_memory
Trim virtual memory.
auto zpl_vm_purge(zpl_virtual_memory vm) -> zpl_b32
Purge virtual memory.
auto zpl_virtual_memory_page_size(zpl_isize* alignment_out) -> zpl_isize
Retrieve VM's page size and alignment.
auto zpl_alloc_align(zpl_allocator a, zpl_isize size, zpl_isize alignment) -> void*
Allocate memory with specified alignment.
auto zpl_alloc(zpl_allocator a, zpl_isize size) -> void*
Allocate memory with default alignment.
auto zpl_free(zpl_allocator a, void* ptr) -> void
Free allocated memory.
auto zpl_free_all(zpl_allocator a) -> void
Free all memory allocated by an allocator.
auto zpl_resize(zpl_allocator a, void* ptr, zpl_isize old_size, zpl_isize new_size) -> void*
Resize an allocated memory.
auto zpl_resize_align(zpl_allocator a, void* ptr, zpl_isize old_size, zpl_isize new_size, zpl_isize alignment) -> void*
Resize an allocated memory with specified alignment.
auto zpl_alloc_copy(zpl_allocator a, void const* src, zpl_isize size) -> void*
Allocate memory and copy data into it.
auto zpl_alloc_copy_align(zpl_allocator a, void const* src, zpl_isize size, zpl_isize alignment) -> void*
Allocate memory with specified alignment and copy data into it.
auto zpl_alloc_str(zpl_allocator a, char const* str) -> char*
Allocate memory for null-terminated C-String.
auto zpl_alloc_str_len(zpl_allocator a, char const* str, zpl_isize len) -> char*
Allocate memory for C-String with specified size.
auto zpl_default_resize_align(zpl_allocator a, void* ptr, zpl_isize old_size, zpl_isize new_size, zpl_isize alignment) -> void*
Allocate/Resize memory using default options.
auto zpl_heap_allocator(void) -> zpl_allocator
The heap allocator backed by operating system's memory manager.
auto zpl_heap_allocator_proc(void* allocator_data, zplAllocationType type, zpl_isize size, zpl_isize alignment, void* old_memory, zpl_isize old_size, zpl_u64 flags) -> void*
auto zpl_arena_init_from_memory(zpl_arena* arena, void* start, zpl_isize size) -> void
Initialize memory arena from existing memory region.
auto zpl_arena_init_from_allocator(zpl_arena* arena, zpl_allocator backing, zpl_isize size) -> void
Initialize memory arena using existing memory allocator.
auto zpl_arena_init_sub(zpl_arena* arena, zpl_arena* parent_arena, zpl_isize size) -> void
Initialize memory arena within an existing parent memory arena.
auto zpl_arena_free(zpl_arena* arena) -> void
Release the memory used by memory arena.
auto zpl_arena_alignment_of(zpl_arena* arena, zpl_isize alignment) -> zpl_isize
Retrieve memory arena's aligned allocation address.
auto zpl_arena_size_remaining(zpl_arena* arena, zpl_isize alignment) -> zpl_isize
Retrieve memory arena's remaining size.
auto zpl_arena_check(zpl_arena* arena) -> void
Check whether memory arena has any temporary snapshots.
auto zpl_arena_allocator(zpl_arena* arena) -> zpl_allocator
Allocation Types: alloc, free_all, resize.
auto zpl_arena_allocator_proc(void* allocator_data, zplAllocationType type, zpl_isize size, zpl_isize alignment, void* old_memory, zpl_isize old_size, zpl_u64 flags) -> void*
auto zpl_temp_arena_memory_begin(zpl_arena* arena) -> zpl_temp_arena_memory
Capture a snapshot of used memory in a memory arena.
auto zpl_temp_arena_memory_end(zpl_temp_arena_memory tmp_mem) -> void
Reset memory arena's usage by a captured snapshot.
auto zpl_pool_init(zpl_pool* pool, zpl_allocator backing, zpl_isize num_blocks, zpl_isize block_size) -> void
Initialize pool allocator.
auto zpl_pool_init_align(zpl_pool* pool, zpl_allocator backing, zpl_isize num_blocks, zpl_isize block_size, zpl_isize block_align) -> void
Initialize pool allocator with specific block alignment.
auto zpl_pool_free(zpl_pool* pool) -> void
Release the resources used by pool allocator.
auto zpl_pool_allocator(zpl_pool* pool) -> zpl_allocator
Allocation Types: alloc, free.
auto zpl_pool_allocator_proc(void* allocator_data, zplAllocationType type, zpl_isize size, zpl_isize alignment, void* old_memory, zpl_isize old_size, zpl_u64 flags) -> void*
auto zpl_allocation_header(void* data) -> zpl_allocation_header_ev*
auto zpl_allocation_header_fill(zpl_allocation_header_ev* header, void* data, zpl_isize size) -> void
auto zpl_scratch_memory_init(zpl_scratch_memory* s, void* start, zpl_isize size) -> void
Initialize ring buffer arena.
auto zpl_scratch_memory_is_in_use(zpl_scratch_memory* s, void* ptr) -> zpl_b32
Check whether ring buffer arena is in use.
auto zpl_scratch_allocator(zpl_scratch_memory* s) -> zpl_allocator
Allocation Types: alloc, free, free_all, resize.
auto zpl_scratch_allocator_proc(void* allocator_data, zplAllocationType type, zpl_isize size, zpl_isize alignment, void* old_memory, zpl_isize old_size, zpl_u64 flags) -> void*
auto zpl_stack_memory_init_from_memory(zpl_stack_memory* s, void* start, zpl_isize size) -> void
Initialize stack allocator from existing memory.
auto zpl_stack_memory_init(zpl_stack_memory* s, zpl_allocator backing, zpl_isize size) -> void
Initialize stack allocator using existing memory allocator.
auto zpl_stack_memory_is_in_use(zpl_stack_memory* s, void* ptr) -> zpl_b32
Check whether stack allocator is in use.
auto zpl_stack_memory_free(zpl_stack_memory* s) -> void
Release the resources used by stack allocator.
auto zpl_stack_allocator(zpl_stack_memory* s) -> zpl_allocator
Allocation Types: alloc, free, free_all.
auto zpl_stack_allocator_proc(void* allocator_data, zplAllocationType type, zpl_isize size, zpl_isize alignment, void* old_memory, zpl_isize old_size, zpl_u64 flags) -> void*

Defines

#define zpl_ptr_add
#define zpl_ptr_sub
#define zpl_ptr_add_const
#define zpl_ptr_sub_const
#define zpl_ptr_diff
#define zpl_zero_item(t)
Clears up an item.
#define zpl_zero_array(a, count)
Clears up an array.
#define zpl_memcopy_array(dst, src, count)
Copy non-overlapping array.
#define zpl_memmove_array(dst, src, count)
Copy an array.
#define ZPL_BIT_CAST(dest, source)
#define zpl_kilobytes(x)
#define zpl_megabytes(x)
#define zpl_gigabytes(x)
#define zpl_terabytes(x)
#define ZPL_ALLOCATOR_PROC(name)
#define ZPL_DEFAULT_MEMORY_ALIGNMENT
#define ZPL_DEFAULT_ALLOCATOR_FLAGS
#define zpl_alloc_item(allocator_, Type)
Allocate memory for an item.
#define zpl_alloc_array(allocator_, Type, count)
Allocate memory for an array of items.
#define zpl_malloc(sz)
Helper to allocate memory using heap allocator.
#define zpl_mfree(ptr)
Helper to free memory allocated by heap allocator.
#define zpl_heap
Alias to heap allocator.

Function documentation

void zpl_zero_size(void* ptr, zpl_isize size)

Clears up memory at location by specified size.

Parameters
ptr Memory location to clear up.
size The size to clear up with.

zpl_virtual_memory zpl_vm_alloc(void* addr, zpl_isize size)

Allocate virtual memory at address with size.

Parameters
addr The starting address of the region to allocate. If NULL, it lets operating system to decide where to allocate it.
size

void* zpl_default_resize_align(zpl_allocator a, void* ptr, zpl_isize old_size, zpl_isize new_size, zpl_isize alignment)

Allocate/Resize memory using default options.

Use this if you don't need a "fancy" resize allocation