Veritable Lasagna
An Allocator & Data Structure Library for C.
|
Macros | |
#define | VL_MEMORY_ORIGIN_INLINE(headerPtr) (void*)(((vl_uint8_t*)(headerPtr) - ((headerPtr)->headOffset))) |
Returns the original result of the malloc system call. | |
#define | VL_MEMORY_HEADER_INLINE(memPtr) ((vl_memory_header*)(memPtr) - 1) |
Returns a pointer to the specified block of memory Assumes header is stored immediately adjacent to the memory. | |
#define | vl_MemSortSwap(temp, a, b, memSize) memcpy(temp, a, memSize); memcpy(a, b, memSize); memcpy(b, temp, memSize) |
Functions | |
vl_memory * | vlMemAlloc (vl_memsize_t allocSize) |
Attempts to allocate a block of memory. | |
vl_memory * | vlMemAllocAligned (vl_memsize_t size, vl_uint_t align) |
Allocates a block of memory with an alignment. | |
vl_memory * | vlMemRealloc (vl_memory *mem, vl_memsize_t allocSize) |
Reallocates the specified block of memory to hold the specified total number of bytes. | |
vl_memory * | vlMemClone (vl_memory *mem) |
Clones the specified block of memory, returning a pointer to its new clone. | |
vl_int_t | vl_MemSortPartition (vl_usmall_t *buffer, void *pivot, void *swap, vl_int_t low, vl_int_t high, vl_memsize_t elementSize, vl_compare_function comparator) |
void | vl_MemSortQuicksort (vl_int_t *stack, void *buffer, void *pivot, void *swap, vl_memsize_t elementSize, vl_dsidx_t numElements, vl_compare_function comparator) |
void | vlMemSort (void *buffer, vl_memsize_t elementSize, vl_dsidx_t numElements, vl_compare_function comparator) |
Sorts the specified buffer in-place according to the specified element and comparator function. | |
void | vlMemCopyStride (const void *src, vl_dsoffs_t srcStride, void *dest, vl_dsoffs_t dstStride, vl_memsize_t elementSize, vl_dsidx_t numElements) |
Copies data from one buffer to another, with a stride applied to both. | |
void | vlMemReverseSubArraysStride (void *src, vl_dsoffs_t srcStride, vl_memsize_t elementSize, vl_dsidx_t numElements) |
Reverses the bytes in a series of elements of a defined length and stride between them. | |
vl_uint_t | vlMemAlignment (vl_memory *mem) |
Returns the alignment of the specified block of memory. | |
vl_memsize_t | vlMemSize (vl_memory *mem) |
Returns the size (in total number of bytes) of the specified block of vl_memory. | |
void | vlMemFree (vl_memory *mem) |
Frees the specified block of memory. | |
#define VL_MEMORY_HEADER_INLINE | ( | memPtr | ) | ((vl_memory_header*)(memPtr) - 1) |
Returns a pointer to the specified block of memory Assumes header is stored immediately adjacent to the memory.
#define VL_MEMORY_ORIGIN_INLINE | ( | headerPtr | ) | (void*)(((vl_uint8_t*)(headerPtr) - ((headerPtr)->headOffset))) |
Returns the original result of the malloc system call.
#define vl_MemSortSwap | ( | temp, | |
a, | |||
b, | |||
memSize ) memcpy(temp, a, memSize); memcpy(a, b, memSize); memcpy(b, temp, memSize) |
vl_int_t vl_MemSortPartition | ( | vl_usmall_t * | buffer, |
void * | pivot, | ||
void * | swap, | ||
vl_int_t | low, | ||
vl_int_t | high, | ||
vl_memsize_t | elementSize, | ||
vl_compare_function | comparator ) |
void vl_MemSortQuicksort | ( | vl_int_t * | stack, |
void * | buffer, | ||
void * | pivot, | ||
void * | swap, | ||
vl_memsize_t | elementSize, | ||
vl_dsidx_t | numElements, | ||
vl_compare_function | comparator ) |
Returns the alignment of the specified block of memory.
Minimum alignment is defined as VL_DEFAULT_MEMORY_ALIGN, or the largest available word size.
mem | pointer to memory block |
vl_memory * vlMemAlloc | ( | vl_memsize_t | allocSize | ) |
Attempts to allocate a block of memory.
Returns NULL on failure.
allocSize | size of the allocation, in bytes. |
vl_memory * vlMemAllocAligned | ( | vl_memsize_t | allocSize, |
vl_uint_t | align ) |
Allocates a block of memory with an alignment.
allocSize must be >= align or align must be < sizeof(uintptr_t), or this will return NULL. The alignment must be a power of 2 (16, 32, 64, etc), otherwise the behavior is undefined.
Guarantees that the returned pointer will have an value that is a multiple of the specified alignment.
The VL_MEMORY_PAD_UP macro may be used to ensure that the actual length of the memory block is also a multiple of the alignment.
allocSize | size of the allocation, in bytes. |
align |
Clones the specified block of memory, returning a pointer to its new clone.
Returns NULL on failure.
If the source block has an alignment, the result will also have an alignment.
mem | pointer |
void vlMemCopyStride | ( | const void * | src, |
vl_dsoffs_t | srcStride, | ||
void * | dest, | ||
vl_dsoffs_t | dstStride, | ||
vl_memsize_t | elementSize, | ||
vl_dsidx_t | numElements ) |
Copies data from one buffer to another, with a stride applied to both.
Stride is the amount of space (in bytes) between each element.
src | memory block pointer |
srcStride | total number of bytes between each element in "src" |
dest | memory block pointer |
dstStride | total number of bytes between each element in "dest" |
elementSize | total number of bytes wide of each element |
numElements | total number of elements |
void vlMemFree | ( | vl_memory * | mem | ) |
Frees the specified block of memory.
mem | pointer to block. |
vl_memory * vlMemRealloc | ( | vl_memory * | mem, |
vl_memsize_t | allocSize ) |
Reallocates the specified block of memory to hold the specified total number of bytes.
If the specified memory block is explicitly aligned, its alignment is preserved.
mem | pointer to block |
allocSize | new size of the allocation. |
void vlMemReverseSubArraysStride | ( | void * | src, |
vl_dsoffs_t | srcStride, | ||
vl_memsize_t | elementSize, | ||
vl_dsidx_t | numElements ) |
Reverses the bytes in a series of elements of a defined length and stride between them.
This function operates on a sequence of elements (or sub-arrays) within a memory block, where each element is separated by a defined stride. It reverses the bytes of each individual element but does not alter the overall structure of the memory. The elements are processed sequentially, one by one, with each element's bytes reversed in-place.
This is useful when you need to reverse the bytes in each element of a collection of data structures that are tightly packed but may have a varying stride (i.e., distance between consecutive elements in memory).
Example: Suppose we have an array of 32-bit integers, each 4 bytes, with a stride of 8 bytes:
Input (elements of size 4 bytes, stride of 8 bytes): [0xAABBCCDD, 0x11223344, 0x55667788] Memory block: [0xAABBCCDD, pad, 0x11223344, pad, 0x55667788, pad] (Where 'pad' represents the unused memory between elements, i.e., stride.)
Output (each element reversed): [0xDDCCBBAA, 0x44332211, 0x88776655] Memory block: [0xDDCCBBAA, pad, 0x44332211, pad, 0x88776655, pad]
The bytes within each element are reversed, but the stride between elements is respected.
src | memory block pointer. The base address of the memory containing the elements to be reversed. |
srcStride | total number of bytes between each sub-array (or element). This defines the gap between consecutive elements. |
elementSize | size of each sub-array (or element) in bytes. This is the number of bytes that constitute one element. |
numElements | total number of sub-arrays (or elements). This is the number of individual elements to process. |
vl_memsize_t vlMemSize | ( | vl_memory * | mem | ) |
Returns the size (in total number of bytes) of the specified block of vl_memory.
void vlMemSort | ( | void * | buffer, |
vl_memsize_t | elementSize, | ||
vl_dsidx_t | numElements, | ||
vl_compare_function | comparator ) |
Sorts the specified buffer in-place according to the specified element and comparator function.
This function implements an iterative Quicksort, allocating a temporary workspace on either the stack or the heap depending on the size of the data being sorted.
buffer | |
elementSize | |
numElements | |
comparator |