This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFixedSizePool.hpp
147 lines (124 loc) · 3.63 KB
/
FixedSizePool.hpp
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
#ifndef _FIXEDSIZEPOOL_HPP
#define _FIXEDSIZEPOOL_HPP
#include <cstring>
#define _XOPEN_SOURCE_EXTENDED 1
#include <strings.h>
#include <iostream>
#include <stdio.h>
#include "StdAllocator.hpp"
template<class T, class MA, class IA = StdAllocator, int NP=(1<<6)>
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++) {
const int bit = ffs(p->avail[i]) - 1;
if (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;
}
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() {
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);
ptr = allocate();
// TODO: In this case we should reverse the linked list for optimality
}
else {
numBlocks++;
}
return ptr;
}
void deallocate(T* ptr) {
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--;
return;
}
i++;
}
std::cerr << "Could not find pointer to deallocate" << std::endl;
throw(std::bad_alloc());
}
/// Return allocated size to user.
std::size_t allocatedSize() const { return numBlocks * sizeof(T); }
/// Return total size with internal overhead.
std::size_t totalSize() const {
return numPools() * totalPoolSize;
}
/// Return the number of pools
std::size_t numPools() const {
std::size_t np = 0;
for (struct Pool *curr = pool; curr; curr = curr->next) np++;
return np;
}
/// Return the pool size
std::size_t poolSize() const { return totalPoolSize; }
};
#endif // _FIXEDSIZEPOOL_HPP