forked from IBM/simpool
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFixedSizePool.hpp
More file actions
178 lines (155 loc) · 3.86 KB
/
FixedSizePool.hpp
File metadata and controls
178 lines (155 loc) · 3.86 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifndef _FIXEDSIZEPOOL_HPP
#define _FIXEDSIZEPOOL_HPP
#include <cstring>
#define _XOPEN_SOURCE_EXTENDED 1
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include "StdAllocator.hpp"
template<class T, class MA, class IA = StdAllocator, int NP = (1 << 8)>
class FixedSizePool
{
protected:
struct Pool
{
unsigned char* data;
unsigned int* avail;
unsigned int numAvail;
struct Pool* next;
};
struct Pool* pool;
const std::size_t numPerPool;
const std::size_t totalPoolSize;
std::size_t numBlocks;
void newPool(struct Pool** pnew) {
struct Pool* p = static_cast<struct Pool*>(IA::allocate(sizeof(struct Pool) + NP * sizeof(unsigned int)));
p->numAvail = numPerPool;
p->next = NULL;
p->data = reinterpret_cast<unsigned char*>(MA::allocate(numPerPool * sizeof(T)));
p->avail = reinterpret_cast<unsigned int*>(p + 1);
for (int i = 0; i < NP; i++) p->avail[i] = -1;
*pnew = p;
}
T* allocInPool(struct Pool* p) {
if (!p->numAvail) return NULL;
for (int i = 0; i < NP; i++) {
DWORD tmp = 0;
DWORD mask = *(DWORD*)&p->avail[i];
const bool isNonzero = _BitScanForward(&tmp, mask);
const int bit = (int)tmp;
if (isNonzero && bit >= 0) {
p->avail[i] ^= 1 << bit;
--p->numAvail;
const int entry = i * sizeof(unsigned int) * 8 + bit;
return reinterpret_cast<T*>(p->data) + entry;
}
}
return NULL;
}
//std::mutex mutex;
public:
static inline FixedSizePool& getInstance() {
static FixedSizePool instance;
return instance;
}
FixedSizePool()
: numPerPool(NP * sizeof(unsigned int) * 8),
totalPoolSize(sizeof(struct Pool) +
numPerPool * sizeof(T) +
NP * sizeof(unsigned int)),
numBlocks(0)
{
newPool(&pool);
}
~FixedSizePool() {
for (struct Pool* curr = pool; curr; ) {
struct Pool* next = curr->next;
MA::deallocate(curr);
curr = next;
}
}
T* allocate() {
//mutex.lock();
T* ptr = NULL;
struct Pool* prev = NULL;
struct Pool* curr = pool;
while (!ptr && curr) {
ptr = allocInPool(curr);
prev = curr;
curr = curr->next;
}
if (!ptr) {
newPool(&prev->next);
//mutex.unlock();
ptr = allocate();
//mutex.lock();
// TODO: In this case we should reverse the linked list for optimality
}
else {
numBlocks++;
}
//mutex.unlock();
return ptr;
}
void deallocate(T* ptr) {
//mutex.lock();
int i = 0;
for (struct Pool* curr = pool; curr; curr = curr->next) {
const T* start = reinterpret_cast<T*>(curr->data);
const T* end = reinterpret_cast<T*>(curr->data) + numPerPool;
if ((ptr >= start) && (ptr < end)) {
// indexes bits 0 - numPerPool-1
const int indexD = ptr - reinterpret_cast<T*>(curr->data);
const int indexI = indexD / (sizeof(unsigned int) * 8);
const int indexB = indexD % (sizeof(unsigned int) * 8);
#ifndef NDEBUG
if ((curr->avail[indexI] & (1 << indexB))) {
std::cerr << "Trying to deallocate an entry that was not marked as allocated" << std::endl;
}
#endif
curr->avail[indexI] ^= 1 << indexB;
curr->numAvail++;
numBlocks--;
//mutex.unlock();
return;
}
i++;
}
//mutex.unlock();
std::cerr << "Could not find pointer to deallocate" << std::endl;
throw(std::bad_alloc());
}
/// Return allocated size to user.
std::size_t allocatedSize()
{
//mutex.lock();
auto ret = numBlocks * sizeof(T);
//mutex.unlock();
return ret;
}
/// Return total size with internal overhead.
std::size_t totalSize() {
//mutex.lock();
auto ret = numPools() * totalPoolSize;
//mutex.unlock();
return ret;
}
/// Return the number of pools
std::size_t numPools() {
//mutex.lock();
std::size_t np = 0;
for (struct Pool* curr = pool; curr; curr = curr->next) np++;
//mutex.unlock();
return np;
}
/// Return the pool size
std::size_t poolSize()
{
//mutex.lock();
auto ret = totalPoolSize;
//mutex.unlock();
return ret;
}
};
#endif // _FIXEDSIZEPOOL_HPP