-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpl_allocator.h
More file actions
53 lines (42 loc) · 1007 Bytes
/
pl_allocator.h
File metadata and controls
53 lines (42 loc) · 1007 Bytes
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
#ifndef __PL_ALLOCATOR_H__
#define __PL_ALLOCATOR_H__
#include <algorithm>
#include <memory>
#include <unordered_map>
#include <vector>
template <typename _Tp, typename _Alloc = std::allocator<_Tp>>
class pl_allocator : public _Alloc {
struct pl_thread_data {
std::vector<_Tp *> v_pl;
~pl_thread_data() {
for (auto p : v_pl) {
_Alloc().deallocate(p, 1);
}
}
static pl_thread_data &get_instance() {
static thread_local pl_thread_data tdata;
return tdata;
}
};
public:
_Tp *allocate(std::size_t n) {
auto &v_pl = pl_thread_data::get_instance().v_pl;
_Tp *p;
if (n > 1 || v_pl.empty()) {
p = _Alloc().allocate(n);
} else {
p = v_pl.back();
v_pl.pop_back();
}
return p;
}
void deallocate(_Tp *p, std::size_t n) {
auto &v_pl = pl_thread_data::get_instance().v_pl;
if (n > 1) {
_Alloc().deallocate(p, n);
} else {
v_pl.push_back(p);
}
}
};
#endif // __PL_ALLOCATOR_H__