-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmappedObjectPool.h
More file actions
206 lines (186 loc) · 7.42 KB
/
BitmappedObjectPool.h
File metadata and controls
206 lines (186 loc) · 7.42 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
/**
* BitmappedObjectPool<T> - A high-performance, thread-safe object pool implementation
*
* Manages allocation and deallocation of objects with zero-fragmentation and
* minimal overhead. Key features:
* - Chunk-based allocation with dynamic growth and controlled shrinking
* - Type-safe template implementation for any C++ object type
* - Thread-safe operations for concurrent allocate/release
* - Configurable memory retention via percent_slack parameter
* - O(1) allocations for already-reserved memory
* - Zero-fragmentation memory layout
* - Safe pointer validation to detect use-after-free bugs
* - Support for cross-type conversion with proper type checking
*
* Ideal for high-performance applications requiring frequent allocation/deallocation
* of objects with deterministic performance characteristics and minimal memory overhead.
*/
#include "Bitmap.h"
#include <mutex>
#include <string>
#include <vector>
#include <memory>
#include <cstdint>
#include <type_traits>
template <class T>
class BitmappedObjectPool
{
public:
/* Note: memory is allocated in chunks of multiples of the given template unit.
percent_slack is used so that for a given chunk, if the chunks above it
are empty/currently-unused then those extra chunks are available to be
freed to release the memory. 0% slack means release extra chunks as
soon as they are unneeded. 100% slack means wait until the current
chunk is entirely unused before getting rid of ones above it. -1
percent_slack means never free up chunks, keep them reserved forever. */
BitmappedObjectPool(const int chunk_size, const int percent_slack)
: chunk_size_(chunk_size), percent_slack_(percent_slack) {
}
template <class U>
BitmappedObjectPool(const BitmappedObjectPool<U>& old_obj) {
chunk_size_ = old_obj.chunk_size_;
percent_slack_ = old_obj.percent_slack_;
usage_bitmaps_.reserve(old_obj.usage_bitmaps_.size());
for (const auto& e : old_obj.usage_bitmaps_) {
usage_bitmaps_.push_back(std::make_shared<Bitmap>(*e));
}
data_elements_.reserve(old_obj.data_elements_.size());
for (size_t i = 0; i < old_obj.data_elements_.size(); ++i) {
auto new_chunk = std::shared_ptr<T[]>(new T[chunk_size_], std::default_delete<T[]>());
// copy elements if types are compatible
if constexpr (std::is_convertible_v<U, T>) {
for (int j = 0; j < chunk_size_; ++j) {
new_chunk.get()[j] = static_cast<T>(old_obj.data_elements_[i].get()[j]);
}
}
data_elements_.push_back(std::move(new_chunk));
}
}
T* getAndMarkNextUnused() {
std::lock_guard<std::mutex> lock(in_use_);
int32_t bitmap_index = -1;
int bitnum = -1;
for (unsigned int i = 0; i < usage_bitmaps_.size(); ++i) {
int idx = usage_bitmaps_[i]->getAndSetFirstZero();
if (idx >= 0) {
bitmap_index = i;
bitnum = idx;
break;
}
}
if (bitmap_index == -1) {
usage_bitmaps_.push_back(std::make_shared<Bitmap>(chunk_size_, 0));
data_elements_.push_back(std::shared_ptr<T[]>(new T[chunk_size_], std::default_delete<T[]>()));
bitmap_index = static_cast<int32_t>(usage_bitmaps_.size() - 1);
bitnum = usage_bitmaps_.back()->getAndSetFirstZero();
}
if (bitmap_index >= 0 && bitmap_index < static_cast<int32_t>(data_elements_.size())) {
return &data_elements_[bitmap_index].get()[bitnum];
}
return nullptr;
}
bool markAsUnused(T* now_unused) {
std::lock_guard<std::mutex> lock(in_use_);
if (!belongs(now_unused)) {
return false;
}
for (size_t i = 0; i < usage_bitmaps_.size(); ++i) {
T* start_address = getPoolStart(i);
if (now_unused >= start_address && now_unused <= getPoolEnd(i)) {
std::ptrdiff_t offset = now_unused - start_address;
if (offset >= 0 && offset < chunk_size_) {
usage_bitmaps_[i]->setBitTo(static_cast<int>(offset), 0);
if (percent_slack_ != -1) {
if (i < usage_bitmaps_.size() - 1) {
bool empty_above_us = true;
for (size_t cn = i + 1; cn < usage_bitmaps_.size() && empty_above_us; ++cn) {
empty_above_us = (usage_bitmaps_[cn]->countOnes() == 0);
}
if (empty_above_us) {
int64_t number_of_zeroes = usage_bitmaps_[i]->countZeroes();
int64_t slack_ratio = (number_of_zeroes * 100LL) / static_cast<int64_t>(chunk_size_);
if (slack_ratio >= percent_slack_) {
auto new_size = i + 1;
usage_bitmaps_.resize(new_size);
data_elements_.resize(new_size);
}
}
}
}
return true;
}
return false;
}
}
return false;
}
bool belongs(const T* item) const {
if (!item) return false;
for (size_t i = 0; i < data_elements_.size(); ++i) {
T* start = getPoolStart(i);
T* end = getPoolEnd(i);
if (item >= start && item <= end) {
return true;
}
}
return false;
}
bool isValidObject(const T* item) const {
if (!item || !belongs(item)) {
return false;
}
std::lock_guard<std::mutex> lock(in_use_);
for (size_t i = 0; i < data_elements_.size(); ++i) {
T* start = getPoolStart(i);
T* end = getPoolEnd(i);
if (item >= start && item <= end) {
std::ptrdiff_t offset = item - start;
if (offset >= 0 && offset < chunk_size_) {
return usage_bitmaps_[i]->isSet(static_cast<int>(offset));
}
return false;
}
}
return false;
}
int countBuffers() const {
int count = 0;
for (auto& bm : usage_bitmaps_) {
count += bm->countOnes();
}
return count;
}
const std::string asHexString() const {
std::string result;
for (auto& bm : usage_bitmaps_) {
result.append(bm->asHexString());
}
return result;
}
const std::string asBinaryString() const {
std::string result;
for (auto& bm : usage_bitmaps_) {
result.append(bm->asBinaryString());
}
return result;
}
private:
T* getPoolStart(size_t index) const {
if (index < data_elements_.size()) {
return &data_elements_[index].get()[0];
}
return nullptr;
}
T* getPoolEnd(size_t index) const {
if (index < data_elements_.size()) {
return &data_elements_[index].get()[chunk_size_ - 1];
}
return nullptr;
}
mutable std::mutex in_use_;
std::vector<std::shared_ptr<Bitmap>> usage_bitmaps_;
std::vector<std::shared_ptr<T[]>> data_elements_;
int chunk_size_;
int percent_slack_;
};