Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_memory.h
Go to the documentation of this file.
1
14#ifndef VL_MEMORY_H
15#define VL_MEMORY_H
16
17#include <malloc.h>
18
19#include "vl_compare.h"
20#include "vl_numtypes.h"
21
22typedef VL_MEMORY_SIZE_T vl_memsize_t;
23
24#ifndef VL_KB
30#define VL_KB(x) ((vl_memsize_t)(x) << 10)
31#endif
32
33#ifndef VL_MB
39#define VL_MB(x) ((vl_memsize_t)(x) << 20)
40#endif
41
42#ifndef VL_DEFAULT_MEMORY_SIZE
46#define VL_DEFAULT_MEMORY_SIZE VL_KB(1)
47#endif
48
49#ifndef VL_DEFAULT_MEMORY_ALIGN
50
51#ifdef _MSC_VER
52#define VL_ALIGNOF(T) __alignof(T)
53#else
54#define VL_ALIGNOF(T) __alignof__(T)
55#endif
56
60#define VL_DEFAULT_MEMORY_ALIGN VL_ALIGNOF(vl_ularge_t)
61#endif
62
63#ifndef VL_MEMORY_PAD_UP
75#define VL_MEMORY_PAD_UP(len, pad) (((len) + (pad) - 1) & ~((pad) - 1))
76#endif
77
84#ifndef VL_ALIGN_HINT
85#if defined(_MSC_VER)
86#define VL_ALIGN_HINT(x) __declspec(align(x))
87#elif defined(__GNUC__) || defined(__clang__)
88#define VL_ALIGN_HINT(x) __attribute__((aligned(x)))
89#else
90#define VL_ALIGN_HINT(x)
91#warning VL_ALIGN_HINT failed to resolve. Structure alignment is not guaranteed.
92#endif
93#endif
94
108typedef VL_MEMORY_T vl_memory;
109
118typedef VL_MEMORY_T vl_transient;
119
141VL_API vl_memory* vlMemAlloc(vl_memsize_t allocSize);
142
169VL_API vl_memory* vlMemRealloc(vl_memory* mem, vl_memsize_t allocSize);
170
198VL_API vl_memory* vlMemAllocAligned(vl_memsize_t allocSize, vl_uint_t align);
199
235#define vlMemAllocType(element_type) ((element_type*)vlMemAllocAligned(sizeof(element_type), VL_ALIGNOF(element_type)))
236
271#define vlMemAllocTypeArray(element_type, count) \
272 ((element_type*)vlMemAllocAligned(sizeof(element_type) * (count), VL_ALIGNOF(element_type)))
273
324inline vl_transient* vlMemAllocStack(vl_memsize_t allocSize)
325{
326#ifdef _MSC_VER
327 return (vl_transient*)_alloca(allocSize);
328#elif defined(__GNUC__) || defined(__clang__)
329 return (vl_transient*)__builtin_alloca(allocSize);
330#else
331#error Failed to resolve stack allocation method.
332#endif
333}
334
390inline vl_transient* vlMemAllocStackAligned(vl_memsize_t allocSize, vl_uint16_t align)
391{
392 return (vl_transient*)(((vl_uintptr_t)vlMemAllocStack((allocSize) + (align)-1) + (align)-1) & ~((align)-1));
393}
394
436#define vlMemAllocStackType(element_type) \
437 ((element_type*)vlMemAllocStackAligned(sizeof(element_type), VL_ALIGNOF(element_type)))
438
478#define vlMemAllocStackTypeArray(element_type, count) \
479 ((element_type*)vlMemAllocStackAligned((count) * sizeof(element_type), VL_ALIGNOF(element_type)))
480
505VL_API vl_memory* vlMemClone(vl_memory* mem);
506
524VL_API vl_memsize_t vlMemSize(vl_memory* mem);
525
546
572VL_API void vlMemSort(void* buffer, vl_memsize_t elementSize, vl_dsidx_t numElements, vl_compare_function comparator);
573
599VL_API void vlMemCopyStride(const void* src, vl_dsoffs_t srcStride, void* dest, vl_dsoffs_t dstStride,
600 vl_memsize_t elementSize, vl_dsidx_t numElements);
601
652VL_API void vlMemReverseSubArraysStride(void* src, vl_dsoffs_t srcStride, vl_memsize_t elementSize,
653 vl_dsidx_t numElements);
654
655#ifndef vlMemReverse
672#define vlMemReverse(src, numBytes) vlMemReverseSubArraysStride(src, 1, numBytes, 1)
673#endif
674
675#ifndef vlMemReverseSubArrays
696#define vlMemReverseSubArrays(src, elementSize, numElements) \
697 vlMemReverseSubArraysStride(src, elementSize, elementSize, numElements)
698#endif
699
716VL_API void vlMemFree(vl_memory* mem);
717
718#endif // VL_MEMORY_H
vl_transient * vlMemAllocStack(vl_memsize_t allocSize)
Allocate memory on the stack (automatic storage).
Definition vl_memory.h:324
VL_API void vlMemFree(vl_memory *mem)
Frees the specified block of memory.
Definition vl_memory.c:272
VL_API vl_memory * vlMemRealloc(vl_memory *mem, vl_memsize_t allocSize)
Reallocates the specified block of memory to hold the specified total number of bytes.
Definition vl_memory.c:79
VL_API vl_memory * vlMemAlloc(vl_memsize_t allocSize)
Attempts to allocate a block of memory.
Definition vl_memory.c:42
VL_API 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.
Definition vl_memory.c:197
VL_API vl_memory * vlMemClone(vl_memory *mem)
Clones the specified block of memory, returning a pointer to its new clone.
Definition vl_memory.c:118
VL_API 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.
Definition vl_memory.c:214
VL_API vl_memsize_t vlMemSize(vl_memory *mem)
Returns the size (in total number of bytes) of the specified block of vl_memory.
Definition vl_memory.c:265
VL_MEMORY_T vl_memory
Definition vl_memory.h:108
VL_MEMORY_T vl_transient
Definition vl_memory.h:118
VL_API 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.
Definition vl_memory.c:234
VL_API vl_memory * vlMemAllocAligned(vl_memsize_t allocSize, vl_uint_t align)
Allocates a block of memory with an alignment.
Definition vl_memory.c:56
VL_API vl_uint_t vlMemAlignment(vl_memory *mem)
Returns the alignment of the specified block of memory.
Definition vl_memory.c:258
vl_transient * vlMemAllocStackAligned(vl_memsize_t allocSize, vl_uint16_t align)
Allocate aligned memory on the stack (automatic storage).
Definition vl_memory.h:390
VL_UPTR_T vl_uintptr_t
Unsigned integer type suitable for expressing memory addresses.
Definition vl_numtypes.h:163
VL_UINT_T vl_uint_t
Standard unsigned integer type.
Definition vl_numtypes.h:158
VL_STRUCTURE_INDEX_T vl_dsidx_t
Index type for data structures.
Definition vl_numtypes.h:75
VL_STRUCTURE_OFFSET_T vl_dsoffs_t
Byte offset type for data structures.
Definition vl_numtypes.h:70