blob: 837a12b7b54dc93ad946d25515bcc496603201da [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>
Colin Crossc1228c72016-03-04 16:36:12 -080027#include <unordered_map>
Colin Crossbcb4ed32016-01-14 15:35:40 -080028#include <unordered_set>
29#include <vector>
Colin Crossa9939e92017-06-21 13:13:00 -070030
31namespace android {
32
Colin Crossbcb4ed32016-01-14 15:35:40 -080033extern std::atomic<int> heap_count;
34
35class HeapImpl;
36
Colin Crossa83881e2017-06-22 10:50:05 -070037template <typename T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080038class Allocator;
39
Colin Crossbcb4ed32016-01-14 15:35:40 -080040// Non-templated class that implements wraps HeapImpl to keep
41// implementation out of the header file
42class Heap {
Colin Crossa83881e2017-06-22 10:50:05 -070043 public:
Colin Crossbcb4ed32016-01-14 15:35:40 -080044 Heap();
45 ~Heap();
46
47 // Copy constructor that does not take ownership of impl_
48 Heap(const Heap& other) : impl_(other.impl_), owns_impl_(false) {}
49
50 // Assignment disabled
51 Heap& operator=(const Heap&) = delete;
52
53 // Allocate size bytes
54 void* allocate(size_t size);
55
56 // Deallocate allocation returned by allocate
57 void deallocate(void*);
58
59 bool empty();
60
61 static void deallocate(HeapImpl* impl, void* ptr);
62
63 // Allocate a class of type T
Colin Crossa83881e2017-06-22 10:50:05 -070064 template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080065 T* allocate() {
66 return reinterpret_cast<T*>(allocate(sizeof(T)));
67 }
68
69 // Comparators, copied objects will be equal
Colin Crossa83881e2017-06-22 10:50:05 -070070 bool operator==(const Heap& other) const { return impl_ == other.impl_; }
71 bool operator!=(const Heap& other) const { return !(*this == other); }
Colin Crossbcb4ed32016-01-14 15:35:40 -080072
73 // std::unique_ptr wrapper that allocates using allocate and deletes using
74 // deallocate
Colin Crossa83881e2017-06-22 10:50:05 -070075 template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080076 using unique_ptr = std::unique_ptr<T, std::function<void(void*)>>;
77
Colin Crossa83881e2017-06-22 10:50:05 -070078 template <class T, class... Args>
Colin Crossbcb4ed32016-01-14 15:35:40 -080079 unique_ptr<T> make_unique(Args&&... args) {
80 HeapImpl* impl = impl_;
Colin Crossa83881e2017-06-22 10:50:05 -070081 return unique_ptr<T>(new (allocate<T>()) T(std::forward<Args>(args)...), [impl](void* ptr) {
82 reinterpret_cast<T*>(ptr)->~T();
83 deallocate(impl, ptr);
84 });
Colin Crossbcb4ed32016-01-14 15:35:40 -080085 }
86
87 // std::unique_ptr wrapper that allocates using allocate and deletes using
88 // deallocate
Colin Crossa83881e2017-06-22 10:50:05 -070089 template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080090 using shared_ptr = std::shared_ptr<T>;
91
Colin Crossa83881e2017-06-22 10:50:05 -070092 template <class T, class... Args>
Colin Crossbcb4ed32016-01-14 15:35:40 -080093 shared_ptr<T> make_shared(Args&&... args);
94
Colin Crossa83881e2017-06-22 10:50:05 -070095 protected:
Colin Crossbcb4ed32016-01-14 15:35:40 -080096 HeapImpl* impl_;
97 bool owns_impl_;
98};
99
100// STLAllocator implements the std allocator interface on top of a Heap
Colin Crossa83881e2017-06-22 10:50:05 -0700101template <typename T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800102class STLAllocator {
Colin Crossa83881e2017-06-22 10:50:05 -0700103 public:
Colin Crossbcb4ed32016-01-14 15:35:40 -0800104 using value_type = T;
Colin Crossa83881e2017-06-22 10:50:05 -0700105 ~STLAllocator() {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800106
107 // Construct an STLAllocator on top of a Heap
Colin Crossa83881e2017-06-22 10:50:05 -0700108 STLAllocator(const Heap& heap)
109 : // NOLINT, implicit
110 heap_(heap) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800111
112 // Rebind an STLAllocator from an another STLAllocator
Colin Crossa83881e2017-06-22 10:50:05 -0700113 template <typename U>
114 STLAllocator(const STLAllocator<U>& other)
115 : // NOLINT, implicit
116 heap_(other.heap_) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800117
118 STLAllocator(const STLAllocator&) = default;
119 STLAllocator<T>& operator=(const STLAllocator<T>&) = default;
120
Colin Crossa83881e2017-06-22 10:50:05 -0700121 T* allocate(std::size_t n) { return reinterpret_cast<T*>(heap_.allocate(n * sizeof(T))); }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800122
Colin Crossa83881e2017-06-22 10:50:05 -0700123 void deallocate(T* ptr, std::size_t) { heap_.deallocate(ptr); }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800124
Colin Crossa83881e2017-06-22 10:50:05 -0700125 template <typename U>
126 bool operator==(const STLAllocator<U>& other) const {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800127 return heap_ == other.heap_;
128 }
Colin Crossa83881e2017-06-22 10:50:05 -0700129 template <typename U>
130 inline bool operator!=(const STLAllocator<U>& other) const {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800131 return !(this == other);
132 }
133
Colin Crossa83881e2017-06-22 10:50:05 -0700134 template <typename U>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800135 friend class STLAllocator;
136
Colin Crossa83881e2017-06-22 10:50:05 -0700137 protected:
Colin Crossbcb4ed32016-01-14 15:35:40 -0800138 Heap heap_;
139};
140
Colin Crossbcb4ed32016-01-14 15:35:40 -0800141// Allocator extends STLAllocator with some convenience methods for allocating
142// a single object and for constructing unique_ptr and shared_ptr objects with
143// appropriate deleters.
Colin Crossa83881e2017-06-22 10:50:05 -0700144template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800145class Allocator : public STLAllocator<T> {
146 public:
147 ~Allocator() {}
148
Colin Crossa83881e2017-06-22 10:50:05 -0700149 Allocator(const Heap& other)
150 : // NOLINT, implicit
151 STLAllocator<T>(other) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800152
Colin Crossa83881e2017-06-22 10:50:05 -0700153 template <typename U>
154 Allocator(const STLAllocator<U>& other)
155 : // NOLINT, implicit
156 STLAllocator<T>(other) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800157
158 Allocator(const Allocator&) = default;
159 Allocator<T>& operator=(const Allocator<T>&) = default;
160
161 using STLAllocator<T>::allocate;
162 using STLAllocator<T>::deallocate;
163 using STLAllocator<T>::heap_;
164
Colin Crossa83881e2017-06-22 10:50:05 -0700165 T* allocate() { return STLAllocator<T>::allocate(1); }
166 void deallocate(void* ptr) { heap_.deallocate(ptr); }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800167
168 using shared_ptr = Heap::shared_ptr<T>;
169
Colin Crossa83881e2017-06-22 10:50:05 -0700170 template <class... Args>
171 shared_ptr make_shared(Args&&... args) {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800172 return heap_.template make_shared<T>(std::forward<Args>(args)...);
173 }
174
175 using unique_ptr = Heap::unique_ptr<T>;
176
Colin Crossa83881e2017-06-22 10:50:05 -0700177 template <class... Args>
178 unique_ptr make_unique(Args&&... args) {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800179 return heap_.template make_unique<T>(std::forward<Args>(args)...);
180 }
181};
182
183// std::unique_ptr wrapper that allocates using allocate and deletes using
184// deallocate. Implemented outside class definition in order to pass
185// Allocator<T> to shared_ptr.
Colin Crossa83881e2017-06-22 10:50:05 -0700186template <class T, class... Args>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800187inline Heap::shared_ptr<T> Heap::make_shared(Args&&... args) {
188 return std::allocate_shared<T, Allocator<T>, Args...>(Allocator<T>(*this),
Colin Crossa83881e2017-06-22 10:50:05 -0700189 std::forward<Args>(args)...);
Colin Crossbcb4ed32016-01-14 15:35:40 -0800190}
191
192namespace allocator {
193
Colin Crossa83881e2017-06-22 10:50:05 -0700194template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800195using vector = std::vector<T, Allocator<T>>;
196
Colin Crossa83881e2017-06-22 10:50:05 -0700197template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800198using list = std::list<T, Allocator<T>>;
199
Colin Crossa83881e2017-06-22 10:50:05 -0700200template <class Key, class T, class Compare = std::less<Key>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800201using map = std::map<Key, T, Compare, Allocator<std::pair<const Key, T>>>;
202
Colin Crossa83881e2017-06-22 10:50:05 -0700203template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
204using unordered_map =
205 std::unordered_map<Key, T, Hash, KeyEqual, Allocator<std::pair<const Key, T>>>;
Colin Crossc1228c72016-03-04 16:36:12 -0800206
Colin Crossa83881e2017-06-22 10:50:05 -0700207template <class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800208using unordered_set = std::unordered_set<Key, Hash, KeyEqual, Allocator<Key>>;
209
Colin Crossa83881e2017-06-22 10:50:05 -0700210template <class Key, class Compare = std::less<Key>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800211using set = std::set<Key, Compare, Allocator<Key>>;
212
213using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
214}
215
Colin Crossa9939e92017-06-21 13:13:00 -0700216} // namespace android
217
Colin Crossbcb4ed32016-01-14 15:35:40 -0800218#endif