-
Notifications
You must be signed in to change notification settings - Fork 59
/
deferred_allocator.h
169 lines (132 loc) · 4.88 KB
/
deferred_allocator.h
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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2016 Herb Sutter. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GCPP_DEFERRED_ALLOCATOR
#define GCPP_DEFERRED_ALLOCATOR
#include "deferred_heap.h"
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
namespace gcpp {
//----------------------------------------------------------------------------
//
// deferred_allocator - wrap up global_deferred_heap() as a C++14 allocator, with thanks to
// Howard Hinnant's allocator boilerplate exemplar code, online at
// https://howardhinnant.github.io/allocator_boilerplate.html
//
//----------------------------------------------------------------------------
template <class T>
class deferred_allocator
{
deferred_heap& h;
public:
deferred_heap& heap() const {
return h;
}
using value_type = T;
using pointer = deferred_ptr<value_type>;
using const_pointer = deferred_ptr<const value_type>;
using void_pointer = deferred_ptr<void>;
using const_void_pointer = deferred_ptr<const void>;
using difference_type = ptrdiff_t;
using size_type = std::size_t;
template <class U>
struct rebind
{
using other = deferred_allocator<U>;
};
deferred_allocator(deferred_heap& h_) noexcept
: h{ h_ }
{
}
template <class U>
deferred_allocator(deferred_allocator<U> const& that) noexcept
: h{ that.heap() }
{
}
pointer allocate(size_type n)
{
return h.allocate<value_type>(n);
}
void deallocate(pointer, size_type) noexcept
{
}
pointer allocate(size_type n, const_void_pointer)
{
return allocate(n);
}
template <class U, class ...Args>
void construct(U* p, Args&& ...args)
{
h.construct<U>(p, std::forward<Args>(args)...);
}
template <class U>
void destroy(U* p) noexcept
{
h.destroy<U>(p);
}
size_type max_size() const noexcept
{
return std::numeric_limits<size_type>::max();
}
deferred_allocator select_on_container_copy_construction() const
{
return *this; // deferred_heap is not copyable
}
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
using is_always_equal = std::true_type;
};
template <class T, class U>
inline bool operator==(deferred_allocator<T> const& a, deferred_allocator<U> const& b) noexcept
{
return &a.h == &b.h;
}
template <class T, class U>
inline bool operator!=(deferred_allocator<T> const& a, deferred_allocator<U> const& b) noexcept
{
return !(a == b);
}
//----------------------------------------------------------------------------
//
// Convenience aliases for containers
//
//----------------------------------------------------------------------------
template<class T>
using deferred_vector = std::vector<T, deferred_allocator<T>>;
template<class T>
using deferred_list = std::list<T, deferred_allocator<T>>;
template<class K, class C = std::less<K>>
using deferred_set = std::set<K, C, deferred_allocator<K>>;
template<class K, class T, class C = std::less<K>>
using deferred_multiset = std::multiset<K, C, deferred_allocator<K>>;
template<class K, class T, class C = std::less<K>>
using deferred_map = std::map<K, T, C, deferred_allocator<std::pair<const K, T>>>;
template<class K, class T, class C = std::less<K>>
using deferred_multimap = std::multimap<K, T, C, deferred_allocator<std::pair<const K, T>>>;
template<class K, class H = std::hash<K>, class E = std::equal_to<K>>
using deferred_unordered_set = std::unordered_set<K, H, E, deferred_allocator<K>>;
template<class K, class H = std::hash<K>, class E = std::equal_to<K>>
using deferred_unordered_multiset = std::unordered_multiset<K, H, E, deferred_allocator<K>>;
template<class K, class T, class H = std::hash<K>, class E = std::equal_to<K>>
using deferred_unordered_map = std::unordered_map<K, T, H, E, deferred_allocator<std::pair<const K, T>>>;
template<class K, class T, class H = std::hash<K>, class E = std::equal_to<K>>
using deferred_unordered_multimap = std::unordered_multimap<K, H, E, deferred_allocator<std::pair<const K, T>>>;
}
#endif