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#include "krpch.h"
16
17#ifdef KR_WINDOWS_PLATFORM
18#include "Platform/Windows/Core/WindowsPlatformMemory.h"
19#elif KR_MAC_PLATFORM
21#elif KR_LINUX_PLATFORM
23#endif
24
25#include "UObjectAllocator.h"
26
27namespace Karma
28{
32 typedef unsigned int uint32;
33
37 enum
38 {
40
42 };
43
48 {
53 {
54 None = -1,
55 Default,
56 Temporary,
57 SmallPool,
58
59 Max
60 };
61
62 // @name Memory functions (wrapper for FPlatformMemory)
63
78 static FORCEINLINE void* Memmove(void* Dest, const void* Src, SIZE_T Count)
79 {
80 return FPlatformMemory::Memmove(Dest, Src, Count);
81 }
82
93 static FORCEINLINE int32_t Memcmp(const void* Buf1, const void* Buf2, SIZE_T Count)
94 {
95 return FPlatformMemory::Memcmp(Buf1, Buf2, Count);
96 }
97
108 static FORCEINLINE void* Memset(void* Dest, uint8_t Char, SIZE_T Count)
109 {
110 return FPlatformMemory::Memset(Dest, Char, Count);
111 }
112
121 template< class T >
122 static FORCEINLINE void Memset(T& Src, uint8_t ValueToSet)
123 {
124 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the three parameters function");
125 Memset(&Src, ValueToSet, sizeof(T));
126 }
127
136 static FORCEINLINE void* Memzero(void* Dest, SIZE_T Count)
137 {
138 return FPlatformMemory::Memzero(Dest, Count);
139 }
140
148 template< class T >
149 static FORCEINLINE void Memzero(T& Src)
150 {
151 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the two parameters function");
152 Memzero(&Src, sizeof(T));
153 }
154
167 static FORCEINLINE void* Memcpy(void* Dest, const void* Src, SIZE_T Count)
168 {
169 return FPlatformMemory::Memcpy(Dest, Src, Count);
170 }
171
180 template< class T >
181 static FORCEINLINE void Memcpy(T& Dest, const T& Src)
182 {
183 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the three parameters function");
184 Memcpy(&Dest, &Src, sizeof(T));
185 }
186
199 static FORCEINLINE void* BigBlockMemcpy(void* Dest, const void* Src, SIZE_T Count)
200 {
201 return FPlatformMemory::BigBlockMemcpy(Dest, Src, Count);
202 }
203
216 static FORCEINLINE void* StreamingMemcpy(void* Dest, const void* Src, SIZE_T Count)
217 {
218 return FPlatformMemory::StreamingMemcpy(Dest, Src, Count);
219 }
220
231 static FORCEINLINE void* ParallelMemcpy(void* Dest, const void* Src, SIZE_T Count, EMemcpyCachePolicy Policy = EMemcpyCachePolicy::StoreCached)
232 {
233 return FPlatformMemory::ParallelMemcpy(Dest, Src, Count, Policy);
234 }
235
236 // NOT FUNCTIONAL YET
237 static FORCEINLINE void Memswap(void* Ptr1, void* Ptr2, SIZE_T Size)
238 {
239 FPlatformMemory::Memswap(Ptr1, Ptr2, Size);
240 }
241
248 static FORCEINLINE void* SystemMalloc(SIZE_T Size)
249 {
250 /* TODO: Trace! */
251 return ::malloc(Size);
252 }
253
260 static FORCEINLINE void SystemFree(void* Ptr)
261 {
262 /* TODO: Trace! */
263 ::free(Ptr);
264 }
265
266 //
267 // C style memory allocation stubs. Not functional as intended
268 //
269
270 static void* Malloc(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT);
271 /*static void* Realloc(void* Original, SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT);
272 static void Free(void* Original);
273 static SIZE_T GetAllocSize(void* Original);*/
274
286 static FORCEINLINE void* MallocZeroed(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT)
287 {
288 void* Memory = Malloc(Count, Alignment);
289 Memzero(Memory, Count);
290 return Memory;
291 }
292 };
293}
#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
EMemcpyCachePolicy
Definition GenericPlatformMemory.h:72
@ StoreCached
Writes to destination memory are cache-visible (default).
Definition GenericPlatformMemory.h:78
@ MIN_ALIGNMENT
Minimum allocator alignment.
Definition KarmaMemory.h:41
@ DEFAULT_ALIGNMENT
Default allocator alignment. If the default is specified, the allocator applies to engine rules....
Definition KarmaMemory.h:39
unsigned int uint32
32-bit unsigned integer
Definition KarmaMemory.h:32
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...
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
Class with memory relevant functions.
Definition KarmaMemory.h:48
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:108
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:216
static FORCEINLINE void * SystemMalloc(SIZE_T Size)
C style memory allocation stubs that fall back to C runtime.
Definition KarmaMemory.h:248
static FORCEINLINE void * Memzero(void *Dest, SIZE_T Count)
Zeros the Count number of characters of object pointed by Dest.
Definition KarmaMemory.h:136
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:167
static FORCEINLINE void Memset(T &Src, uint8_t ValueToSet)
Copy value ValueToSet to template Src.
Definition KarmaMemory.h:122
static FORCEINLINE void SystemFree(void *Ptr)
C style memory deallocation.
Definition KarmaMemory.h:260
static FORCEINLINE void * BigBlockMemcpy(void *Dest, const void *Src, SIZE_T Count)
Memcpy optimized for big blocks.
Definition KarmaMemory.h:199
static FORCEINLINE void Memzero(T &Src)
Zeros the template Src.
Definition KarmaMemory.h:149
AllocationHints
Some allocators can be given hints to treat allocations differently depending on how the memory is us...
Definition KarmaMemory.h:53
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:78
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:181
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:231
static FORCEINLINE void * MallocZeroed(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Return a zeroed block of allocated memory.
Definition KarmaMemory.h:286
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:93