Plasma Engine  2.0
Loading...
Searching...
No Matches
MemoryUtils.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <cstdint> // for uintptr_t
5
6enum plConstructionMode
7{
8 ConstructAll = 0,
9 SkipTrivialTypes = 1,
10};
11
26{
27public:
28 using ConstructorFunction = void (*)(void* pDestination);
29 using CopyConstructorFunction = void (*)(void* pDestination, const void* pSource);
30 using DestructorFunction = void (*)(void* pDestination);
31
35 template <plConstructionMode mode, typename T>
36 static void Construct(T* pDestination, size_t uiCount = 1); // [tested]
37
39 template <plConstructionMode mode, typename T>
40 static ConstructorFunction MakeConstructorFunction(); // [tested]
41
43 template <typename Destination, typename Source>
44 static void CopyConstruct(Destination* pDestination, const Source& copy, size_t uiCount = 1); // [tested]
45
48 template <typename T>
49 static void CopyConstructArray(T* pDestination, const T* pSource, size_t uiCount); // [tested]
50
52 template <typename T>
53 static CopyConstructorFunction MakeCopyConstructorFunction(); // [tested]
54
56 template <typename T>
57 static void MoveConstruct(T* pDestination, T&& source); // [tested]
58
61 template <typename T>
62 static void MoveConstruct(T* pDestination, T* pSource, size_t uiCount = 1);
63
66 template <typename Destination, typename Source>
67 static void CopyOrMoveConstruct(Destination* pDestination, Source&& source);
68
72 template <typename T>
73 static void RelocateConstruct(T* pDestination, T* pSource, size_t uiCount = 1);
74
76 template <typename T>
77 static void Destruct(T* pDestination, size_t uiCount = 1); // [tested]
78
80 template <typename T>
81 static DestructorFunction MakeDestructorFunction(); // [tested]
82
86 template <typename T>
87 static void Copy(T* pDestination, const T* pSource, size_t uiCount = 1); // [tested]
88
90 static void RawByteCopy(void* pDestination, const void* pSource, size_t uiNumBytesToCopy);
91
95 template <typename T>
96 static void CopyOverlapped(T* pDestination, const T* pSource, size_t uiCount = 1);
97
99 template <typename T>
100 static void Relocate(T* pDestination, T* pSource, size_t uiCount = 1); // [tested]
101
105 template <typename T>
106 static void RelocateOverlapped(T* pDestination, T* pSource, size_t uiCount = 1); // [tested]
107
109 template <typename T>
110 static void Prepend(T* pDestination, const T& source, size_t uiCount);
111
113 template <typename T>
114 static void Prepend(T* pDestination, T&& source, size_t uiCount);
115
117 template <typename T>
118 static void Prepend(T* pDestination, const T* pSource, size_t uiSourceCount, size_t uiCount);
119
121 template <typename T>
122 static bool IsEqual(const T* a, const T* b, size_t uiCount = 1); // [tested]
123
125 template <typename T>
126 static void ZeroFill(T* pDestination, size_t uiCount = 1); // [tested]
127
129 template <typename T, size_t N>
130 static void ZeroFill(T (&destination)[N]) = delete;
131
133 template <typename T, size_t N>
134 static void ZeroFillArray(T (&destination)[N]); // [tested]
135
137 template <typename T>
138 static void PatternFill(T* pDestination, plUInt8 uiBytePattern, size_t uiCount = 1); // [tested]
139
141 template <typename T, size_t N>
142 static void PatternFill(T (&destination)[N], plUInt8 uiBytePattern) = delete;
143
145 template <typename T, size_t N>
146 static void PatternFillArray(T (&destination)[N], plUInt8 uiBytePattern); // [tested]
147
149 template <typename T>
150 static plInt32 Compare(const T* a, const T* b, size_t uiCount = 1); // [tested]
151
153 static plInt32 RawByteCompare(const void* a, const void* b, size_t uiNumBytesToCompare);
154
159 template <typename T>
160 static T* AddByteOffset(T* pPtr, std::ptrdiff_t offset); // [tested]
161
163 template <typename T>
164 static T* AlignBackwards(T* pPtr, size_t uiAlignment); // [tested]
165
167 template <typename T>
168 static T* AlignForwards(T* pPtr, size_t uiAlignment); // [tested]
169
171 template <typename T>
172 static T AlignSize(T uiSize, T uiAlignment); // [tested]
173
175 template <typename T>
176 static bool IsAligned(const T* pPtr, size_t uiAlignment); // [tested]
177
179 template <typename T>
180 static bool IsSizeAligned(T uiSize, T uiAlignment); // [tested]
181
188 static void ReserveLower4GBAddressSpace();
189};
190
191#include <Foundation/Memory/Implementation/MemoryUtils_inl.h>
This class provides functions to work on raw memory.
Definition MemoryUtils.h:26
static void ZeroFill(T(&destination)[N])=delete
Overload to prevent confusing calling this on a single object or a static array of objects....
static T * AlignForwards(T *pPtr, size_t uiAlignment)
Aligns the pointer ptr by moving its address forwards to the next multiple of uiAlignment.
static void CopyConstruct(Destination *pDestination, const Source &copy, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination, by creating uiCount copies of c...
static T * AddByteOffset(T *pPtr, std::ptrdiff_t offset)
Returns the address stored in ptr plus the given byte offset iOffset, cast to type T.
static void CopyConstructArray(T *pDestination, const T *pSource, size_t uiCount)
Constructs uiCount objects of type T in a raw buffer at pDestination from an existing array of object...
static void Prepend(T *pDestination, T &&source, size_t uiCount)
Moves uiCount objects in pDestination by one object and moves source to the free space.
static T * AlignBackwards(T *pPtr, size_t uiAlignment)
Aligns the pointer ptr by moving its address backwards to the previous multiple of uiAlignment.
static ConstructorFunction MakeConstructorFunction()
Returns a function pointer to construct an instance of T. Returns nullptr for trivial types.
static void CopyOrMoveConstruct(Destination *pDestination, Source &&source)
This function will either move call MoveConstruct or CopyConstruct for a single element source,...
static bool IsEqual(const T *a, const T *b, size_t uiCount=1)
Tests if objects of type T from pSource and pDestination are equal.
static CopyConstructorFunction MakeCopyConstructorFunction()
Returns a function pointer to copy construct an instance of T.
static void ReserveLower4GBAddressSpace()
Reserves the lower 4GB of address space in 64-bit builds to ensure all allocations start above 4GB.
Definition MemoryUtils.cpp:9
static void ZeroFillArray(T(&destination)[N])
Zeros every byte in the provided memory buffer.
static void RelocateConstruct(T *pDestination, T *pSource, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination from an existing array of object...
static void Construct(T *pDestination, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination.
static DestructorFunction MakeDestructorFunction()
Returns a function pointer to destruct an instance of T. Returns nullptr for POD-types.
static void MoveConstruct(T *pDestination, T *pSource, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination from an existing array of object...
static bool IsSizeAligned(T uiSize, T uiAlignment)
Checks whether the given size is aligned.
static void RawByteCopy(void *pDestination, const void *pSource, size_t uiNumBytesToCopy)
Copies exactly uiNumBytesToCopy from pSource to pDestination, independent of the involved types and t...
Definition MemoryUtils_inl.h:197
static void PatternFill(T *pDestination, plUInt8 uiBytePattern, size_t uiCount=1)
Fills every byte of the provided buffer with the given value.
static void Prepend(T *pDestination, const T &source, size_t uiCount)
Moves uiCount objects in pDestination by one object and copies source to the free space.
static plInt32 RawByteCompare(const void *a, const void *b, size_t uiNumBytesToCompare)
Compares exactly uiNumBytesToCompare from a and b, independent of the involved types and their sizes.
Definition MemoryUtils_inl.h:461
static void Copy(T *pDestination, const T *pSource, size_t uiCount=1)
Copies objects of type T from pSource to pDestination.
static void PatternFill(T(&destination)[N], plUInt8 uiBytePattern)=delete
Overload to prevent confusing calling this on a single object or a static array of objects....
static void PatternFillArray(T(&destination)[N], plUInt8 uiBytePattern)
Fills every byte of the provided buffer with the given value.
static void MoveConstruct(T *pDestination, T &&source)
Constructs an object of type T in a raw buffer at pDestination, by using move construction from sourc...
static plInt32 Compare(const T *a, const T *b, size_t uiCount=1)
Compares two buffers of raw memory byte wise.
static bool IsAligned(const T *pPtr, size_t uiAlignment)
Checks whether ptr is aligned to a memory address that is a multiple of uiAlignment.
static void RelocateOverlapped(T *pDestination, T *pSource, size_t uiCount=1)
Moves objects of type T from pSource to pDestination.
static void CopyOverlapped(T *pDestination, const T *pSource, size_t uiCount=1)
Copies objects of type T from pSource to pDestination.
static void Prepend(T *pDestination, const T *pSource, size_t uiSourceCount, size_t uiCount)
Moves uiCount objects in pDestination by uiSourceCount objects and copies source to the free space.
static void ZeroFill(T *pDestination, size_t uiCount=1)
Zeros every byte in the provided memory buffer.
static T AlignSize(T uiSize, T uiAlignment)
Aligns the given size uiSize by rounding up to the next multiple of the size.
static void Relocate(T *pDestination, T *pSource, size_t uiCount=1)
Moves objects of type T from pSource to pDestination.
static void Destruct(T *pDestination, size_t uiCount=1)
Destructs uiCount objects of type T at pDestination.