KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
GenericPlatformMemory.h
Go to the documentation of this file.
1
10
11#pragma once
12
13namespace Karma
14{
15 typedef size_t SIZE_T;
16
17 //---------------------------------------------------------------------
18 // Utility for automatically setting up the pointer-sized integer type
19 //---------------------------------------------------------------------
20
27 template<typename T32BITS, typename T64BITS, int PointerSize>
29 {
30 // nothing here are is it an error if the partial specializations fail
31 };
32
39 template<typename T32BITS, typename T64BITS>
40 struct SelectIntPointerType<T32BITS, T64BITS, 8>
41 {
42 // Select the 64 bit type.
43 typedef T64BITS TIntPointer;
44 };
45
52 template<typename T32BITS, typename T64BITS>
53 struct SelectIntPointerType<T32BITS, T64BITS, 4>
54 {
55 // Select the 32 bit type.
56 typedef T32BITS TIntPointer;
57 };
58
67 typedef SelectIntPointerType<uint32_t, uint64_t, sizeof(void*)>::TIntPointer UPTRINT;
68
69 enum class EMemcpyCachePolicy : uint8_t
70 {
77
84 };
85
90 {
105 static FORCEINLINE void* Memmove(void* Dest, const void* Src, SIZE_T Count)
106 {
107 return memmove(Dest, Src, Count);
108 }
109
120 static FORCEINLINE int32_t Memcmp(const void* Buf1, const void* Buf2, SIZE_T Count)
121 {
122 return memcmp(Buf1, Buf2, Count);
123 }
124
135 static FORCEINLINE void* Memset(void* Dest, uint8_t Char, SIZE_T Count)
136 {
137 return memset(Dest, Char, Count);
138 }
139
148 static FORCEINLINE void* Memzero(void* Dest, SIZE_T Count)
149 {
150 return memset(Dest, 0, Count);
151 }
152
165 static FORCEINLINE void* Memcpy(void* Dest, const void* Src, SIZE_T Count)
166 {
167 return memcpy(Dest, Src, Count);
168 }
169
182 static FORCEINLINE void* BigBlockMemcpy(void* Dest, const void* Src, SIZE_T Count)
183 {
184 return memcpy(Dest, Src, Count);
185 }
186
199 static FORCEINLINE void* StreamingMemcpy(void* Dest, const void* Src, SIZE_T Count)
200 {
201 return memcpy(Dest, Src, Count);
202 }
203
214 static FORCEINLINE void* ParallelMemcpy(void* Dest, const void* Src, SIZE_T Count, EMemcpyCachePolicy Policy = EMemcpyCachePolicy::StoreCached)
215 {
216 (void)Policy;
217 return memcpy(Dest, Src, Count);
218 }
219
220 private:
229 template <typename T>
230 static FORCEINLINE void Valswap(T& A, T& B)
231 {
232 // Usually such an implementation would use move semantics, but
233 // we're only ever going to call it on fundamental types and MoveTemp
234 // is not necessarily in scope here anyway, so we don't want to
235 // #include it if we don't need to.
236 T Tmp = A;
237 A = B;
238 B = Tmp;
239 }
240
252 static void MemswapGreaterThan8(void* Ptr1, void* Ptr2, SIZE_T Size);
253
254 public:
255 // NOT FUNCTIONAL YET
256 static inline void Memswap(void* Ptr1, void* Ptr2, SIZE_T Size)
257 {
258 switch (Size)
259 {
260 case 0:
261 break;
262
263 case 1:
264 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
265 break;
266
267 case 2:
268 Valswap(*(uint16_t*)Ptr1, *(uint16_t*)Ptr2);
269 break;
270
271 case 3:
272 Valswap(*((uint16_t*&)Ptr1)++, *((uint16_t*&)Ptr2)++);
273 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
274 break;
275
276 case 4:
277 Valswap(*(uint32_t*)Ptr1, *(uint32_t*)Ptr2);
278 break;
279
280 case 5:
281 Valswap(*((uint32_t*&)Ptr1)++, *((uint32_t*&)Ptr2)++);
282 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
283 break;
284
285 case 6:
286 Valswap(*((uint32_t*&)Ptr1)++, *((uint32_t*&)Ptr2)++);
287 Valswap(*(uint16_t*)Ptr1, *(uint16_t*)Ptr2);
288 break;
289
290 case 7:
291 Valswap(*((uint32_t*&)Ptr1)++, *((uint32_t*&)Ptr2)++);
292 Valswap(*((uint16_t*&)Ptr1)++, *((uint16_t*&)Ptr2)++);
293 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
294 break;
295
296 case 8:
297 Valswap(*(uint64_t*)Ptr1, *(uint64_t*)Ptr2);
298 break;
299
300 case 16:
301 Valswap(((uint64_t*)Ptr1)[0], ((uint64_t*)Ptr2)[0]);
302 Valswap(((uint64_t*)Ptr1)[1], ((uint64_t*)Ptr2)[1]);
303 break;
304
305 default:
306 MemswapGreaterThan8(Ptr1, Ptr2, Size);
307 break;
308 }
309 }
310 };
311}
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:170
SelectIntPointerType< uint32_t, uint64_t, sizeof(void *)>::TIntPointer UPTRINT
Definition GenericPlatformMemory.h:67
EMemcpyCachePolicy
Definition GenericPlatformMemory.h:70
@ StoreUncached
Writes to destination memory bypass cache (avoiding pollution).
Definition GenericPlatformMemory.h:83
@ StoreCached
Writes to destination memory are cache-visible (default).
Definition GenericPlatformMemory.h:76
Base class for Platform based memory operations.
Definition GenericPlatformMemory.h:90
static FORCEINLINE void * ParallelMemcpy(void *Dest, const void *Src, SIZE_T Count, EMemcpyCachePolicy Policy=EMemcpyCachePolicy::StoreCached)
On some platforms memcpy can be distributed over multiple threads for throughput.
Definition GenericPlatformMemory.h:214
static FORCEINLINE void * Memzero(void *Dest, SIZE_T Count)
Zeros the Count number of characters of object pointed by Dest.
Definition GenericPlatformMemory.h:148
static FORCEINLINE void * Memset(void *Dest, uint8_t Char, SIZE_T Count)
Copy value Char in each of first Count characters of object pointed to by Dest.
Definition GenericPlatformMemory.h:135
static FORCEINLINE void * BigBlockMemcpy(void *Dest, const void *Src, SIZE_T Count)
Memcpy optimized for big blocks.
Definition GenericPlatformMemory.h:182
static FORCEINLINE void * Memmove(void *Dest, const void *Src, SIZE_T Count)
Copies count bytes of characters from Src to Dest. If some regions of the source area and the destina...
Definition GenericPlatformMemory.h:105
static FORCEINLINE int32_t Memcmp(const void *Buf1, const void *Buf2, SIZE_T Count)
Compares first Count bytes of memory of Buf1 and Buf2.
Definition GenericPlatformMemory.h:120
static FORCEINLINE void * StreamingMemcpy(void *Dest, const void *Src, SIZE_T Count)
On some platforms memcpy optimized for big blocks that avoid L2 cache pollution are available.
Definition GenericPlatformMemory.h:199
static FORCEINLINE void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Copies Count bytes from the object pointed by Src to the object pointed by Dest.
Definition GenericPlatformMemory.h:165
Defaulter for sized different from 4 and 8. Meaning not supporting 16 bit systems.
Definition GenericPlatformMemory.h:29