-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (72 loc) · 2.28 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (72 loc) · 2.28 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
#include <iostream>
#include <vector>
#include <set>
template <typename T>
class bucket_allocator {
struct bucket {
void* ptr;
size_t sz;
size_t n_alloc;
size_t n_dealloc;
std::set<void*> objs;
};
public:
typedef T value_type;
explicit bucket_allocator(const size_t threshold_size = 1024) : _thr_sz(threshold_size) {}
bucket_allocator(const bucket_allocator& other) = delete;
bucket_allocator& operator= (const bucket_allocator& other) = delete;
~bucket_allocator() = default;
T* allocate(const size_t n) {
if(n > _thr_sz) {
_buckets.push_back(bucket{operator new[] (n * sizeof(T)), n, n, 0});
_buckets.back().objs.insert(_buckets.back().ptr);
return reinterpret_cast<T*>(_buckets.back().ptr);
}
for(bucket& b : _buckets) {
if(b.sz - b.n_alloc >= n) {
b.n_alloc += n;
b.objs.insert(reinterpret_cast<T*>(b.ptr) + (b.n_alloc - n));
return reinterpret_cast<T*>(b.ptr) + (b.n_alloc - n);
}
}
_buckets.push_back(bucket{operator new[] (_thr_sz * sizeof(T)), _thr_sz, n, 0});
_buckets.back().objs.insert(_buckets.back().ptr);
return reinterpret_cast<T*>(_buckets.back().ptr);
}
void deallocate(T* ptr, size_t n) {
for(auto it = _buckets.begin(); it != _buckets.end(); it++) {
if(it->objs.count(ptr) > 0) {
it->n_dealloc += n;
it->objs.erase(ptr);
if(it->n_dealloc == it->sz) {
operator delete[] (it->ptr);
_buckets.erase(it);
}
break;
}
}
}
private:
size_t _thr_sz;
std::vector<bucket> _buckets;
};
int main() {
std::allocator<int> a;
bucket_allocator<int> b;
int* arr0 = b.allocate(10);
int* arr1 = b.allocate(10);
int* arr2 = b.allocate(10);
int* arr3 = b.allocate(10);
int* arr4 = b.allocate(10);
for(int i = 0; i < 10; i++)
arr0[i] = i;
for(int i = 0; i < 10; i++)
arr1[i] = i;
for(int i = 0; i < 10; i++)
arr2[i] = i;
for(int i = 0; i < 10; i++)
arr3[i] = i;
for(int i = 0; i < 10; i++)
std::cout << arr3[i] << ' ';
return 0;
}