blob: 08e243b64440f0b89345f03f425ddb399964d36a [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
27#include "base/logging.h"
28#include "base/macros.h"
29#include "base/mutex.h"
Andreas Gampee21dc3d2014-12-08 16:59:43 -080030
31namespace art {
32
Andreas Gampee21dc3d2014-12-08 16:59:43 -080033// An arena pool that creates arenas backed by an mmaped file.
34class SwapSpace {
35 public:
36 SwapSpace(int fd, size_t initial_size);
37 ~SwapSpace();
Mathieu Chartier90443472015-07-16 20:32:27 -070038 void* Alloc(size_t size) REQUIRES(!lock_);
39 void Free(void* ptr, size_t size) REQUIRES(!lock_);
Andreas Gampee21dc3d2014-12-08 16:59:43 -080040
41 size_t GetSize() {
42 return size_;
43 }
44
45 private:
Vladimir Markoef9230b2015-10-06 13:51:55 +010046 // Chunk of space.
47 struct SpaceChunk {
Vladimir Marko3e107022017-02-22 10:57:03 +000048 // We need mutable members as we keep these objects in a std::set<> (providing only const
49 // access) but we modify these members while carefully preserving the std::set<> ordering.
50 mutable uint8_t* ptr;
51 mutable size_t size;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080052
Vladimir Markoef9230b2015-10-06 13:51:55 +010053 uintptr_t Start() const {
54 return reinterpret_cast<uintptr_t>(ptr);
55 }
56 uintptr_t End() const {
57 return reinterpret_cast<uintptr_t>(ptr) + size;
58 }
59 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080060
Vladimir Markoef9230b2015-10-06 13:51:55 +010061 class SortChunkByPtr {
62 public:
63 bool operator()(const SpaceChunk& a, const SpaceChunk& b) const {
64 return reinterpret_cast<uintptr_t>(a.ptr) < reinterpret_cast<uintptr_t>(b.ptr);
65 }
66 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080067
Andreas Gampee21dc3d2014-12-08 16:59:43 -080068 typedef std::set<SpaceChunk, SortChunkByPtr> FreeByStartSet;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080069
70 // Map size to an iterator to free_by_start_'s entry.
Vladimir Marko3e107022017-02-22 10:57:03 +000071 struct FreeBySizeEntry {
72 FreeBySizeEntry(size_t sz, FreeByStartSet::const_iterator entry)
73 : size(sz), free_by_start_entry(entry) { }
74
75 // We need mutable members as we keep these objects in a std::set<> (providing only const
76 // access) but we modify these members while carefully preserving the std::set<> ordering.
77 mutable size_t size;
78 mutable FreeByStartSet::const_iterator free_by_start_entry;
79 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080080 struct FreeBySizeComparator {
Yi Kong88307ed2017-04-25 22:33:06 -070081 bool operator()(const FreeBySizeEntry& lhs, const FreeBySizeEntry& rhs) const {
Vladimir Marko3e107022017-02-22 10:57:03 +000082 if (lhs.size != rhs.size) {
83 return lhs.size < rhs.size;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080084 } else {
Vladimir Marko3e107022017-02-22 10:57:03 +000085 return lhs.free_by_start_entry->Start() < rhs.free_by_start_entry->Start();
Andreas Gampee21dc3d2014-12-08 16:59:43 -080086 }
87 }
88 };
89 typedef std::set<FreeBySizeEntry, FreeBySizeComparator> FreeBySizeSet;
Vladimir Markoef9230b2015-10-06 13:51:55 +010090
91 SpaceChunk NewFileChunk(size_t min_size) REQUIRES(lock_);
92
93 void RemoveChunk(FreeBySizeSet::const_iterator free_by_size_pos) REQUIRES(lock_);
94 void InsertChunk(const SpaceChunk& chunk) REQUIRES(lock_);
95
96 int fd_;
97 size_t size_;
Vladimir Markoef9230b2015-10-06 13:51:55 +010098
99 // NOTE: Boost.Bimap would be useful for the two following members.
100
101 // Map start of a free chunk to its size.
102 FreeByStartSet free_by_start_ GUARDED_BY(lock_);
103 // Free chunks ordered by size.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800104 FreeBySizeSet free_by_size_ GUARDED_BY(lock_);
105
106 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
107 DISALLOW_COPY_AND_ASSIGN(SwapSpace);
108};
109
110template <typename T> class SwapAllocator;
111
112template <>
113class SwapAllocator<void> {
114 public:
115 typedef void value_type;
116 typedef void* pointer;
117 typedef const void* const_pointer;
118
119 template <typename U>
120 struct rebind {
121 typedef SwapAllocator<U> other;
122 };
123
124 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
125
126 template <typename U>
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700127 SwapAllocator(const SwapAllocator<U>& other) // NOLINT, implicit
128 : swap_space_(other.swap_space_) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800129
130 SwapAllocator(const SwapAllocator& other) = default;
131 SwapAllocator& operator=(const SwapAllocator& other) = default;
132 ~SwapAllocator() = default;
133
134 private:
135 SwapSpace* swap_space_;
136
137 template <typename U>
138 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100139
140 template <typename U>
141 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800142};
143
144template <typename T>
145class SwapAllocator {
146 public:
147 typedef T value_type;
148 typedef T* pointer;
149 typedef T& reference;
150 typedef const T* const_pointer;
151 typedef const T& const_reference;
152 typedef size_t size_type;
153 typedef ptrdiff_t difference_type;
154
155 template <typename U>
156 struct rebind {
157 typedef SwapAllocator<U> other;
158 };
159
160 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
161
162 template <typename U>
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700163 SwapAllocator(const SwapAllocator<U>& other) // NOLINT, implicit
164 : swap_space_(other.swap_space_) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800165
166 SwapAllocator(const SwapAllocator& other) = default;
167 SwapAllocator& operator=(const SwapAllocator& other) = default;
168 ~SwapAllocator() = default;
169
170 size_type max_size() const {
171 return static_cast<size_type>(-1) / sizeof(T);
172 }
173
174 pointer address(reference x) const { return &x; }
175 const_pointer address(const_reference x) const { return &x; }
176
177 pointer allocate(size_type n, SwapAllocator<void>::pointer hint ATTRIBUTE_UNUSED = nullptr) {
178 DCHECK_LE(n, max_size());
179 if (swap_space_ == nullptr) {
Vladimir Marko305ff2d2015-09-04 11:10:40 +0100180 T* result = reinterpret_cast<T*>(malloc(n * sizeof(T)));
181 CHECK(result != nullptr || n == 0u); // Abort if malloc() fails.
182 return result;
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800183 } else {
184 return reinterpret_cast<T*>(swap_space_->Alloc(n * sizeof(T)));
185 }
186 }
187 void deallocate(pointer p, size_type n) {
188 if (swap_space_ == nullptr) {
189 free(p);
190 } else {
191 swap_space_->Free(p, n * sizeof(T));
192 }
193 }
194
195 void construct(pointer p, const_reference val) {
196 new (static_cast<void*>(p)) value_type(val);
197 }
198 template <class U, class... Args>
199 void construct(U* p, Args&&... args) {
200 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
201 }
202 void destroy(pointer p) {
203 p->~value_type();
204 }
205
206 inline bool operator==(SwapAllocator const& other) {
207 return swap_space_ == other.swap_space_;
208 }
209 inline bool operator!=(SwapAllocator const& other) {
210 return !operator==(other);
211 }
212
213 private:
214 SwapSpace* swap_space_;
215
216 template <typename U>
217 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100218
219 template <typename U>
220 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800221};
222
223template <typename T>
Vladimir Markoef9230b2015-10-06 13:51:55 +0100224inline bool operator==(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
225 return lhs.swap_space_ == rhs.swap_space_;
226}
227
228template <typename T>
229inline bool operator!=(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
230 return !(lhs == rhs);
231}
232
233template <typename T>
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800234using SwapVector = std::vector<T, SwapAllocator<T>>;
235template <typename T, typename Comparator>
236using SwapSet = std::set<T, Comparator, SwapAllocator<T>>;
237
238} // namespace art
239
240#endif // ART_COMPILER_UTILS_SWAP_SPACE_H_