the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1
2#pragma once
3
4
5#include <stdlib.h>
6
7class C4JMemoryPool
8{
9public:
10 unsigned int Align(unsigned int val, unsigned int align) { return int((val+(align-1))/align) * align; }
11 virtual void* Alloc(size_t size) = 0;
12 virtual void Free(void* ptr) = 0;
13};
14
15
16
17// Fast Efficient Fixed-Size Memory Pool : No Loops and No Overhead
18// http://www.alogicalmind.com/memory_pools/index.htm
19class C4JMemoryPoolFixed : public C4JMemoryPool
20{
21 // Basic type define
22 typedef unsigned int uint;
23 typedef unsigned char uchar;
24 uint m_numOfBlocks; // Num of blocks
25 uint m_sizeOfEachBlock; // Size of each block
26 uint m_numFreeBlocks; // Num of remaining blocks
27 uint m_numInitialized; // Num of initialized blocks
28 uchar* m_memStart; // Beginning of memory pool
29 uchar* m_memEnd; // End of memory pool
30 uchar* m_next; // Num of next free block
31// CRITICAL_SECTION m_CS;
32public:
33 C4JMemoryPoolFixed()
34 {
35 m_numOfBlocks = 0;
36 m_sizeOfEachBlock = 0;
37 m_numFreeBlocks = 0;
38 m_numInitialized = 0;
39 m_memStart = NULL;
40 m_memEnd = NULL;
41 m_next = 0;
42 }
43
44 C4JMemoryPoolFixed(uint sizeOfEachBlock, uint numOfBlocks)
45 {
46 CreatePool(sizeOfEachBlock, numOfBlocks);
47 }
48
49 ~C4JMemoryPoolFixed() { DestroyPool(); }
50
51 void CreatePool(uint sizeOfEachBlock, uint numOfBlocks)
52 {
53 assert(sizeOfEachBlock >= 4); // has to be at least the size of an int, for book keeping
54 m_numOfBlocks = numOfBlocks;
55 m_sizeOfEachBlock = sizeOfEachBlock;
56 m_numFreeBlocks = numOfBlocks;
57 m_numInitialized = 0;
58 m_memStart = new uchar[ m_sizeOfEachBlock *
59 m_numOfBlocks ];
60 m_memEnd = m_memStart + (m_sizeOfEachBlock * m_numOfBlocks);
61 m_next = m_memStart;
62// InitializeCriticalSection(&m_CS);
63 }
64
65 void DestroyPool()
66 {
67 delete[] m_memStart;
68 m_memStart = NULL;
69 }
70
71 uchar* AddrFromIndex(uint i) const
72 {
73 return m_memStart + ( i * m_sizeOfEachBlock );
74 }
75
76 uint IndexFromAddr(const uchar* p) const
77 {
78 return (((uint)(p - m_memStart)) / m_sizeOfEachBlock);
79 }
80
81 virtual void* Alloc(size_t size)
82 {
83 if(size > m_sizeOfEachBlock)
84 return ::malloc(size);
85// EnterCriticalSection(&m_CS);
86 if (m_numInitialized < m_numOfBlocks )
87 {
88 uint* p = (uint*)AddrFromIndex( m_numInitialized );
89 *p = m_numInitialized + 1;
90 m_numInitialized++;
91 }
92 void* ret = NULL;
93 if ( m_numFreeBlocks > 0 )
94 {
95 ret = (void*)m_next;
96 --m_numFreeBlocks;
97 if (m_numFreeBlocks!=0)
98 {
99 m_next = AddrFromIndex( *((uint*)m_next) );
100 }
101 else
102 {
103 m_next = NULL;
104 }
105 }
106// LeaveCriticalSection(&m_CS);
107 return ret;
108 }
109
110 virtual void Free(void* ptr)
111 {
112 if(ptr < m_memStart || ptr > m_memEnd)
113 {
114 ::free(ptr);
115 return;
116 }
117// EnterCriticalSection(&m_CS);
118 if (m_next != NULL)
119 {
120 (*(uint*)ptr) = IndexFromAddr( m_next );
121 m_next = (uchar*)ptr;
122 }
123 else
124 {
125 *((uint*)ptr) = m_numOfBlocks;
126 m_next = (uchar*)ptr;
127 }
128 ++m_numFreeBlocks;
129// LeaveCriticalSection(&m_CS);
130 }
131}; // End pool class
132
133
134// this pool will constantly grow until it is reset (automatically when all allocs have been "freed")
135class C4JMemoryPoolGrow : public C4JMemoryPool
136{
137 uint32_t m_totalSize;
138 uint32_t m_memUsed;
139 uint32_t m_numAllocations;
140 uint8_t* m_pMemory;
141 uint32_t m_currentOffset;
142
143public:
144 C4JMemoryPoolGrow(uint32_t size = 64*1024)
145 {
146 size = Align(size, 4);
147 m_totalSize = size;
148 m_pMemory = new uint8_t[size];
149 m_currentOffset = 0;
150 m_memUsed = 0;
151 m_numAllocations = 0;
152 }
153
154 virtual void* Alloc(size_t size)
155 {
156 size = Align(size, 4); // 4 byte align the memory
157 assert((m_currentOffset + size) < m_totalSize); // make sure we haven't ran out of space
158 void* returnMem = &m_pMemory[m_currentOffset]; // grab the return memory
159 m_currentOffset += size;
160 m_numAllocations++;
161 return returnMem;
162 }
163 virtual void Free(void* ptr)
164 {
165 m_numAllocations--;
166 if(m_numAllocations == 0)
167 m_currentOffset = 0; // reset the pool when we reach zero allocations
168 }
169};
170
171
172
173
174
175
176