tracked_allocation.h
| File Path | engine/include/conjure/core/memory/tracked_allocation.h |
| API ↗ | Memory |
Counted, aligned heap allocation.
Constants
| Name | Type | Value | Description |
|---|---|---|---|
memory::DEFAULT_ALIGNMENT |
inline constexpr usize |
alignof(std:: |
Alignment used when a caller does not ask for a specific one. Every allocation is at least this aligned, whatever smaller value was requested. Trap: alignof(std::max_align_t) is 8 on MSVC x64 but STDCPP_DEFAULT_NEW_ALIGNMENT is 16 there, so this is not the alignment operator new guarantees. A replacement of global operator new (the test support's tracked_global_new.cpp) must pass STDCPP_DEFAULT_NEW_ALIGNMENT to tracked_allocate explicitly, and its aligned overloads must forward their alignment; relying on this default would hand out under-aligned storage for 16-byte aligned types (SSE and glm aligned types) on MSVC only. |
memory::MAX_ALLOCATION_BYTES |
inline constexpr usize |
static_ |
Largest request tracked_allocate and tracked_reallocate accept: PTRDIFF_MAX. No object may be larger (pointer differences inside it would not be representable), every C runtime refuses such requests, and rejecting them here keeps the size arithmetic of the allocators built on top from wrapping. Larger requests fail like any other out-of-memory condition: nullptr, no assert, counters untouched. |
Functions
Public
memory::align_up
memory::align_upRounds value up to the next multiple of alignment (a power of two). Returns value when it is already aligned. Wraps for values within alignment of the usize maximum; callers that cannot rule that out check first.
memory::is_aligned
memory::is_aligned[[nodiscard]] CONJURE_API bool is_aligned(const void *pointer, usize alignment) noexcept;True when the address of pointer is a multiple of alignment (a power of two). nullptr is aligned to everything.
memory::is_power_of_two
memory::is_power_of_two[[nodiscard]] constexpr bool is_power_of_two(usize value) noexcept;True for 1, 2, 4, 8, ... and false for 0.
memory::tracked_allocate
memory::tracked_allocate[[nodiscard]] CONJURE_API void *tracked_allocate(usize bytes, usize alignment = DEFAULT_ALIGNMENT) noexcept;Allocates bytes bytes whose address is a multiple of alignment (a power of two; values below DEFAULT_ALIGNMENT are raised to it). A zero-byte request returns a unique, non-null pointer like operator new does. Returns nullptr when the system is out of memory, when bytes exceeds MAX_ALLOCATION_BYTES, or when the alignment is not a power of two (the latter also asserts). Nothing is counted on failure.
memory::tracked_free
memory::tracked_freeCONJURE_API void tracked_free(void *pointer, usize bytes) noexcept;Releases memory obtained from tracked_allocate or tracked_reallocate. bytes is the size that was requested; it is kept for symmetry with the allocation call and for future per-size statistics, the counters themselves only count calls. nullptr is ignored and not counted.
memory::tracked_reallocate
memory::tracked_reallocate[[nodiscard]] CONJURE_API void *tracked_reallocate( void *pointer, usize oldBytes, usize newBytes, usize alignment = DEFAULT_ALIGNMENT ) noexcept;Resizes an allocation, preserving the first min(oldBytes, newBytes) bytes. alignment must be the alignment the block was allocated with. A nullptr pointer behaves like tracked_allocate, a zero newBytes frees the block and returns nullptr. Counted as one free plus one allocation so live counts stay balanced. On failure (out of memory, newBytes above MAX_ALLOCATION_BYTES, invalid alignment) the original block is untouched and nullptr returned. Platform note: Windows resizes in place when the C runtime can (_aligned_realloc); POSIX has no aligned realloc, so the block is always allocated anew and the preserved bytes copied. Callers that resize often should grow geometrically so the copy stays amortised O(1).