-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_alloc.h
More file actions
251 lines (207 loc) · 6 KB
/
Copy pathfixed_alloc.h
File metadata and controls
251 lines (207 loc) · 6 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <cstdio>
#include <memory>
#include <iostream>
#include <limits>
namespace helper
{
template<class T>
class Stack {
private:
static const int EXTENSION_COEFF = 2;
static const int COMPRESS_COEFF = 2;
static const int MIN_USE_COEFF = 25; // in percents
static const int MIN_CAPACITY = 1;
size_t size_;
size_t capacity_;
T* buf_;
public:
Stack(): size_(0), capacity_(MIN_CAPACITY), buf_(new T[MIN_CAPACITY]) {}
Stack(const Stack& other) = delete;
Stack(Stack&& other) {
size_ = other.size_;
capacity_ = other.capacity_;
buf_ = other.buf_;
other.size_ = 0;
other.capacity_ = 0;
other.buf_ = nullptr;
}
Stack& operator=(const Stack& other) = delete;
Stack& operator=(Stack&& other) {
if (this != &other) {
size_ = other.size_;
capacity_ = other.capacity_;
buf_ = other.buf_;
other.size_ = 0;
other.capacity_ = 0;
other.buf_ = nullptr;
}
return *this;
}
void recopy(size_t newSize) {
T* newBuf = new T[newSize];
for (size_t i = 0; i < size_; ++i)
newBuf[i] = buf_[i];
delete[] buf_;
buf_ = newBuf;
capacity_ = newSize;
}
void push(const T& val) {
if (size_ == capacity_)
recopy(capacity_ * EXTENSION_COEFF);
buf_[size_++] = val;
}
T top() const {
return buf_[size_ - 1];
}
void pop() {
--size_;
if (size_ * 100 / capacity_ < MIN_USE_COEFF && capacity_ > MIN_CAPACITY)
recopy(capacity_ / COMPRESS_COEFF);
}
bool empty() const {
return size_ == 0;
}
~Stack() {
delete[] buf_;
}
};
};
template<size_t chunkSize>
class FixedAllocator {
private:
static const int EXTENSION_COEFF = 2;
char* cur_;
char* end_;
helper::Stack<void*> freed_;
helper::Stack<void*> alloced_;
size_t allocSize_;
public:
FixedAllocator(): cur_(nullptr), end_(nullptr), allocSize_(1) {} // because else it doesn't work
FixedAllocator(const FixedAllocator& other) = delete;
FixedAllocator(FixedAllocator&& other): cur_ = other.cur_, end_ = other.end_, allocSize_ = other.allocSize_ {
freed_ = std::move(other.freed_);
alloced_ = std::move(other.alloced_);
other.cur_ = other.end_ = nullptr;
other.allocSize_ = 0;
}
FixedAllocator& operator=(const FixedAllocator& other) = delete;
FixedAllocator& operator=(FixedAllocator&& other) {
if (this != &other) {
cur_ = other.cur_;
end_ = other.end_;
allocSize_ = other.allocSize_;
freed_ = std::move(other.freed_);
}
return *this;
}
void* allocate() {
if (!freed_.empty()) {
void* res = freed_.top();
freed_.pop();
return res;
}
if (cur_ == end_) {
allocSize_ *= EXTENSION_COEFF;
cur_ = operator new(allocSize_);
end_ = cur_ + allocSize_;
}
cur_ += allocSize_;
return (void*)(cur_ - allocSize_);
}
void deallocate(void* ptr) {
freed_.push(ptr);
}
~FixedAllocator() {
while (!alloced_.empty()) {
operator delete(alloced_.top());
alloced_.pop();
}
}
};
template<class T>
class FastAllocator {
private:
FixedAllocator<sizeof(T) * 4> fa4_;
FixedAllocator<sizeof(T) * 8> fa8_;
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
FastAllocator() = default;
FastAllocator(FastAllocator&& other) = default;
template<class U>
FastAllocator(const FastAllocator<U>& other) {}
template<class U>
FastAllocator& operator=(const FastAllocator& other) {
return *this;
}
T* allocate(size_t count) {
switch (count) {
case 4:
return reinterpret_cast<T*>(fa4_.allocate());
break;
case 8:
return reinterpret_cast<T*>(fa8_.allocate());
break;
default:
return new T[count];
}
}
void deallocate(T* ptr, size_t count) {
switch (count) {
case 4:
fa4_.deallocate(ptr);
break;
case 8:
fa8_.deallocate(ptr);
break;
default:
delete[] ptr;
}
}
pointer address(reference x) const {
return &x;
}
const_pointer address(const_reference x) const {
return &x;
}
size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(value_type);
}
void construct(pointer p, const_reference val) {
new((void*)p) value_type(val);
}
template<class U, class... Args>
void construct(U* p, Args&&... args) {
new((void *)p) U(std::forward<Args>(args)...);
}
void destroy(pointer p) {
deallocate(p);
}
template<class U>
void destroy(U* p) {
deallocate(p);
}
~FastAllocator() = default;
};
namespace std {
template<class T>
struct allocator_traits<FastAllocator<T>> {
typedef T* pointer;
typedef const T* const_pointer;
typedef void* void_pointer;
typedef const void* const_void_pointer;
};
}
template<class T1, class T2>
bool operator==(const FastAllocator<T1>& fs, const FastAllocator<T2>& sc) {
return &fs == ≻
}
template<class T1, class T2>
bool operator!=(const FastAllocator<T1>& fs, const FastAllocator<T2>& sc) {
return !(fs == sc);
}