Memory namespace
#include <Base/Memory.h>
Memory-related utilities.
Functions
-
template<class T, std::auto AllocateAligned(std::
size_t alignment = alignof(T)> size_t size) -> Containers:: Array<T> - Allocate aligned memory and value-initialize it.
-
template<class T, std::auto AllocateAligned(Containers::
size_t alignment = alignof(T)> ValueInitT, std:: size_t size) -> Containers:: Array<T> - Allocate aligned memory and value-initialize it.
-
template<class T, std::auto AllocateAligned(Containers::
size_t alignment = alignof(T)> NoInitT, std:: size_t size) -> Containers:: Array<T> - Allocate aligned memory and leave it uninitialized.
-
template<typename T>auto AsBE(T value) -> T
- Converts a value from/to Big-Endian.
-
template<typename T>auto AsLE(T value) -> T
- Converts a value from/to Little-Endian.
-
template<typename T, typename std::auto LoadUnaligned(const void* p) -> T constexpr noexcept
enable_if<std:: is_trivially_copyable<T>::value, int>::type = 0> - Returns a value of given size from unaligned pointer.
-
template<typename T, typename std::void StoreUnaligned(void* p, T v) constexpr noexcept
enable_if<std:: is_trivially_copyable<T>::value, int>::type = 0> - Stores a value of given size to unaligned pointer.
-
template<class T>auto SwapBytes(T value) -> T
- Endian-swap bytes of given value.
Function documentation
template<class T, std:: size_t alignment = alignof(T)>
Containers:: Array<T> Death:: Memory:: AllocateAligned(std:: size_t size)
Allocate aligned memory and value-initialize it.
| Template parameters | |
|---|---|
| T | Type of the returned array |
| alignment | Allocation alignment, in bytes |
| Parameters | |
| size | Count of T items to allocate. If 0, no allocation is done. |
Compared to the classic C std::new that commonly aligns only to 2*sizeof(void*), this function returns "overaligned" allocations, which is mainly useful for efficient SIMD operations.
The alignment is implicitly alignof(T), but can be overridden with the alignment template parameter. When specified explicitly, it is expected to be a power-of-two value, at most 256 bytes and the total byte size being a multiple of the alignment.
The returned pointer is always aligned to at least the desired value, but the alignment can be also higher. For example, allocating a 2 MB buffer may result in it being aligned to the whole memory page, or small alignment values could get rounded up to the default 2*sizeof(void*) alignment.
Array initialization
Like with Containers::
- AllocateAligned(Containers::
ValueInitT, std:: size_t) is equivalent to the default case, zero-initializing trivial types and calling the default constructor elsewhere. Useful when you want to make the choice appear explicit. - AllocateAligned(Containers::
NoInitT, std:: size_t) does not initialize anything. Useful for trivial types when you'll be overwriting the contents anyway, for non-trivial types this is the dangerous option and you need to call the constructor on all elements manually using placement new, std:: uninitialized_copy() or similar.
template<class T, std:: size_t alignment = alignof(T)>
Containers:: Array<T> Death:: Memory:: AllocateAligned(Containers:: ValueInitT,
std:: size_t size)
Allocate aligned memory and value-initialize it.
Same as AllocateAligned(std::
template<class T, std:: size_t alignment = alignof(T)>
Containers:: Array<T> Death:: Memory:: AllocateAligned(Containers:: NoInitT,
std:: size_t size)
Allocate aligned memory and leave it uninitialized.
Compared to AllocateAligned(std::
template<typename T>
T Death:: Memory:: AsBE(T value)
Converts a value from/to Big-Endian.
On Little-Endian systems calls SwapBytes(), on Big-Endian systems returns the value unchanged. Only trivial types of size 2, 4, or 8 bytes are supported.
template<typename T>
T Death:: Memory:: AsLE(T value)
Converts a value from/to Little-Endian.
On Big-Endian systems calls SwapBytes(), on Little-Endian systems returns the value unchanged. Only trivial types of size 2, 4, or 8 bytes are supported.
template<class T>
T Death:: Memory:: SwapBytes(T value)
Endian-swap bytes of given value.
Only trivial types of size 2, 4, or 8 bytes are supported.