KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
KarmaMemory.h
Go to the documentation of this file.
1
10
11// Taken from Unreal Engine with a soft corner of mind
12
13#pragma once
14
15#ifdef KR_WINDOWS_PLATFORM
17#elif KR_MAC_PLATFORM
19#elif KR_LINUX_PLATFORM
21#endif
22
23#include "UObjectAllocator.h"
24
25namespace Karma
26{
30 typedef unsigned int uint32;
31
35 enum
36 {
38
40 };
41
46 {
51 {
52 None = -1,
53 Default,
54 Temporary,
55 SmallPool,
56
57 Max
58 };
59
60 // @name Memory functions (wrapper for FPlatformMemory)
61
76 static FORCEINLINE void* Memmove(void* Dest, const void* Src, SIZE_T Count)
77 {
78 return FPlatformMemory::Memmove(Dest, Src, Count);
79 }
80
91 static FORCEINLINE int32_t Memcmp(const void* Buf1, const void* Buf2, SIZE_T Count)
92 {
93 return FPlatformMemory::Memcmp(Buf1, Buf2, Count);
94 }
95
106 static FORCEINLINE void* Memset(void* Dest, uint8_t Char, SIZE_T Count)
107 {
108 return FPlatformMemory::Memset(Dest, Char, Count);
109 }
110
119 template< class T >
120 static FORCEINLINE void Memset(T& Src, uint8_t ValueToSet)
121 {
122 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the three parameters function");
123 Memset(&Src, ValueToSet, sizeof(T));
124 }
125
134 static FORCEINLINE void* Memzero(void* Dest, SIZE_T Count)
135 {
136 return FPlatformMemory::Memzero(Dest, Count);
137 }
138
146 template< class T >
147 static FORCEINLINE void Memzero(T& Src)
148 {
149 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the two parameters function");
150 Memzero(&Src, sizeof(T));
151 }
152
165 static FORCEINLINE void* Memcpy(void* Dest, const void* Src, SIZE_T Count)
166 {
167 return FPlatformMemory::Memcpy(Dest, Src, Count);
168 }
169
178 template< class T >
179 static FORCEINLINE void Memcpy(T& Dest, const T& Src)
180 {
181 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the three parameters function");
182 Memcpy(&Dest, &Src, sizeof(T));
183 }
184
197 static FORCEINLINE void* BigBlockMemcpy(void* Dest, const void* Src, SIZE_T Count)
198 {
199 return FPlatformMemory::BigBlockMemcpy(Dest, Src, Count);
200 }
201
214 static FORCEINLINE void* StreamingMemcpy(void* Dest, const void* Src, SIZE_T Count)
215 {
216 return FPlatformMemory::StreamingMemcpy(Dest, Src, Count);
217 }
218
229 static FORCEINLINE void* ParallelMemcpy(void* Dest, const void* Src, SIZE_T Count, EMemcpyCachePolicy Policy = EMemcpyCachePolicy::StoreCached)
230 {
231 return FPlatformMemory::ParallelMemcpy(Dest, Src, Count, Policy);
232 }
233
234 // NOT FUNCTIONAL YET
235 static FORCEINLINE void Memswap(void* Ptr1, void* Ptr2, SIZE_T Size)
236 {
237 FPlatformMemory::Memswap(Ptr1, Ptr2, Size);
238 }
239
246 static FORCEINLINE void* SystemMalloc(SIZE_T Size)
247 {
248 /* TODO: Trace! */
249 return ::malloc(Size);
250 }
251
258 static FORCEINLINE void SystemFree(void* Ptr)
259 {
260 /* TODO: Trace! */
261 ::free(Ptr);
262 }
263
264 //
265 // C style memory allocation stubs. Not functional as intended
266 //
267
268 static void* Malloc(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT);
269 /*static void* Realloc(void* Original, SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT);
270 static void Free(void* Original);
271 static SIZE_T GetAllocSize(void* Original);*/
272
284 static FORCEINLINE void* MallocZeroed(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT)
285 {
286 void* Memory = Malloc(Count, Alignment);
287 Memzero(Memory, Count);
288 return Memory;
289 }
290 };
291}
#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
EMemcpyCachePolicy
Definition GenericPlatformMemory.h:70
@ StoreCached
Writes to destination memory are cache-visible (default).
Definition GenericPlatformMemory.h:76
@ MIN_ALIGNMENT
Minimum allocator alignment.
Definition KarmaMemory.h:39
@ DEFAULT_ALIGNMENT
Default allocator alignment. If the default is specified, the allocator applies to engine rules....
Definition KarmaMemory.h:37
unsigned int uint32
32-bit unsigned integer
Definition KarmaMemory.h:30
This file contains the class FLinuxPlatformMemory.
This file contains the class FMacPlatformMemory.
This file contains the class FUObjectAllocator along with the global variable declaration and several...
Windows platform specific implementation of memory functions.
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
Class with memory relevant functions.
Definition KarmaMemory.h:46
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 KarmaMemory.h:106
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 KarmaMemory.h:214
static FORCEINLINE void * SystemMalloc(SIZE_T Size)
C style memory allocation stubs that fall back to C runtime.
Definition KarmaMemory.h:246
static FORCEINLINE void * Memzero(void *Dest, SIZE_T Count)
Zeros the Count number of characters of object pointed by Dest.
Definition KarmaMemory.h:134
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 KarmaMemory.h:165
static FORCEINLINE void Memset(T &Src, uint8_t ValueToSet)
Copy value ValueToSet to template Src.
Definition KarmaMemory.h:120
static FORCEINLINE void SystemFree(void *Ptr)
C style memory deallocation.
Definition KarmaMemory.h:258
static FORCEINLINE void * BigBlockMemcpy(void *Dest, const void *Src, SIZE_T Count)
Memcpy optimized for big blocks.
Definition KarmaMemory.h:197
static FORCEINLINE void Memzero(T &Src)
Zeros the template Src.
Definition KarmaMemory.h:147
AllocationHints
Some allocators can be given hints to treat allocations differently depending on how the memory is us...
Definition KarmaMemory.h:51
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 KarmaMemory.h:76
static FORCEINLINE void Memcpy(T &Dest, const T &Src)
Copies from the object pointed by Src to the object pointed by Dest.
Definition KarmaMemory.h:179
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 KarmaMemory.h:229
static FORCEINLINE void * MallocZeroed(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Return a zeroed block of allocated memory.
Definition KarmaMemory.h:284
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 KarmaMemory.h:91