Karma Engine
Loading...
Searching...
No Matches
KarmaMemory.h
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Taken from Unreal Engine with a soft corner of mind
4
5#pragma once
6
7#include "krpch.h"
8
9#ifdef KR_WINDOWS_PLATFORM
10#include "Platform/Windows/Core/WindowsPlatformMemory.h"
11#elif KR_MAC_PLATFORM
12#include "Platform/Mac/Core/MacPlatformMemory.h"
13#elif KR_LINUX_PLATFORM
14#include "Platform/Linux/Core/LinuxPlatformMemory.h"
15#endif
16
17#include "UObjectAllocator.h"
18
19namespace Karma
20{
21 // 32-bit unsigned integer
22 typedef unsigned int uint32;
23
24 enum
25 {
26 // Default allocator alignment. If the default is specified, the allocator applies to engine rules.
27 // Blocks >= 16 bytes will be 16-byte-aligned, Blocks < 16 will be 8-byte aligned. If the allocator does
28 // not support allocation alignment, the alignment will be ignored.
29 DEFAULT_ALIGNMENT = 0,
30
31 // Minimum allocator alignment
32 MIN_ALIGNMENT = 8,
33 };
34
35
36 struct KARMA_API FMemory
37 {
40 {
41 None = -1,
42 Default,
43 Temporary,
44 SmallPool,
45
46 Max
47 };
48
51 static FORCEINLINE void* Memmove(void* Dest, const void* Src, SIZE_T Count)
52 {
53 return FPlatformMemory::Memmove(Dest, Src, Count);
54 }
55
56 static FORCEINLINE int32_t Memcmp(const void* Buf1, const void* Buf2, SIZE_T Count)
57 {
58 return FPlatformMemory::Memcmp(Buf1, Buf2, Count);
59 }
60
61 static FORCEINLINE void* Memset(void* Dest, uint8_t Char, SIZE_T Count)
62 {
63 return FPlatformMemory::Memset(Dest, Char, Count);
64 }
65
66 template< class T >
67 static FORCEINLINE void Memset(T& Src, uint8_t ValueToSet)
68 {
69 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the three parameters function");
70 Memset(&Src, ValueToSet, sizeof(T));
71 }
72
73 static FORCEINLINE void* Memzero(void* Dest, SIZE_T Count)
74 {
75 return FPlatformMemory::Memzero(Dest, Count);
76 }
77
78 template< class T >
79 static FORCEINLINE void Memzero(T& Src)
80 {
81 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the two parameters function");
82 Memzero(&Src, sizeof(T));
83 }
84
85 static FORCEINLINE void* Memcpy(void* Dest, const void* Src, SIZE_T Count)
86 {
87 return FPlatformMemory::Memcpy(Dest, Src, Count);
88 }
89
90 template< class T >
91 static FORCEINLINE void Memcpy(T& Dest, const T& Src)
92 {
93 KR_CORE_ASSERT(!TIsPointer<T>::Value, "For pointers use the three parameters function");
94 Memcpy(&Dest, &Src, sizeof(T));
95 }
96
97 static FORCEINLINE void* BigBlockMemcpy(void* Dest, const void* Src, SIZE_T Count)
98 {
99 return FPlatformMemory::BigBlockMemcpy(Dest, Src, Count);
100 }
101
102 static FORCEINLINE void* StreamingMemcpy(void* Dest, const void* Src, SIZE_T Count)
103 {
104 return FPlatformMemory::StreamingMemcpy(Dest, Src, Count);
105 }
106
107 static FORCEINLINE void* ParallelMemcpy(void* Dest, const void* Src, SIZE_T Count, EMemcpyCachePolicy Policy = EMemcpyCachePolicy::StoreCached)
108 {
109 return FPlatformMemory::ParallelMemcpy(Dest, Src, Count, Policy);
110 }
111
112 // NOT FUNCTIONAL YET
113 static FORCEINLINE void Memswap(void* Ptr1, void* Ptr2, SIZE_T Size)
114 {
115 FPlatformMemory::Memswap(Ptr1, Ptr2, Size);
116 }
117
118 //
119 // C style memory allocation stubs that fall back to C runtime
120 //
121 static FORCEINLINE void* SystemMalloc(SIZE_T Size)
122 {
123 /* TODO: Trace! */
124 return ::malloc(Size);
125 }
126
127 static FORCEINLINE void SystemFree(void* Ptr)
128 {
129 /* TODO: Trace! */
130 ::free(Ptr);
131 }
132
133 //
134 // C style memory allocation stubs. Not functional as intended
135 //
136
137 static void* Malloc(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT);
138 /*static void* Realloc(void* Original, SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT);
139 static void Free(void* Original);
140 static SIZE_T GetAllocSize(void* Original);*/
141
142 static FORCEINLINE void* MallocZeroed(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT)
143 {
144 void* Memory = Malloc(Count, Alignment);
145 Memzero(Memory, Count);
146 return Memory;
147 }
148 };
149}
Definition KarmaMemory.h:37
AllocationHints
Definition KarmaMemory.h:40