blob: 76df52710854a0d1cd5d9ee652e8ea3ccc2d3ebe [file] [log] [blame]
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001/*
2 * Copyright (C) 2014 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 ART_COMPILER_UTILS_SWAP_SPACE_H_
18#define ART_COMPILER_UTILS_SWAP_SPACE_H_
19
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <stddef.h>
21#include <stdint.h>
Andreas Gampee21dc3d2014-12-08 16:59:43 -080022#include <cstdlib>
23#include <list>
24#include <set>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include <vector>
Andreas Gampee21dc3d2014-12-08 16:59:43 -080026
Andreas Gampe57943812017-12-06 21:39:13 -080027#include <android-base/logging.h>
28
Andreas Gampee21dc3d2014-12-08 16:59:43 -080029#include "base/macros.h"
30#include "base/mutex.h"
Andreas Gampee21dc3d2014-12-08 16:59:43 -080031
32namespace art {
33
Andreas Gampee21dc3d2014-12-08 16:59:43 -080034// An arena pool that creates arenas backed by an mmaped file.
35class SwapSpace {
36 public:
37 SwapSpace(int fd, size_t initial_size);
38 ~SwapSpace();
Mathieu Chartier90443472015-07-16 20:32:27 -070039 void* Alloc(size_t size) REQUIRES(!lock_);
40 void Free(void* ptr, size_t size) REQUIRES(!lock_);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080041
42 size_t GetSize() {
43 return size_;
44 }
45
46 private:
Vladimir Markoef9230b2015-10-06 13:51:55 +010047 // Chunk of space.
48 struct SpaceChunk {
Vladimir Marko3e107022017-02-22 10:57:03 +000049 // We need mutable members as we keep these objects in a std::set<> (providing only const
50 // access) but we modify these members while carefully preserving the std::set<> ordering.
51 mutable uint8_t* ptr;
52 mutable size_t size;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080053
Vladimir Markoef9230b2015-10-06 13:51:55 +010054 uintptr_t Start() const {
55 return reinterpret_cast<uintptr_t>(ptr);
56 }
57 uintptr_t End() const {
58 return reinterpret_cast<uintptr_t>(ptr) + size;
59 }
60 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080061
Vladimir Markoef9230b2015-10-06 13:51:55 +010062 class SortChunkByPtr {
63 public:
64 bool operator()(const SpaceChunk& a, const SpaceChunk& b) const {
65 return reinterpret_cast<uintptr_t>(a.ptr) < reinterpret_cast<uintptr_t>(b.ptr);
66 }
67 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080068
Andreas Gampee21dc3d2014-12-08 16:59:43 -080069 typedef std::set<SpaceChunk, SortChunkByPtr> FreeByStartSet;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080070
71 // Map size to an iterator to free_by_start_'s entry.
Vladimir Marko3e107022017-02-22 10:57:03 +000072 struct FreeBySizeEntry {
73 FreeBySizeEntry(size_t sz, FreeByStartSet::const_iterator entry)
74 : size(sz), free_by_start_entry(entry) { }
75
76 // We need mutable members as we keep these objects in a std::set<> (providing only const
77 // access) but we modify these members while carefully preserving the std::set<> ordering.
78 mutable size_t size;
79 mutable FreeByStartSet::const_iterator free_by_start_entry;
80 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080081 struct FreeBySizeComparator {
Yi Kong88307ed2017-04-25 22:33:06 -070082 bool operator()(const FreeBySizeEntry& lhs, const FreeBySizeEntry& rhs) const {
Vladimir Marko3e107022017-02-22 10:57:03 +000083 if (lhs.size != rhs.size) {
84 return lhs.size < rhs.size;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080085 } else {
Vladimir Marko3e107022017-02-22 10:57:03 +000086 return lhs.free_by_start_entry->Start() < rhs.free_by_start_entry->Start();
Andreas Gampee21dc3d2014-12-08 16:59:43 -080087 }
88 }
89 };
90 typedef std::set<FreeBySizeEntry, FreeBySizeComparator> FreeBySizeSet;
Vladimir Markoef9230b2015-10-06 13:51:55 +010091
92 SpaceChunk NewFileChunk(size_t min_size) REQUIRES(lock_);
93
94 void RemoveChunk(FreeBySizeSet::const_iterator free_by_size_pos) REQUIRES(lock_);
95 void InsertChunk(const SpaceChunk& chunk) REQUIRES(lock_);
96
97 int fd_;
98 size_t size_;
Vladimir Markoef9230b2015-10-06 13:51:55 +010099
100 // NOTE: Boost.Bimap would be useful for the two following members.
101
102 // Map start of a free chunk to its size.
103 FreeByStartSet free_by_start_ GUARDED_BY(lock_);
104 // Free chunks ordered by size.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800105 FreeBySizeSet free_by_size_ GUARDED_BY(lock_);
106
107 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
108 DISALLOW_COPY_AND_ASSIGN(SwapSpace);
109};
110
111template <typename T> class SwapAllocator;
112
113template <>
114class SwapAllocator<void> {
115 public:
116 typedef void value_type;
117 typedef void* pointer;
118 typedef const void* const_pointer;
119
120 template <typename U>
121 struct rebind {
122 typedef SwapAllocator<U> other;
123 };
124
125 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
126
127 template <typename U>
Igor Murashkin5573c372017-11-16 13:34:30 -0800128 SwapAllocator(const SwapAllocator<U>& other)
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700129 : swap_space_(other.swap_space_) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800130
131 SwapAllocator(const SwapAllocator& other) = default;
132 SwapAllocator& operator=(const SwapAllocator& other) = default;
133 ~SwapAllocator() = default;
134
135 private:
136 SwapSpace* swap_space_;
137
138 template <typename U>
139 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100140
141 template <typename U>
142 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800143};
144
145template <typename T>
146class SwapAllocator {
147 public:
148 typedef T value_type;
149 typedef T* pointer;
150 typedef T& reference;
151 typedef const T* const_pointer;
152 typedef const T& const_reference;
153 typedef size_t size_type;
154 typedef ptrdiff_t difference_type;
155
156 template <typename U>
157 struct rebind {
158 typedef SwapAllocator<U> other;
159 };
160
161 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
162
163 template <typename U>
Igor Murashkin5573c372017-11-16 13:34:30 -0800164 SwapAllocator(const SwapAllocator<U>& other)
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700165 : swap_space_(other.swap_space_) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800166
167 SwapAllocator(const SwapAllocator& other) = default;
168 SwapAllocator& operator=(const SwapAllocator& other) = default;
169 ~SwapAllocator() = default;
170
171 size_type max_size() const {
172 return static_cast<size_type>(-1) / sizeof(T);
173 }
174
175 pointer address(reference x) const { return &x; }
176 const_pointer address(const_reference x) const { return &x; }
177
178 pointer allocate(size_type n, SwapAllocator<void>::pointer hint ATTRIBUTE_UNUSED = nullptr) {
179 DCHECK_LE(n, max_size());
180 if (swap_space_ == nullptr) {
Vladimir Marko305ff2d2015-09-04 11:10:40 +0100181 T* result = reinterpret_cast<T*>(malloc(n * sizeof(T)));
182 CHECK(result != nullptr || n == 0u); // Abort if malloc() fails.
183 return result;
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800184 } else {
185 return reinterpret_cast<T*>(swap_space_->Alloc(n * sizeof(T)));
186 }
187 }
188 void deallocate(pointer p, size_type n) {
189 if (swap_space_ == nullptr) {
190 free(p);
191 } else {
192 swap_space_->Free(p, n * sizeof(T));
193 }
194 }
195
196 void construct(pointer p, const_reference val) {
197 new (static_cast<void*>(p)) value_type(val);
198 }
199 template <class U, class... Args>
200 void construct(U* p, Args&&... args) {
201 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
202 }
203 void destroy(pointer p) {
204 p->~value_type();
205 }
206
207 inline bool operator==(SwapAllocator const& other) {
208 return swap_space_ == other.swap_space_;
209 }
210 inline bool operator!=(SwapAllocator const& other) {
211 return !operator==(other);
212 }
213
214 private:
215 SwapSpace* swap_space_;
216
217 template <typename U>
218 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100219
220 template <typename U>
221 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800222};
223
224template <typename T>
Vladimir Markoef9230b2015-10-06 13:51:55 +0100225inline bool operator==(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
226 return lhs.swap_space_ == rhs.swap_space_;
227}
228
229template <typename T>
230inline bool operator!=(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
231 return !(lhs == rhs);
232}
233
234template <typename T>
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800235using SwapVector = std::vector<T, SwapAllocator<T>>;
236template <typename T, typename Comparator>
237using SwapSet = std::set<T, Comparator, SwapAllocator<T>>;
238
239} // namespace art
240
241#endif // ART_COMPILER_UTILS_SWAP_SPACE_H_