IRC_SERVER
By @hyunjunk (hyunjun2372@gmail.com)
Loading...
Searching...
No Matches
FixedMemoryPool.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <new>
5#include "Core/Log.hpp"
7
8namespace IRCCore
9{
10
11/** Memory pool that can allocate fixed amount of data
12 *
13 * @details Allocates raw memory on a per-page basis ( see PAGE_SIZE ), and manages data there.
14 * Also uses an index array filled sequentially from 1 to N,
15 * and a corresponding cursor to identify allocatable data.
16 *
17 * @tparam T Type of data to allocate
18 * @tparam MemoryPageCapacity Number of pages to allocate. (see details)
19 */
20template <typename T, size_t MemoryPageCapacity>
22{
23public:
26 , mIndices()
27 , mCursor(0)
28 , mMemoryRaw(new char[MemoryPageCapacity * PAGE_SIZE])
29 {
30 for (size_t i = 0; i < mCapacity; i++)
31 {
32 mIndices[i] = i;
33 }
34 }
35
37 {
38 if (mCursor != 0)
39 {
40 CoreMemoryLeakLog("[FixedMemoryPool] Memory Leak Detected. Cursor: " + ValToString(mCursor));
41 }
42
43 delete[] mMemoryRaw;
44 }
45
46 /** Allocate a data */
48 {
49 if (mCursor < mCapacity)
50 {
51 const size_t idx = mIndices[mCursor++];
52 const char* ptrRaw = mMemoryRaw + (idx * sizeof(T));
53
54 // CoreLog("[FixedMemoryPool] Allocate: " + ValToStringByHex(reinterpret_cast<const void*>(ptrRaw)) + " idx: " + ValToString(idx));
55
56 return (T*)ptrRaw;
57 }
58 return NULL;
59 }
60
61 /** Deallocate a data
62 *
63 * @param ptr Pointer to the data to deallocate
64 */
66 {
67 Assert(ptr >= (T*)mMemoryRaw && ptr < (T*)(mMemoryRaw + mCapacity * sizeof(T)));
68
69 if (ptr != NULL)
70 {
71 const size_t idx = ptr - (T*)mMemoryRaw;
72 Assert(mCursor !=0);
73 mIndices[--mCursor] = idx;
74
75 // DEBUG
76 // CoreLog("[FixedMemoryPool] Deallocate: " + ValToStringByHex(ptr) + " idx: " + ValToString(idx) + " Cursor: " + ValToString(mCursor) + " Class: " + typeid(T).name());
77
78 }
79 }
80
82 {
83 return mCursor == mCapacity;
84 }
85
86 FORCEINLINE bool IsInPool(const void* ptr) const
87 {
88 return ptr >= mMemoryRaw && ptr < mMemoryRaw + mCapacity * sizeof(T);
89 }
90
91 FORCEINLINE size_t GetUsed() const
92 {
93 return mCursor;
94 }
95
96 FORCEINLINE size_t GetCapacity() const
97 {
98 return mCapacity;
99 }
100
101private:
102 enum { CAPACITY = MemoryPageCapacity * PAGE_SIZE / sizeof(T) }; //< Floor to the sizeof(T)
103
104 size_t mCapacity;
106 size_t mCursor;
108};
109
110} // namespace IRCCore
#define FORCEINLINE
Definition AttributeDefines.hpp:55
#define NODISCARD
Definition AttributeDefines.hpp:17
#define Assert(exp)
Implemented as direct interrupt to avoid dirtying the call stack with assert function when debugging.
Definition MacroDefines.hpp:30
Memory pool that can allocate fixed amount of data.
Definition FixedMemoryPool.hpp:22
T * Allocate()
Allocate a data.
Definition FixedMemoryPool.hpp:47
void Deallocate(T *ptr)
Deallocate a data.
Definition FixedMemoryPool.hpp:65
size_t mIndices[CAPACITY]
Definition FixedMemoryPool.hpp:105
bool IsInPool(const void *ptr) const
Definition FixedMemoryPool.hpp:86
@ CAPACITY
Definition FixedMemoryPool.hpp:102
size_t GetUsed() const
Definition FixedMemoryPool.hpp:91
bool IsCapacityFull() const
Definition FixedMemoryPool.hpp:81
size_t mCapacity
Definition FixedMemoryPool.hpp:104
size_t mCursor
Definition FixedMemoryPool.hpp:106
char * mMemoryRaw
Definition FixedMemoryPool.hpp:107
~FixedMemoryPool()
Definition FixedMemoryPool.hpp:36
size_t GetCapacity() const
Definition FixedMemoryPool.hpp:96
FixedMemoryPool()
Definition FixedMemoryPool.hpp:24
Definition ControlBlock.hpp:7
@ PAGE_SIZE
Definition GlobalConstants.hpp:7
void CoreMemoryLeakLog(const std::string &msg)
Definition Log.hpp:19
std::string ValToString(const T value)
Definition Log.hpp:23