-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemoryPoolManager.cpp
104 lines (81 loc) · 2.32 KB
/
MemoryPoolManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "MemoryPoolManager.h"
#include <stdio.h>
typedef struct chunk
{
chunk* next;
void* getData()
{
return (void*) (((int)this) + (int)sizeof(Chunk));
}
static void* getChunk(void* ptr)
{
return (void*) ((int)ptr - (int)sizeof(Chunk));
}
}Chunk;
//Enter in bytes. The real pool size is (poolSize + ((poolsize / datasize) * 4) in size.
MemoryPoolManager::MemoryPoolManager(int poolSize, int dataSize)
{
this->pool_size = poolSize;
this->data_size = dataSize;
char* c = new char[(this->pool_size + ((this->pool_size / dataSize) * sizeof(Chunk)))](); //this is the only place new will be called.
this->head = (void*) c;
this->pool = c; //used when destroying. Having a handle for the pool isn't a bad thing either if you want to do stuff with it.
Chunk* currentChunk = (Chunk*)this->head;
for(int i=(int)this->head; i < (int)this->head + (this->pool_size + ((this->pool_size / dataSize) * sizeof(Chunk))) ; i += sizeof(Chunk) + dataSize)
{
currentChunk->next = (Chunk*) i;
currentChunk = currentChunk->next;
}
}
MemoryPoolManager::~MemoryPoolManager()
{
delete this->pool;
}
void* MemoryPoolManager::allocate()
{
int address = 0;
if(this->head != nullptr)
{
address = (int)this->head;
this->head = (void*) ((Chunk*)this->head)->next; //remove the head from the free list
}
else
{
OutOfMemory();
}
return (void*) ((Chunk*)address)->getData();
}
//free an allocated data
void MemoryPoolManager::free(void* ptr)
{
Chunk* header = (Chunk*)Chunk::getChunk(ptr);
this->head = header;
}
//Get the remaining space in the pool.
int MemoryPoolManager::remainingSpace()
{
int freeChunkRemaining = 0;
Chunk* curChunk = (Chunk*) this->head;
while(curChunk != nullptr)
{
++freeChunkRemaining;
curChunk = curChunk->next;
}
return freeChunkRemaining * this->data_size;
}
//Free and clear all the allocated instances in the pool.
void MemoryPoolManager::clear()
{
this->head = (void*)this->pool; //reconstruct all the free nodes.
Chunk* currentChunk = (Chunk*)this->head;
for(int i=(int)this->head; i < (int)this->head + (this->pool_size + ((this->pool_size / this->data_size) * sizeof(Chunk))) ; i += sizeof(Chunk) + this->data_size)
{
currentChunk->next = (Chunk*) i;
currentChunk = currentChunk->next;
}
}
void MemoryPoolManager::OutOfMemory()
{
printf("\nMemory pool out of memory\n");
getchar();
}