blob: b0a4d4cfbac5c1529b443a8b6ea9afb1b5e4e0a3 [file] [log] [blame]
Colin Crossbcb4ed32016-01-14 15:35:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef LIBMEMUNREACHABLE_ALLOCATOR_H_
18#define LIBMEMUNREACHABLE_ALLOCATOR_H_
19
20#include <atomic>
21#include <cstddef>
22#include <functional>
23#include <list>
24#include <map>
25#include <memory>
26#include <set>
27#include <unordered_set>
28#include <vector>
29extern std::atomic<int> heap_count;
30
31class HeapImpl;
32
33template<typename T>
34class Allocator;
35
36
37// Non-templated class that implements wraps HeapImpl to keep
38// implementation out of the header file
39class Heap {
40public:
41 Heap();
42 ~Heap();
43
44 // Copy constructor that does not take ownership of impl_
45 Heap(const Heap& other) : impl_(other.impl_), owns_impl_(false) {}
46
47 // Assignment disabled
48 Heap& operator=(const Heap&) = delete;
49
50 // Allocate size bytes
51 void* allocate(size_t size);
52
53 // Deallocate allocation returned by allocate
54 void deallocate(void*);
55
56 bool empty();
57
58 static void deallocate(HeapImpl* impl, void* ptr);
59
60 // Allocate a class of type T
61 template<class T>
62 T* allocate() {
63 return reinterpret_cast<T*>(allocate(sizeof(T)));
64 }
65
66 // Comparators, copied objects will be equal
67 bool operator ==(const Heap& other) const {
68 return impl_ == other.impl_;
69 }
70 bool operator !=(const Heap& other) const {
71 return !(*this == other);
72 }
73
74 // std::unique_ptr wrapper that allocates using allocate and deletes using
75 // deallocate
76 template<class T>
77 using unique_ptr = std::unique_ptr<T, std::function<void(void*)>>;
78
79 template<class T, class... Args>
80 unique_ptr<T> make_unique(Args&&... args) {
81 HeapImpl* impl = impl_;
82 return unique_ptr<T>(new (allocate<T>()) T(std::forward<Args>(args)...),
83 [impl](void* ptr) {
84 reinterpret_cast<T*>(ptr)->~T();
85 deallocate(impl, ptr);
86 });
87 }
88
89 // std::unique_ptr wrapper that allocates using allocate and deletes using
90 // deallocate
91 template<class T>
92 using shared_ptr = std::shared_ptr<T>;
93
94 template<class T, class... Args>
95 shared_ptr<T> make_shared(Args&&... args);
96
97protected:
98 HeapImpl* impl_;
99 bool owns_impl_;
100};
101
102// STLAllocator implements the std allocator interface on top of a Heap
103template<typename T>
104class STLAllocator {
105public:
106 using value_type = T;
107 ~STLAllocator() {
108 }
109
110 // Construct an STLAllocator on top of a Heap
111 STLAllocator(const Heap& heap) :
112 heap_(heap) {
113 }
114
115 // Rebind an STLAllocator from an another STLAllocator
116 template<typename U>
117 STLAllocator(const STLAllocator<U>& other) :
118 heap_(other.heap_) {
119 }
120
121 STLAllocator(const STLAllocator&) = default;
122 STLAllocator<T>& operator=(const STLAllocator<T>&) = default;
123
124 T* allocate(std::size_t n) {
125 return reinterpret_cast<T*>(heap_.allocate(n * sizeof(T)));
126 }
127
128 void deallocate(T* ptr, std::size_t) {
129 heap_.deallocate(ptr);
130 }
131
132 template<typename U>
133 bool operator ==(const STLAllocator<U>& other) const {
134 return heap_ == other.heap_;
135 }
136 template<typename U>
137 inline bool operator !=(const STLAllocator<U>& other) const {
138 return !(this == other);
139 }
140
141 template<typename U>
142 friend class STLAllocator;
143
144protected:
145 Heap heap_;
146};
147
148
149// Allocator extends STLAllocator with some convenience methods for allocating
150// a single object and for constructing unique_ptr and shared_ptr objects with
151// appropriate deleters.
152template<class T>
153class Allocator : public STLAllocator<T> {
154 public:
155 ~Allocator() {}
156
157 Allocator(const Heap& other) :
158 STLAllocator<T>(other) {
159 }
160
161 template<typename U>
162 Allocator(const STLAllocator<U>& other) :
163 STLAllocator<T>(other) {
164 }
165
166 Allocator(const Allocator&) = default;
167 Allocator<T>& operator=(const Allocator<T>&) = default;
168
169 using STLAllocator<T>::allocate;
170 using STLAllocator<T>::deallocate;
171 using STLAllocator<T>::heap_;
172
173 T* allocate() {
174 return STLAllocator<T>::allocate(1);
175 }
176 void deallocate(void* ptr) {
177 heap_.deallocate(ptr);
178 }
179
180 using shared_ptr = Heap::shared_ptr<T>;
181
182 template<class... Args>
183 shared_ptr make_shared(Args&& ...args) {
184 return heap_.template make_shared<T>(std::forward<Args>(args)...);
185 }
186
187 using unique_ptr = Heap::unique_ptr<T>;
188
189 template<class... Args>
190 unique_ptr make_unique(Args&& ...args) {
191 return heap_.template make_unique<T>(std::forward<Args>(args)...);
192 }
193};
194
195// std::unique_ptr wrapper that allocates using allocate and deletes using
196// deallocate. Implemented outside class definition in order to pass
197// Allocator<T> to shared_ptr.
198template<class T, class... Args>
199inline Heap::shared_ptr<T> Heap::make_shared(Args&&... args) {
200 return std::allocate_shared<T, Allocator<T>, Args...>(Allocator<T>(*this),
201 std::forward<Args>(args)...);
202}
203
204namespace allocator {
205
206template<class T>
207using vector = std::vector<T, Allocator<T>>;
208
209template<class T>
210using list = std::list<T, Allocator<T>>;
211
212template<class T, class Key, class Compare = std::less<Key>>
213using map = std::map<Key, T, Compare, Allocator<std::pair<const Key, T>>>;
214
215template<class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
216using unordered_set = std::unordered_set<Key, Hash, KeyEqual, Allocator<Key>>;
217
218template<class Key, class Compare = std::less<Key>>
219using set = std::set<Key, Compare, Allocator<Key>>;
220
221using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
222}
223
224#endif