blob: 9600907278976cfd8c464596e76d8004d01b6bc7 [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
20#include <cstdlib>
21#include <list>
Vladimir Markoef9230b2015-10-06 13:51:55 +010022#include <vector>
Andreas Gampee21dc3d2014-12-08 16:59:43 -080023#include <set>
24#include <stdint.h>
25#include <stddef.h>
26
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 {
48 uint8_t* ptr;
49 size_t size;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080050
Vladimir Markoef9230b2015-10-06 13:51:55 +010051 uintptr_t Start() const {
52 return reinterpret_cast<uintptr_t>(ptr);
53 }
54 uintptr_t End() const {
55 return reinterpret_cast<uintptr_t>(ptr) + size;
56 }
57 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080058
Vladimir Markoef9230b2015-10-06 13:51:55 +010059 class SortChunkByPtr {
60 public:
61 bool operator()(const SpaceChunk& a, const SpaceChunk& b) const {
62 return reinterpret_cast<uintptr_t>(a.ptr) < reinterpret_cast<uintptr_t>(b.ptr);
63 }
64 };
Andreas Gampee21dc3d2014-12-08 16:59:43 -080065
Andreas Gampee21dc3d2014-12-08 16:59:43 -080066 typedef std::set<SpaceChunk, SortChunkByPtr> FreeByStartSet;
Andreas Gampee21dc3d2014-12-08 16:59:43 -080067
68 // Map size to an iterator to free_by_start_'s entry.
69 typedef std::pair<size_t, FreeByStartSet::const_iterator> FreeBySizeEntry;
70 struct FreeBySizeComparator {
71 bool operator()(const FreeBySizeEntry& lhs, const FreeBySizeEntry& rhs) {
72 if (lhs.first != rhs.first) {
73 return lhs.first < rhs.first;
74 } else {
75 return lhs.second->Start() < rhs.second->Start();
76 }
77 }
78 };
79 typedef std::set<FreeBySizeEntry, FreeBySizeComparator> FreeBySizeSet;
Vladimir Markoef9230b2015-10-06 13:51:55 +010080
81 SpaceChunk NewFileChunk(size_t min_size) REQUIRES(lock_);
82
83 void RemoveChunk(FreeBySizeSet::const_iterator free_by_size_pos) REQUIRES(lock_);
84 void InsertChunk(const SpaceChunk& chunk) REQUIRES(lock_);
85
86 int fd_;
87 size_t size_;
Vladimir Markoef9230b2015-10-06 13:51:55 +010088
89 // NOTE: Boost.Bimap would be useful for the two following members.
90
91 // Map start of a free chunk to its size.
92 FreeByStartSet free_by_start_ GUARDED_BY(lock_);
93 // Free chunks ordered by size.
Andreas Gampee21dc3d2014-12-08 16:59:43 -080094 FreeBySizeSet free_by_size_ GUARDED_BY(lock_);
95
96 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
97 DISALLOW_COPY_AND_ASSIGN(SwapSpace);
98};
99
100template <typename T> class SwapAllocator;
101
102template <>
103class SwapAllocator<void> {
104 public:
105 typedef void value_type;
106 typedef void* pointer;
107 typedef const void* const_pointer;
108
109 template <typename U>
110 struct rebind {
111 typedef SwapAllocator<U> other;
112 };
113
114 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
115
116 template <typename U>
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700117 SwapAllocator(const SwapAllocator<U>& other) // NOLINT, implicit
118 : swap_space_(other.swap_space_) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800119
120 SwapAllocator(const SwapAllocator& other) = default;
121 SwapAllocator& operator=(const SwapAllocator& other) = default;
122 ~SwapAllocator() = default;
123
124 private:
125 SwapSpace* swap_space_;
126
127 template <typename U>
128 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100129
130 template <typename U>
131 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800132};
133
134template <typename T>
135class SwapAllocator {
136 public:
137 typedef T value_type;
138 typedef T* pointer;
139 typedef T& reference;
140 typedef const T* const_pointer;
141 typedef const T& const_reference;
142 typedef size_t size_type;
143 typedef ptrdiff_t difference_type;
144
145 template <typename U>
146 struct rebind {
147 typedef SwapAllocator<U> other;
148 };
149
150 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
151
152 template <typename U>
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700153 SwapAllocator(const SwapAllocator<U>& other) // NOLINT, implicit
154 : swap_space_(other.swap_space_) {}
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800155
156 SwapAllocator(const SwapAllocator& other) = default;
157 SwapAllocator& operator=(const SwapAllocator& other) = default;
158 ~SwapAllocator() = default;
159
160 size_type max_size() const {
161 return static_cast<size_type>(-1) / sizeof(T);
162 }
163
164 pointer address(reference x) const { return &x; }
165 const_pointer address(const_reference x) const { return &x; }
166
167 pointer allocate(size_type n, SwapAllocator<void>::pointer hint ATTRIBUTE_UNUSED = nullptr) {
168 DCHECK_LE(n, max_size());
169 if (swap_space_ == nullptr) {
Vladimir Marko305ff2d2015-09-04 11:10:40 +0100170 T* result = reinterpret_cast<T*>(malloc(n * sizeof(T)));
171 CHECK(result != nullptr || n == 0u); // Abort if malloc() fails.
172 return result;
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800173 } else {
174 return reinterpret_cast<T*>(swap_space_->Alloc(n * sizeof(T)));
175 }
176 }
177 void deallocate(pointer p, size_type n) {
178 if (swap_space_ == nullptr) {
179 free(p);
180 } else {
181 swap_space_->Free(p, n * sizeof(T));
182 }
183 }
184
185 void construct(pointer p, const_reference val) {
186 new (static_cast<void*>(p)) value_type(val);
187 }
188 template <class U, class... Args>
189 void construct(U* p, Args&&... args) {
190 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
191 }
192 void destroy(pointer p) {
193 p->~value_type();
194 }
195
196 inline bool operator==(SwapAllocator const& other) {
197 return swap_space_ == other.swap_space_;
198 }
199 inline bool operator!=(SwapAllocator const& other) {
200 return !operator==(other);
201 }
202
203 private:
204 SwapSpace* swap_space_;
205
206 template <typename U>
207 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100208
209 template <typename U>
210 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800211};
212
213template <typename T>
Vladimir Markoef9230b2015-10-06 13:51:55 +0100214inline bool operator==(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
215 return lhs.swap_space_ == rhs.swap_space_;
216}
217
218template <typename T>
219inline bool operator!=(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
220 return !(lhs == rhs);
221}
222
223template <typename T>
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800224using SwapVector = std::vector<T, SwapAllocator<T>>;
225template <typename T, typename Comparator>
226using SwapSet = std::set<T, Comparator, SwapAllocator<T>>;
227
228} // namespace art
229
230#endif // ART_COMPILER_UTILS_SWAP_SPACE_H_