Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 22 | #include <vector> |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 23 | #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 Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 33 | // An arena pool that creates arenas backed by an mmaped file. |
| 34 | class SwapSpace { |
| 35 | public: |
| 36 | SwapSpace(int fd, size_t initial_size); |
| 37 | ~SwapSpace(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 38 | void* Alloc(size_t size) REQUIRES(!lock_); |
| 39 | void Free(void* ptr, size_t size) REQUIRES(!lock_); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 40 | |
| 41 | size_t GetSize() { |
| 42 | return size_; |
| 43 | } |
| 44 | |
| 45 | private: |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 46 | // Chunk of space. |
| 47 | struct SpaceChunk { |
| 48 | uint8_t* ptr; |
| 49 | size_t size; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 50 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 51 | 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 Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 58 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 59 | 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 Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 65 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 66 | typedef std::set<SpaceChunk, SortChunkByPtr> FreeByStartSet; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 67 | |
| 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 Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 80 | |
| 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 Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 88 | |
| 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 Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 94 | FreeBySizeSet free_by_size_ GUARDED_BY(lock_); |
| 95 | |
| 96 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 97 | DISALLOW_COPY_AND_ASSIGN(SwapSpace); |
| 98 | }; |
| 99 | |
| 100 | template <typename T> class SwapAllocator; |
| 101 | |
| 102 | template <> |
| 103 | class 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 Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 117 | SwapAllocator(const SwapAllocator<U>& other) // NOLINT, implicit |
| 118 | : swap_space_(other.swap_space_) {} |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 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 Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 129 | |
| 130 | template <typename U> |
| 131 | friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | template <typename T> |
| 135 | class 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 Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 153 | SwapAllocator(const SwapAllocator<U>& other) // NOLINT, implicit |
| 154 | : swap_space_(other.swap_space_) {} |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 155 | |
| 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 Marko | 305ff2d | 2015-09-04 11:10:40 +0100 | [diff] [blame] | 170 | T* result = reinterpret_cast<T*>(malloc(n * sizeof(T))); |
| 171 | CHECK(result != nullptr || n == 0u); // Abort if malloc() fails. |
| 172 | return result; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 173 | } 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 Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 208 | |
| 209 | template <typename U> |
| 210 | friend bool operator==(const SwapAllocator<U>& lhs, const SwapAllocator<U>& rhs); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | template <typename T> |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 214 | inline bool operator==(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) { |
| 215 | return lhs.swap_space_ == rhs.swap_space_; |
| 216 | } |
| 217 | |
| 218 | template <typename T> |
| 219 | inline bool operator!=(const SwapAllocator<T>& lhs, const SwapAllocator<T>& rhs) { |
| 220 | return !(lhs == rhs); |
| 221 | } |
| 222 | |
| 223 | template <typename T> |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 224 | using SwapVector = std::vector<T, SwapAllocator<T>>; |
| 225 | template <typename T, typename Comparator> |
| 226 | using SwapSet = std::set<T, Comparator, SwapAllocator<T>>; |
| 227 | |
| 228 | } // namespace art |
| 229 | |
| 230 | #endif // ART_COMPILER_UTILS_SWAP_SPACE_H_ |