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
13#include "krpch.h"
14
15namespace Karma
16{
17 typedef size_t SIZE_T;
18
19 //---------------------------------------------------------------------
20 // Utility for automatically setting up the pointer-sized integer type
21 //---------------------------------------------------------------------
22
29 template<typename T32BITS, typename T64BITS, int PointerSize>
31 {
32 // nothing here are is it an error if the partial specializations fail
33 };
34
41 template<typename T32BITS, typename T64BITS>
42 struct SelectIntPointerType<T32BITS, T64BITS, 8>
43 {
44 // Select the 64 bit type.
45 typedef T64BITS TIntPointer;
46 };
47
54 template<typename T32BITS, typename T64BITS>
55 struct SelectIntPointerType<T32BITS, T64BITS, 4>
56 {
57 // Select the 32 bit type.
58 typedef T32BITS TIntPointer;
59 };
60
69 typedef SelectIntPointerType<uint32_t, uint64_t, sizeof(void*)>::TIntPointer UPTRINT;
70
71 enum class EMemcpyCachePolicy : uint8_t
72 {
79
86 };
87
92 {
107 static FORCEINLINE void* Memmove(void* Dest, const void* Src, SIZE_T Count)
108 {
109 return memmove(Dest, Src, Count);
110 }
111
122 static FORCEINLINE int32_t Memcmp(const void* Buf1, const void* Buf2, SIZE_T Count)
123 {
124 return memcmp(Buf1, Buf2, Count);
125 }
126
137 static FORCEINLINE void* Memset(void* Dest, uint8_t Char, SIZE_T Count)
138 {
139 return memset(Dest, Char, Count);
140 }
141
150 static FORCEINLINE void* Memzero(void* Dest, SIZE_T Count)
151 {
152 return memset(Dest, 0, Count);
153 }
154
167 static FORCEINLINE void* Memcpy(void* Dest, const void* Src, SIZE_T Count)
168 {
169 return memcpy(Dest, Src, Count);
170 }
171
184 static FORCEINLINE void* BigBlockMemcpy(void* Dest, const void* Src, SIZE_T Count)
185 {
186 return memcpy(Dest, Src, Count);
187 }
188
201 static FORCEINLINE void* StreamingMemcpy(void* Dest, const void* Src, SIZE_T Count)
202 {
203 return memcpy(Dest, Src, Count);
204 }
205
216 static FORCEINLINE void* ParallelMemcpy(void* Dest, const void* Src, SIZE_T Count, EMemcpyCachePolicy Policy = EMemcpyCachePolicy::StoreCached)
217 {
218 (void)Policy;
219 return memcpy(Dest, Src, Count);
220 }
221
222 private:
231 template <typename T>
232 static FORCEINLINE void Valswap(T& A, T& B)
233 {
234 // Usually such an implementation would use move semantics, but
235 // we're only ever going to call it on fundamental types and MoveTemp
236 // is not necessarily in scope here anyway, so we don't want to
237 // #include it if we don't need to.
238 T Tmp = A;
239 A = B;
240 B = Tmp;
241 }
242
254 static void MemswapGreaterThan8(void* Ptr1, void* Ptr2, SIZE_T Size);
255
256 public:
257 // NOT FUNCTIONAL YET
258 static inline void Memswap(void* Ptr1, void* Ptr2, SIZE_T Size)
259 {
260 switch (Size)
261 {
262 case 0:
263 break;
264
265 case 1:
266 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
267 break;
268
269 case 2:
270 Valswap(*(uint16_t*)Ptr1, *(uint16_t*)Ptr2);
271 break;
272
273 case 3:
274 Valswap(*((uint16_t*&)Ptr1)++, *((uint16_t*&)Ptr2)++);
275 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
276 break;
277
278 case 4:
279 Valswap(*(uint32_t*)Ptr1, *(uint32_t*)Ptr2);
280 break;
281
282 case 5:
283 Valswap(*((uint32_t*&)Ptr1)++, *((uint32_t*&)Ptr2)++);
284 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
285 break;
286
287 case 6:
288 Valswap(*((uint32_t*&)Ptr1)++, *((uint32_t*&)Ptr2)++);
289 Valswap(*(uint16_t*)Ptr1, *(uint16_t*)Ptr2);
290 break;
291
292 case 7:
293 Valswap(*((uint32_t*&)Ptr1)++, *((uint32_t*&)Ptr2)++);
294 Valswap(*((uint16_t*&)Ptr1)++, *((uint16_t*&)Ptr2)++);
295 Valswap(*(uint8_t*)Ptr1, *(uint8_t*)Ptr2);
296 break;
297
298 case 8:
299 Valswap(*(uint64_t*)Ptr1, *(uint64_t*)Ptr2);
300 break;
301
302 case 16:
303 Valswap(((uint64_t*)Ptr1)[0], ((uint64_t*)Ptr2)[0]);
304 Valswap(((uint64_t*)Ptr1)[1], ((uint64_t*)Ptr2)[1]);
305 break;
306
307 default:
308 MemswapGreaterThan8(Ptr1, Ptr2, Size);
309 break;
310 }
311 }
312 };
313}
#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:157
SelectIntPointerType< uint32_t, uint64_t, sizeof(void *)>::TIntPointer UPTRINT
Definition GenericPlatformMemory.h:69
EMemcpyCachePolicy
Definition GenericPlatformMemory.h:72
@ StoreUncached
Writes to destination memory bypass cache (avoiding pollution).
Definition GenericPlatformMemory.h:85
@ StoreCached
Writes to destination memory are cache-visible (default).
Definition GenericPlatformMemory.h:78
Base class for Platform based memory operations.
Definition GenericPlatformMemory.h:92
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:216
static FORCEINLINE void * Memzero(void *Dest, SIZE_T Count)
Zeros the Count number of characters of object pointed by Dest.
Definition GenericPlatformMemory.h:150
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:137
static FORCEINLINE void * BigBlockMemcpy(void *Dest, const void *Src, SIZE_T Count)
Memcpy optimized for big blocks.
Definition GenericPlatformMemory.h:184
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:107
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:122
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:201
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:167
Defaulter for sized different from 4 and 8. Meaning not supporting 16 bit systems.
Definition GenericPlatformMemory.h:31