KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
UObjectAllocator.h
Go to the documentation of this file.
1
10
11/*=============================================================================
12 UnObjAllocator.h: Unreal object allocation
13=============================================================================*/
14
15// This system needs further writing to conform with UE
16// https://github.com/ravimohan1991/KarmaEngine/discussions/10
17
18#pragma once
19
20#include "KarmaTypes.h"
21
22namespace Karma
23{
24 class UObjectBase;
25
35 typedef void (*FUObjectAllocatorCallback)(void* InObject, const std::string& InName, size_t InSize, size_t InAlignment, class UClass* InClass);
36
40 template <typename T>
42 {
43 enum { Value = false };
44 };
45
46 template <> struct TIsIntegral< bool> { enum { Value = true }; };
47 template <> struct TIsIntegral< char> { enum { Value = true }; };
48 template <> struct TIsIntegral<signed char> { enum { Value = true }; };
49 template <> struct TIsIntegral<unsigned char> { enum { Value = true }; };
50 template <> struct TIsIntegral< char16_t> { enum { Value = true }; };
51 template <> struct TIsIntegral< char32_t> { enum { Value = true }; };
52 template <> struct TIsIntegral< wchar_t> { enum { Value = true }; };
53 template <> struct TIsIntegral< short> { enum { Value = true }; };
54 template <> struct TIsIntegral<unsigned short> { enum { Value = true }; };
55 template <> struct TIsIntegral< int> { enum { Value = true }; };
56 template <> struct TIsIntegral<unsigned int> { enum { Value = true }; };
57 template <> struct TIsIntegral< long> { enum { Value = true }; };
58 template <> struct TIsIntegral<unsigned long> { enum { Value = true }; };
59 template <> struct TIsIntegral< long long> { enum { Value = true }; };
60 template <> struct TIsIntegral<unsigned long long> { enum { Value = true }; };
61
62 template <typename T> struct TIsIntegral<const T> { enum { Value = TIsIntegral<T>::Value }; };
63 template <typename T> struct TIsIntegral< volatile T> { enum { Value = TIsIntegral<T>::Value }; };
64
65 template <typename T> struct TIsIntegral<const volatile T> { enum { Value = TIsIntegral<T>::Value }; };
66
73 template <typename T>
75 {
76 enum { Value = false };
77 };
78
79 template <typename T> struct TIsPointer<T*> { enum { Value = true }; };
80
81 template <typename T> struct TIsPointer<const T> { enum { Value = TIsPointer<T>::Value }; };
82 template <typename T> struct TIsPointer< volatile T> { enum { Value = TIsPointer<T>::Value }; };
83
84 template <typename T> struct TIsPointer<const volatile T> { enum { Value = TIsPointer<T>::Value }; };
85
94 template <typename T>
95 FORCEINLINE constexpr T Align(T Val, uint64_t Alignment)
96 {
97 KR_CORE_ASSERT(TIsIntegral<T>::Value || TIsPointer<T>::Value, "Align expects an integer or pointer type");
98
99 return (T)(((uint64_t)Val + Alignment - 1) & ~(Alignment - 1));
100 }
101
112 {
113 public:
114
121 m_PermanentObjectPoolSize(0),
122 m_PermanentObjectPool(nullptr),
123 m_PermanentObjectPoolTail(nullptr),
124 m_PermanentObjectPoolExceededTail(nullptr)
125 {
126 }
127
129
138 void AllocatePermanentObjectPool(int32_t InPermanentObjectPoolSize);
139
149 void Initialize(uint8_t* pMemoryStart, size_t elemetSizeBytes, size_t numberOfElemets);
150
156 void BootMessage();
157
167 {
168 return ((const uint8_t*)Object >= m_PermanentObjectPool) && ((const uint8_t*)Object < m_PermanentObjectPoolTail);
169 }
170
184 UObjectBase* AllocateUObject(size_t Size, size_t Alignment, bool bAllowPermanent);
185
191 void DumpUObjectsInformation(void* InObject, const std::string& InName, size_t InSize, size_t InAlignment, class UClass* InClass);
192
203
209 //void FreeUObject(UObjectBase* Object) const;
210
211 //
212 // Getters
213 //
219 uint8_t* GetPermanentObjectPoolTail() const { return m_PermanentObjectPoolTail; }
220
226 uint8_t* GetPermanentObjectPool() const { return m_PermanentObjectPool; }
227
233 uint32_t GetPermanentPoolSize() const { return m_PermanentObjectPoolSize; }
234
240 uint8_t* GetPermanentObjectPoolEnd() const { return m_PermanentObjectPoolEnd; }
241
247 uint32_t GetBareUObjectSize() const { return m_BareUObjectsSize; }
248
254 uint32_t GetAlignedUObjectSize() const { return m_AlignedUObjectsSize; }
255
261 uint32_t GetNumberOfUObjects() const { return m_NumberOfUObjects; }
262
263 private:
264
266 int32_t m_PermanentObjectPoolSize;
267
269 uint8_t* m_PermanentObjectPool;
270
272 uint8_t* m_PermanentObjectPoolTail;
273
275 uint8_t* m_PermanentObjectPoolEnd;
276
278 uint8_t* m_PermanentObjectPoolExceededTail;
279
281 uint32_t m_BareUObjectsSize;
282
284 uint32_t m_AlignedUObjectsSize;
285
292 uint32_t m_NumberOfUObjects;
293
298 };
299
307 extern KARMA_API FUObjectAllocator GUObjectAllocator;
308}
#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
This file contains the custom types used in Engine's logic.
void(* FUObjectAllocatorCallback)(void *InObject, const std::string &InName, size_t InSize, size_t InAlignment, class UClass *InClass)
A definition useful for UObjects statistics.
Definition UObjectAllocator.h:35
FORCEINLINE constexpr T Align(T Val, uint64_t Alignment)
Definition UObjectAllocator.h:95
A pool allocator for Karma's UObjects.
Definition UObjectAllocator.h:112
uint8_t * GetPermanentObjectPool() const
Definition UObjectAllocator.h:226
uint32_t GetPermanentPoolSize() const
Definition UObjectAllocator.h:233
void BootMessage()
Definition UObjectAllocator.cpp:37
uint8_t * GetPermanentObjectPoolTail() const
Definition UObjectAllocator.h:219
uint8_t * GetPermanentObjectPoolEnd() const
Definition UObjectAllocator.h:240
FUObjectAllocator()
Definition UObjectAllocator.h:120
void RegisterUObjectsStatisticsCallback(FUObjectAllocatorCallback dumpCallback)
Definition UObjectAllocator.cpp:102
uint32_t GetNumberOfUObjects() const
Definition UObjectAllocator.h:261
uint32_t GetBareUObjectSize() const
Definition UObjectAllocator.h:247
void DumpUObjectsInformation(void *InObject, const std::string &InName, size_t InSize, size_t InAlignment, class UClass *InClass)
Definition UObjectAllocator.cpp:107
uint32_t GetAlignedUObjectSize() const
Definition UObjectAllocator.h:254
UObjectBase * AllocateUObject(size_t Size, size_t Alignment, bool bAllowPermanent)
Allocates a UObjectBase from the free store or the permanent object pool.
Definition UObjectAllocator.cpp:49
void Initialize(uint8_t *pMemoryStart, size_t elemetSizeBytes, size_t numberOfElemets)
Definition UObjectAllocator.cpp:24
void AllocatePermanentObjectPool(int32_t InPermanentObjectPoolSize)
Definition UObjectAllocator.cpp:16
FORCEINLINE bool ResidesInPermanentPool(const UObjectBase *Object) const
Definition UObjectAllocator.h:166
An object class.
Definition Class.h:158
Low level implementation of UObject, should not be used directly in game code.
Definition UObjectBase.h:36
Karma's std::vector wrapper with additional functionalities.
Definition KarmaTypes.h:243
Definition UObjectAllocator.h:42
Traits class which tests if a type is a pointer.
Definition UObjectAllocator.h:75