blob: b659f1d3c7516d835b1cac89db85220a5c2cccde [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_;
88 std::list<SpaceChunk> maps_;
89
90 // NOTE: Boost.Bimap would be useful for the two following members.
91
92 // Map start of a free chunk to its size.
93 FreeByStartSet free_by_start_ GUARDED_BY(lock_);
94 // Free chunks ordered by size.
Andreas Gampee21dc3d2014-12-08 16:59:43 -080095 FreeBySizeSet free_by_size_ GUARDED_BY(lock_);
96
97 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
98 DISALLOW_COPY_AND_ASSIGN(SwapSpace);
99};
100
101template <typename T> class SwapAllocator;
102
103template <>
104class SwapAllocator<void> {
105 public:
106 typedef void value_type;
107 typedef void* pointer;
108 typedef const void* const_pointer;
109
110 template <typename U>
111 struct rebind {
112 typedef SwapAllocator<U> other;
113 };
114
115 explicit SwapAllocator(SwapSpace* swap_space) : swap_space_(swap_space) {}
116
117 template <typename U>
118 SwapAllocator(const SwapAllocator<U>& other) : swap_space_(other.swap_space_) {}
119
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>
153 SwapAllocator(const SwapAllocator<U>& other) : swap_space_(other.swap_space_) {}
154
155 SwapAllocator(const SwapAllocator& other) = default;
156 SwapAllocator& operator=(const SwapAllocator& other) = default;
157 ~SwapAllocator() = default;
158
159 size_type max_size() const {
160 return static_cast<size_type>(-1) / sizeof(T);
161 }
162
163 pointer address(reference x) const { return &x; }
164 const_pointer address(const_reference x) const { return &x; }
165
166 pointer allocate(size_type n, SwapAllocator<void>::pointer hint ATTRIBUTE_UNUSED = nullptr) {
167 DCHECK_LE(n, max_size());
168 if (swap_space_ == nullptr) {
Vladimir Marko305ff2d2015-09-04 11:10:40 +0100169 T* result = reinterpret_cast<T*>(malloc(n * sizeof(T)));
170 CHECK(result != nullptr || n == 0u); // Abort if malloc() fails.
171 return result;
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800172 } else {
173 return reinterpret_cast<T*>(swap_space_->Alloc(n * sizeof(T)));
174 }
175 }
176 void deallocate(pointer p, size_type n) {
177 if (swap_space_ == nullptr) {
178 free(p);
179 } else {
180 swap_space_->Free(p, n * sizeof(T));
181 }
182 }
183
184 void construct(pointer p, const_reference val) {
185 new (static_cast<void*>(p)) value_type(val);
186 }
187 template <class U, class... Args>
188 void construct(U* p, Args&&... args) {
189 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
190 }
191 void destroy(pointer p) {
192 p->~value_type();
193 }
194
195 inline bool operator==(SwapAllocator const& other) {
196 return swap_space_ == other.swap_space_;
197 }
198 inline bool operator!=(SwapAllocator const& other) {
199 return !operator==(other);
200 }
201
202 private:
203 SwapSpace* swap_space_;
204
205 template <typename U>
206 friend class SwapAllocator;
Vladimir Markoef9230b2015-10-06 13:51:55 +0100207
208 template <typename U>
209 friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800210};
211
212template <typename T>
Vladimir Markoef9230b2015-10-06 13:51:55 +0100213inline bool operator==(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
214 return lhs.swap_space_ == rhs.swap_space_;
215}
216
217template <typename T>
218inline bool operator!=(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) {
219 return !(lhs == rhs);
220}
221
222template <typename T>
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800223using SwapVector = std::vector<T, SwapAllocator<T>>;
224template <typename T, typename Comparator>
225using SwapSet = std::set<T, Comparator, SwapAllocator<T>>;
226
227} // namespace art
228
229#endif // ART_COMPILER_UTILS_SWAP_SPACE_H_