blob: 67a068f7d4f17f191363838a2627677c77b57952 [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>
30extern std::atomic<int> heap_count;
31
32class HeapImpl;
33
Colin Crossa83881e2017-06-22 10:50:05 -070034template <typename T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080035class Allocator;
36
Colin Crossbcb4ed32016-01-14 15:35:40 -080037// Non-templated class that implements wraps HeapImpl to keep
38// implementation out of the header file
39class Heap {
Colin Crossa83881e2017-06-22 10:50:05 -070040 public:
Colin Crossbcb4ed32016-01-14 15:35:40 -080041 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
Colin Crossa83881e2017-06-22 10:50:05 -070061 template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080062 T* allocate() {
63 return reinterpret_cast<T*>(allocate(sizeof(T)));
64 }
65
66 // Comparators, copied objects will be equal
Colin Crossa83881e2017-06-22 10:50:05 -070067 bool operator==(const Heap& other) const { return impl_ == other.impl_; }
68 bool operator!=(const Heap& other) const { return !(*this == other); }
Colin Crossbcb4ed32016-01-14 15:35:40 -080069
70 // std::unique_ptr wrapper that allocates using allocate and deletes using
71 // deallocate
Colin Crossa83881e2017-06-22 10:50:05 -070072 template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080073 using unique_ptr = std::unique_ptr<T, std::function<void(void*)>>;
74
Colin Crossa83881e2017-06-22 10:50:05 -070075 template <class T, class... Args>
Colin Crossbcb4ed32016-01-14 15:35:40 -080076 unique_ptr<T> make_unique(Args&&... args) {
77 HeapImpl* impl = impl_;
Colin Crossa83881e2017-06-22 10:50:05 -070078 return unique_ptr<T>(new (allocate<T>()) T(std::forward<Args>(args)...), [impl](void* ptr) {
79 reinterpret_cast<T*>(ptr)->~T();
80 deallocate(impl, ptr);
81 });
Colin Crossbcb4ed32016-01-14 15:35:40 -080082 }
83
84 // std::unique_ptr wrapper that allocates using allocate and deletes using
85 // deallocate
Colin Crossa83881e2017-06-22 10:50:05 -070086 template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080087 using shared_ptr = std::shared_ptr<T>;
88
Colin Crossa83881e2017-06-22 10:50:05 -070089 template <class T, class... Args>
Colin Crossbcb4ed32016-01-14 15:35:40 -080090 shared_ptr<T> make_shared(Args&&... args);
91
Colin Crossa83881e2017-06-22 10:50:05 -070092 protected:
Colin Crossbcb4ed32016-01-14 15:35:40 -080093 HeapImpl* impl_;
94 bool owns_impl_;
95};
96
97// STLAllocator implements the std allocator interface on top of a Heap
Colin Crossa83881e2017-06-22 10:50:05 -070098template <typename T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080099class STLAllocator {
Colin Crossa83881e2017-06-22 10:50:05 -0700100 public:
Colin Crossbcb4ed32016-01-14 15:35:40 -0800101 using value_type = T;
Colin Crossa83881e2017-06-22 10:50:05 -0700102 ~STLAllocator() {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800103
104 // Construct an STLAllocator on top of a Heap
Colin Crossa83881e2017-06-22 10:50:05 -0700105 STLAllocator(const Heap& heap)
106 : // NOLINT, implicit
107 heap_(heap) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800108
109 // Rebind an STLAllocator from an another STLAllocator
Colin Crossa83881e2017-06-22 10:50:05 -0700110 template <typename U>
111 STLAllocator(const STLAllocator<U>& other)
112 : // NOLINT, implicit
113 heap_(other.heap_) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800114
115 STLAllocator(const STLAllocator&) = default;
116 STLAllocator<T>& operator=(const STLAllocator<T>&) = default;
117
Colin Crossa83881e2017-06-22 10:50:05 -0700118 T* allocate(std::size_t n) { return reinterpret_cast<T*>(heap_.allocate(n * sizeof(T))); }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800119
Colin Crossa83881e2017-06-22 10:50:05 -0700120 void deallocate(T* ptr, std::size_t) { heap_.deallocate(ptr); }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800121
Colin Crossa83881e2017-06-22 10:50:05 -0700122 template <typename U>
123 bool operator==(const STLAllocator<U>& other) const {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800124 return heap_ == other.heap_;
125 }
Colin Crossa83881e2017-06-22 10:50:05 -0700126 template <typename U>
127 inline bool operator!=(const STLAllocator<U>& other) const {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800128 return !(this == other);
129 }
130
Colin Crossa83881e2017-06-22 10:50:05 -0700131 template <typename U>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800132 friend class STLAllocator;
133
Colin Crossa83881e2017-06-22 10:50:05 -0700134 protected:
Colin Crossbcb4ed32016-01-14 15:35:40 -0800135 Heap heap_;
136};
137
Colin Crossbcb4ed32016-01-14 15:35:40 -0800138// Allocator extends STLAllocator with some convenience methods for allocating
139// a single object and for constructing unique_ptr and shared_ptr objects with
140// appropriate deleters.
Colin Crossa83881e2017-06-22 10:50:05 -0700141template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800142class Allocator : public STLAllocator<T> {
143 public:
144 ~Allocator() {}
145
Colin Crossa83881e2017-06-22 10:50:05 -0700146 Allocator(const Heap& other)
147 : // NOLINT, implicit
148 STLAllocator<T>(other) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800149
Colin Crossa83881e2017-06-22 10:50:05 -0700150 template <typename U>
151 Allocator(const STLAllocator<U>& other)
152 : // NOLINT, implicit
153 STLAllocator<T>(other) {}
Colin Crossbcb4ed32016-01-14 15:35:40 -0800154
155 Allocator(const Allocator&) = default;
156 Allocator<T>& operator=(const Allocator<T>&) = default;
157
158 using STLAllocator<T>::allocate;
159 using STLAllocator<T>::deallocate;
160 using STLAllocator<T>::heap_;
161
Colin Crossa83881e2017-06-22 10:50:05 -0700162 T* allocate() { return STLAllocator<T>::allocate(1); }
163 void deallocate(void* ptr) { heap_.deallocate(ptr); }
Colin Crossbcb4ed32016-01-14 15:35:40 -0800164
165 using shared_ptr = Heap::shared_ptr<T>;
166
Colin Crossa83881e2017-06-22 10:50:05 -0700167 template <class... Args>
168 shared_ptr make_shared(Args&&... args) {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800169 return heap_.template make_shared<T>(std::forward<Args>(args)...);
170 }
171
172 using unique_ptr = Heap::unique_ptr<T>;
173
Colin Crossa83881e2017-06-22 10:50:05 -0700174 template <class... Args>
175 unique_ptr make_unique(Args&&... args) {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800176 return heap_.template make_unique<T>(std::forward<Args>(args)...);
177 }
178};
179
180// std::unique_ptr wrapper that allocates using allocate and deletes using
181// deallocate. Implemented outside class definition in order to pass
182// Allocator<T> to shared_ptr.
Colin Crossa83881e2017-06-22 10:50:05 -0700183template <class T, class... Args>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800184inline Heap::shared_ptr<T> Heap::make_shared(Args&&... args) {
185 return std::allocate_shared<T, Allocator<T>, Args...>(Allocator<T>(*this),
Colin Crossa83881e2017-06-22 10:50:05 -0700186 std::forward<Args>(args)...);
Colin Crossbcb4ed32016-01-14 15:35:40 -0800187}
188
189namespace allocator {
190
Colin Crossa83881e2017-06-22 10:50:05 -0700191template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800192using vector = std::vector<T, Allocator<T>>;
193
Colin Crossa83881e2017-06-22 10:50:05 -0700194template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800195using list = std::list<T, Allocator<T>>;
196
Colin Crossa83881e2017-06-22 10:50:05 -0700197template <class Key, class T, class Compare = std::less<Key>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800198using map = std::map<Key, T, Compare, Allocator<std::pair<const Key, T>>>;
199
Colin Crossa83881e2017-06-22 10:50:05 -0700200template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
201using unordered_map =
202 std::unordered_map<Key, T, Hash, KeyEqual, Allocator<std::pair<const Key, T>>>;
Colin Crossc1228c72016-03-04 16:36:12 -0800203
Colin Crossa83881e2017-06-22 10:50:05 -0700204template <class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800205using unordered_set = std::unordered_set<Key, Hash, KeyEqual, Allocator<Key>>;
206
Colin Crossa83881e2017-06-22 10:50:05 -0700207template <class Key, class Compare = std::less<Key>>
Colin Crossbcb4ed32016-01-14 15:35:40 -0800208using set = std::set<Key, Compare, Allocator<Key>>;
209
210using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
211}
212
213#endif