Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #include "base/mutex-inl.h" |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 18 | #include "mirror/class-inl.h" |
| 19 | #include "mirror/object.h" |
| 20 | #include "mirror/object-inl.h" |
Brian Carlstrom | 218daa2 | 2013-11-25 14:51:44 -0800 | [diff] [blame] | 21 | #include "thread-inl.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 22 | #include "thread_list.h" |
| 23 | #include "rosalloc.h" |
| 24 | |
| 25 | #include <map> |
| 26 | #include <list> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 27 | #include <sstream> |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | namespace art { |
| 31 | namespace gc { |
| 32 | namespace allocator { |
| 33 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 34 | extern "C" void* art_heap_rosalloc_morecore(RosAlloc* rosalloc, intptr_t increment); |
| 35 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 36 | static constexpr bool kUsePrefetchDuringAllocRun = true; |
| 37 | static constexpr bool kPrefetchNewRunDataByZeroing = false; |
| 38 | static constexpr size_t kPrefetchStride = 64; |
| 39 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 40 | size_t RosAlloc::bracketSizes[kNumOfSizeBrackets]; |
| 41 | size_t RosAlloc::numOfPages[kNumOfSizeBrackets]; |
| 42 | size_t RosAlloc::numOfSlots[kNumOfSizeBrackets]; |
| 43 | size_t RosAlloc::headerSizes[kNumOfSizeBrackets]; |
| 44 | size_t RosAlloc::bulkFreeBitMapOffsets[kNumOfSizeBrackets]; |
| 45 | size_t RosAlloc::threadLocalFreeBitMapOffsets[kNumOfSizeBrackets]; |
| 46 | bool RosAlloc::initialized_ = false; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 47 | size_t RosAlloc::dedicated_full_run_storage_[kPageSize / sizeof(size_t)] = { 0 }; |
| 48 | RosAlloc::Run* RosAlloc::dedicated_full_run_ = |
| 49 | reinterpret_cast<RosAlloc::Run*>(dedicated_full_run_storage_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 50 | |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 51 | RosAlloc::RosAlloc(void* base, size_t capacity, size_t max_capacity, |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 52 | PageReleaseMode page_release_mode, size_t page_release_size_threshold) |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 53 | : base_(reinterpret_cast<uint8_t*>(base)), footprint_(capacity), |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 54 | capacity_(capacity), max_capacity_(max_capacity), |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 55 | lock_("rosalloc global lock", kRosAllocGlobalLock), |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 56 | bulk_free_lock_("rosalloc bulk free lock", kRosAllocBulkFreeLock), |
| 57 | page_release_mode_(page_release_mode), |
| 58 | page_release_size_threshold_(page_release_size_threshold) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 59 | DCHECK_EQ(RoundUp(capacity, kPageSize), capacity); |
| 60 | DCHECK_EQ(RoundUp(max_capacity, kPageSize), max_capacity); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 61 | CHECK_LE(capacity, max_capacity); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 62 | CHECK(IsAligned<kPageSize>(page_release_size_threshold_)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 63 | if (!initialized_) { |
| 64 | Initialize(); |
| 65 | } |
| 66 | VLOG(heap) << "RosAlloc base=" |
| 67 | << std::hex << (intptr_t)base_ << ", end=" |
| 68 | << std::hex << (intptr_t)(base_ + capacity_) |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 69 | << ", capacity=" << std::dec << capacity_ |
| 70 | << ", max_capacity=" << std::dec << max_capacity_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 71 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 72 | size_bracket_lock_names_[i] = |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 73 | StringPrintf("an rosalloc size bracket %d lock", static_cast<int>(i)); |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 74 | size_bracket_locks_[i] = new Mutex(size_bracket_lock_names_[i].c_str(), kRosAllocBracketLock); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 75 | current_runs_[i] = dedicated_full_run_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 76 | } |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 77 | DCHECK_EQ(footprint_, capacity_); |
| 78 | size_t num_of_pages = footprint_ / kPageSize; |
| 79 | size_t max_num_of_pages = max_capacity_ / kPageSize; |
| 80 | std::string error_msg; |
| 81 | page_map_mem_map_.reset(MemMap::MapAnonymous("rosalloc page map", NULL, RoundUp(max_num_of_pages, kPageSize), |
| 82 | PROT_READ | PROT_WRITE, false, &error_msg)); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 83 | CHECK(page_map_mem_map_.get() != nullptr) << "Couldn't allocate the page map : " << error_msg; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 84 | page_map_ = page_map_mem_map_->Begin(); |
| 85 | page_map_size_ = num_of_pages; |
| 86 | max_page_map_size_ = max_num_of_pages; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 87 | free_page_run_size_map_.resize(num_of_pages); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 88 | FreePageRun* free_pages = reinterpret_cast<FreePageRun*>(base_); |
| 89 | if (kIsDebugBuild) { |
| 90 | free_pages->magic_num_ = kMagicNumFree; |
| 91 | } |
| 92 | free_pages->SetByteSize(this, capacity_); |
| 93 | DCHECK_EQ(capacity_ % kPageSize, static_cast<size_t>(0)); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 94 | DCHECK(free_pages->IsFree()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 95 | free_pages->ReleasePages(this); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 96 | DCHECK(free_pages->IsFree()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 97 | free_page_runs_.insert(free_pages); |
| 98 | if (kTraceRosAlloc) { |
| 99 | LOG(INFO) << "RosAlloc::RosAlloc() : Inserted run 0x" << std::hex |
| 100 | << reinterpret_cast<intptr_t>(free_pages) |
| 101 | << " into free_page_runs_"; |
| 102 | } |
| 103 | } |
| 104 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 105 | RosAlloc::~RosAlloc() { |
| 106 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 107 | delete size_bracket_locks_[i]; |
| 108 | } |
| 109 | } |
| 110 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 111 | void* RosAlloc::AllocPages(Thread* self, size_t num_pages, uint8_t page_map_type) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 112 | lock_.AssertHeld(self); |
| 113 | DCHECK(page_map_type == kPageMapRun || page_map_type == kPageMapLargeObject); |
| 114 | FreePageRun* res = NULL; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 115 | const size_t req_byte_size = num_pages * kPageSize; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 116 | // Find the lowest address free page run that's large enough. |
| 117 | for (auto it = free_page_runs_.begin(); it != free_page_runs_.end(); ) { |
| 118 | FreePageRun* fpr = *it; |
| 119 | DCHECK(fpr->IsFree()); |
| 120 | size_t fpr_byte_size = fpr->ByteSize(this); |
| 121 | DCHECK_EQ(fpr_byte_size % kPageSize, static_cast<size_t>(0)); |
| 122 | if (req_byte_size <= fpr_byte_size) { |
| 123 | // Found one. |
| 124 | free_page_runs_.erase(it++); |
| 125 | if (kTraceRosAlloc) { |
| 126 | LOG(INFO) << "RosAlloc::AllocPages() : Erased run 0x" |
| 127 | << std::hex << reinterpret_cast<intptr_t>(fpr) |
| 128 | << " from free_page_runs_"; |
| 129 | } |
| 130 | if (req_byte_size < fpr_byte_size) { |
| 131 | // Split. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 132 | FreePageRun* remainder = reinterpret_cast<FreePageRun*>(reinterpret_cast<uint8_t*>(fpr) + req_byte_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 133 | if (kIsDebugBuild) { |
| 134 | remainder->magic_num_ = kMagicNumFree; |
| 135 | } |
| 136 | remainder->SetByteSize(this, fpr_byte_size - req_byte_size); |
| 137 | DCHECK_EQ(remainder->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 138 | // Don't need to call madvise on remainder here. |
| 139 | free_page_runs_.insert(remainder); |
| 140 | if (kTraceRosAlloc) { |
| 141 | LOG(INFO) << "RosAlloc::AllocPages() : Inserted run 0x" << std::hex |
| 142 | << reinterpret_cast<intptr_t>(remainder) |
| 143 | << " into free_page_runs_"; |
| 144 | } |
| 145 | fpr->SetByteSize(this, req_byte_size); |
| 146 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 147 | } |
| 148 | res = fpr; |
| 149 | break; |
| 150 | } else { |
| 151 | ++it; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Failed to allocate pages. Grow the footprint, if possible. |
| 156 | if (UNLIKELY(res == NULL && capacity_ > footprint_)) { |
| 157 | FreePageRun* last_free_page_run = NULL; |
| 158 | size_t last_free_page_run_size; |
| 159 | auto it = free_page_runs_.rbegin(); |
| 160 | if (it != free_page_runs_.rend() && (last_free_page_run = *it)->End(this) == base_ + footprint_) { |
| 161 | // There is a free page run at the end. |
| 162 | DCHECK(last_free_page_run->IsFree()); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 163 | DCHECK(IsFreePage(ToPageMapIndex(last_free_page_run))); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 164 | last_free_page_run_size = last_free_page_run->ByteSize(this); |
| 165 | } else { |
| 166 | // There is no free page run at the end. |
| 167 | last_free_page_run_size = 0; |
| 168 | } |
| 169 | DCHECK_LT(last_free_page_run_size, req_byte_size); |
| 170 | if (capacity_ - footprint_ + last_free_page_run_size >= req_byte_size) { |
| 171 | // If we grow the heap, we can allocate it. |
| 172 | size_t increment = std::min(std::max(2 * MB, req_byte_size - last_free_page_run_size), |
| 173 | capacity_ - footprint_); |
| 174 | DCHECK_EQ(increment % kPageSize, static_cast<size_t>(0)); |
| 175 | size_t new_footprint = footprint_ + increment; |
| 176 | size_t new_num_of_pages = new_footprint / kPageSize; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 177 | DCHECK_LT(page_map_size_, new_num_of_pages); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 178 | DCHECK_LT(free_page_run_size_map_.size(), new_num_of_pages); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 179 | page_map_size_ = new_num_of_pages; |
| 180 | DCHECK_LE(page_map_size_, max_page_map_size_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 181 | free_page_run_size_map_.resize(new_num_of_pages); |
| 182 | art_heap_rosalloc_morecore(this, increment); |
| 183 | if (last_free_page_run_size > 0) { |
| 184 | // There was a free page run at the end. Expand its size. |
| 185 | DCHECK_EQ(last_free_page_run_size, last_free_page_run->ByteSize(this)); |
| 186 | last_free_page_run->SetByteSize(this, last_free_page_run_size + increment); |
| 187 | DCHECK_EQ(last_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 188 | DCHECK_EQ(last_free_page_run->End(this), base_ + new_footprint); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 189 | } else { |
| 190 | // Otherwise, insert a new free page run at the end. |
| 191 | FreePageRun* new_free_page_run = reinterpret_cast<FreePageRun*>(base_ + footprint_); |
| 192 | if (kIsDebugBuild) { |
| 193 | new_free_page_run->magic_num_ = kMagicNumFree; |
| 194 | } |
| 195 | new_free_page_run->SetByteSize(this, increment); |
| 196 | DCHECK_EQ(new_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 197 | free_page_runs_.insert(new_free_page_run); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 198 | DCHECK_EQ(*free_page_runs_.rbegin(), new_free_page_run); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 199 | if (kTraceRosAlloc) { |
| 200 | LOG(INFO) << "RosAlloc::AlloPages() : Grew the heap by inserting run 0x" |
| 201 | << std::hex << reinterpret_cast<intptr_t>(new_free_page_run) |
| 202 | << " into free_page_runs_"; |
| 203 | } |
| 204 | } |
| 205 | DCHECK_LE(footprint_ + increment, capacity_); |
| 206 | if (kTraceRosAlloc) { |
| 207 | LOG(INFO) << "RosAlloc::AllocPages() : increased the footprint from " |
| 208 | << footprint_ << " to " << new_footprint; |
| 209 | } |
| 210 | footprint_ = new_footprint; |
| 211 | |
| 212 | // And retry the last free page run. |
| 213 | it = free_page_runs_.rbegin(); |
| 214 | DCHECK(it != free_page_runs_.rend()); |
| 215 | FreePageRun* fpr = *it; |
| 216 | if (kIsDebugBuild && last_free_page_run_size > 0) { |
| 217 | DCHECK(last_free_page_run != NULL); |
| 218 | DCHECK_EQ(last_free_page_run, fpr); |
| 219 | } |
| 220 | size_t fpr_byte_size = fpr->ByteSize(this); |
| 221 | DCHECK_EQ(fpr_byte_size % kPageSize, static_cast<size_t>(0)); |
| 222 | DCHECK_LE(req_byte_size, fpr_byte_size); |
| 223 | free_page_runs_.erase(fpr); |
| 224 | if (kTraceRosAlloc) { |
| 225 | LOG(INFO) << "RosAlloc::AllocPages() : Erased run 0x" << std::hex << reinterpret_cast<intptr_t>(fpr) |
| 226 | << " from free_page_runs_"; |
| 227 | } |
| 228 | if (req_byte_size < fpr_byte_size) { |
| 229 | // Split if there's a remainder. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 230 | FreePageRun* remainder = reinterpret_cast<FreePageRun*>(reinterpret_cast<uint8_t*>(fpr) + req_byte_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 231 | if (kIsDebugBuild) { |
| 232 | remainder->magic_num_ = kMagicNumFree; |
| 233 | } |
| 234 | remainder->SetByteSize(this, fpr_byte_size - req_byte_size); |
| 235 | DCHECK_EQ(remainder->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 236 | free_page_runs_.insert(remainder); |
| 237 | if (kTraceRosAlloc) { |
| 238 | LOG(INFO) << "RosAlloc::AllocPages() : Inserted run 0x" << std::hex |
| 239 | << reinterpret_cast<intptr_t>(remainder) |
| 240 | << " into free_page_runs_"; |
| 241 | } |
| 242 | fpr->SetByteSize(this, req_byte_size); |
| 243 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 244 | } |
| 245 | res = fpr; |
| 246 | } |
| 247 | } |
| 248 | if (LIKELY(res != NULL)) { |
| 249 | // Update the page map. |
| 250 | size_t page_map_idx = ToPageMapIndex(res); |
| 251 | for (size_t i = 0; i < num_pages; i++) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 252 | DCHECK(IsFreePage(page_map_idx + i)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 253 | } |
| 254 | switch (page_map_type) { |
| 255 | case kPageMapRun: |
| 256 | page_map_[page_map_idx] = kPageMapRun; |
| 257 | for (size_t i = 1; i < num_pages; i++) { |
| 258 | page_map_[page_map_idx + i] = kPageMapRunPart; |
| 259 | } |
| 260 | break; |
| 261 | case kPageMapLargeObject: |
| 262 | page_map_[page_map_idx] = kPageMapLargeObject; |
| 263 | for (size_t i = 1; i < num_pages; i++) { |
| 264 | page_map_[page_map_idx + i] = kPageMapLargeObjectPart; |
| 265 | } |
| 266 | break; |
| 267 | default: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 268 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_type); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | if (kIsDebugBuild) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 272 | // Clear the first page since it is not madvised due to the magic number. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 273 | memset(res, 0, kPageSize); |
| 274 | } |
| 275 | if (kTraceRosAlloc) { |
| 276 | LOG(INFO) << "RosAlloc::AllocPages() : 0x" << std::hex << reinterpret_cast<intptr_t>(res) |
| 277 | << "-0x" << (reinterpret_cast<intptr_t>(res) + num_pages * kPageSize) |
| 278 | << "(" << std::dec << (num_pages * kPageSize) << ")"; |
| 279 | } |
| 280 | return res; |
| 281 | } |
| 282 | |
| 283 | // Fail. |
| 284 | if (kTraceRosAlloc) { |
| 285 | LOG(INFO) << "RosAlloc::AllocPages() : NULL"; |
| 286 | } |
| 287 | return nullptr; |
| 288 | } |
| 289 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 290 | size_t RosAlloc::FreePages(Thread* self, void* ptr, bool already_zero) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 291 | lock_.AssertHeld(self); |
| 292 | size_t pm_idx = ToPageMapIndex(ptr); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 293 | DCHECK_LT(pm_idx, page_map_size_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 294 | uint8_t pm_type = page_map_[pm_idx]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 295 | DCHECK(pm_type == kPageMapRun || pm_type == kPageMapLargeObject); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 296 | uint8_t pm_part_type; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 297 | switch (pm_type) { |
| 298 | case kPageMapRun: |
| 299 | pm_part_type = kPageMapRunPart; |
| 300 | break; |
| 301 | case kPageMapLargeObject: |
| 302 | pm_part_type = kPageMapLargeObjectPart; |
| 303 | break; |
| 304 | default: |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 305 | LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << " : " << "pm_idx=" << pm_idx << ", pm_type=" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 306 | << static_cast<int>(pm_type) << ", ptr=" << std::hex |
| 307 | << reinterpret_cast<intptr_t>(ptr); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 308 | return 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 309 | } |
| 310 | // Update the page map and count the number of pages. |
| 311 | size_t num_pages = 1; |
| 312 | page_map_[pm_idx] = kPageMapEmpty; |
| 313 | size_t idx = pm_idx + 1; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 314 | size_t end = page_map_size_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 315 | while (idx < end && page_map_[idx] == pm_part_type) { |
| 316 | page_map_[idx] = kPageMapEmpty; |
| 317 | num_pages++; |
| 318 | idx++; |
| 319 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 320 | const size_t byte_size = num_pages * kPageSize; |
| 321 | if (already_zero) { |
| 322 | if (kCheckZeroMemory) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 323 | const uintptr_t* word_ptr = reinterpret_cast<uintptr_t*>(ptr); |
| 324 | for (size_t i = 0; i < byte_size / sizeof(uintptr_t); ++i) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 325 | CHECK_EQ(word_ptr[i], 0U) << "words don't match at index " << i; |
| 326 | } |
| 327 | } |
| 328 | } else if (!DoesReleaseAllPages()) { |
| 329 | memset(ptr, 0, byte_size); |
| 330 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 331 | |
| 332 | if (kTraceRosAlloc) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 333 | LOG(INFO) << __PRETTY_FUNCTION__ << " : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr) |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 334 | << "-0x" << (reinterpret_cast<intptr_t>(ptr) + byte_size) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 335 | << "(" << std::dec << (num_pages * kPageSize) << ")"; |
| 336 | } |
| 337 | |
| 338 | // Turn it into a free run. |
| 339 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(ptr); |
| 340 | if (kIsDebugBuild) { |
| 341 | fpr->magic_num_ = kMagicNumFree; |
| 342 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 343 | fpr->SetByteSize(this, byte_size); |
| 344 | DCHECK(IsAligned<kPageSize>(fpr->ByteSize(this))); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 345 | |
| 346 | DCHECK(free_page_runs_.find(fpr) == free_page_runs_.end()); |
| 347 | if (!free_page_runs_.empty()) { |
| 348 | // Try to coalesce in the higher address direction. |
| 349 | if (kTraceRosAlloc) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 350 | LOG(INFO) << __PRETTY_FUNCTION__ << "RosAlloc::FreePages() : trying to coalesce a free page run 0x" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 351 | << std::hex << reinterpret_cast<uintptr_t>(fpr) << " [" << std::dec << pm_idx << "] -0x" |
| 352 | << std::hex << reinterpret_cast<uintptr_t>(fpr->End(this)) << " [" << std::dec |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 353 | << (fpr->End(this) == End() ? page_map_size_ : ToPageMapIndex(fpr->End(this))) << "]"; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 354 | } |
| 355 | auto higher_it = free_page_runs_.upper_bound(fpr); |
| 356 | if (higher_it != free_page_runs_.end()) { |
| 357 | for (auto it = higher_it; it != free_page_runs_.end(); ) { |
| 358 | FreePageRun* h = *it; |
| 359 | DCHECK_EQ(h->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 360 | if (kTraceRosAlloc) { |
| 361 | LOG(INFO) << "RosAlloc::FreePages() : trying to coalesce with a higher free page run 0x" |
| 362 | << std::hex << reinterpret_cast<uintptr_t>(h) << " [" << std::dec << ToPageMapIndex(h) << "] -0x" |
| 363 | << std::hex << reinterpret_cast<uintptr_t>(h->End(this)) << " [" << std::dec |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 364 | << (h->End(this) == End() ? page_map_size_ : ToPageMapIndex(h->End(this))) << "]"; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 365 | } |
| 366 | if (fpr->End(this) == h->Begin()) { |
| 367 | if (kTraceRosAlloc) { |
| 368 | LOG(INFO) << "Success"; |
| 369 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 370 | // Clear magic num since this is no longer the start of a free page run. |
| 371 | if (kIsDebugBuild) { |
| 372 | h->magic_num_ = 0; |
| 373 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 374 | free_page_runs_.erase(it++); |
| 375 | if (kTraceRosAlloc) { |
| 376 | LOG(INFO) << "RosAlloc::FreePages() : (coalesce) Erased run 0x" << std::hex |
| 377 | << reinterpret_cast<intptr_t>(h) |
| 378 | << " from free_page_runs_"; |
| 379 | } |
| 380 | fpr->SetByteSize(this, fpr->ByteSize(this) + h->ByteSize(this)); |
| 381 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 382 | } else { |
| 383 | // Not adjacent. Stop. |
| 384 | if (kTraceRosAlloc) { |
| 385 | LOG(INFO) << "Fail"; |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | // Try to coalesce in the lower address direction. |
| 392 | auto lower_it = free_page_runs_.upper_bound(fpr); |
| 393 | if (lower_it != free_page_runs_.begin()) { |
| 394 | --lower_it; |
| 395 | for (auto it = lower_it; ; ) { |
| 396 | // We want to try to coalesce with the first element but |
| 397 | // there's no "<=" operator for the iterator. |
| 398 | bool to_exit_loop = it == free_page_runs_.begin(); |
| 399 | |
| 400 | FreePageRun* l = *it; |
| 401 | DCHECK_EQ(l->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 402 | if (kTraceRosAlloc) { |
| 403 | LOG(INFO) << "RosAlloc::FreePages() : trying to coalesce with a lower free page run 0x" |
| 404 | << std::hex << reinterpret_cast<uintptr_t>(l) << " [" << std::dec << ToPageMapIndex(l) << "] -0x" |
| 405 | << std::hex << reinterpret_cast<uintptr_t>(l->End(this)) << " [" << std::dec |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 406 | << (l->End(this) == End() ? page_map_size_ : ToPageMapIndex(l->End(this))) << "]"; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 407 | } |
| 408 | if (l->End(this) == fpr->Begin()) { |
| 409 | if (kTraceRosAlloc) { |
| 410 | LOG(INFO) << "Success"; |
| 411 | } |
| 412 | free_page_runs_.erase(it--); |
| 413 | if (kTraceRosAlloc) { |
| 414 | LOG(INFO) << "RosAlloc::FreePages() : (coalesce) Erased run 0x" << std::hex |
| 415 | << reinterpret_cast<intptr_t>(l) |
| 416 | << " from free_page_runs_"; |
| 417 | } |
| 418 | l->SetByteSize(this, l->ByteSize(this) + fpr->ByteSize(this)); |
| 419 | DCHECK_EQ(l->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 420 | // Clear magic num since this is no longer the start of a free page run. |
| 421 | if (kIsDebugBuild) { |
| 422 | fpr->magic_num_ = 0; |
| 423 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 424 | fpr = l; |
| 425 | } else { |
| 426 | // Not adjacent. Stop. |
| 427 | if (kTraceRosAlloc) { |
| 428 | LOG(INFO) << "Fail"; |
| 429 | } |
| 430 | break; |
| 431 | } |
| 432 | if (to_exit_loop) { |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Insert it. |
| 440 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 441 | DCHECK(free_page_runs_.find(fpr) == free_page_runs_.end()); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 442 | DCHECK(fpr->IsFree()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 443 | fpr->ReleasePages(this); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 444 | DCHECK(fpr->IsFree()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 445 | free_page_runs_.insert(fpr); |
| 446 | DCHECK(free_page_runs_.find(fpr) != free_page_runs_.end()); |
| 447 | if (kTraceRosAlloc) { |
| 448 | LOG(INFO) << "RosAlloc::FreePages() : Inserted run 0x" << std::hex << reinterpret_cast<intptr_t>(fpr) |
| 449 | << " into free_page_runs_"; |
| 450 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 451 | return byte_size; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 454 | void* RosAlloc::AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 455 | DCHECK_GT(size, kLargeSizeThreshold); |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 456 | size_t num_pages = RoundUp(size, kPageSize) / kPageSize; |
| 457 | void* r; |
| 458 | { |
| 459 | MutexLock mu(self, lock_); |
| 460 | r = AllocPages(self, num_pages, kPageMapLargeObject); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 461 | } |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 462 | if (UNLIKELY(r == nullptr)) { |
| 463 | if (kTraceRosAlloc) { |
| 464 | LOG(INFO) << "RosAlloc::AllocLargeObject() : NULL"; |
| 465 | } |
| 466 | return nullptr; |
| 467 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 468 | const size_t total_bytes = num_pages * kPageSize; |
| 469 | *bytes_allocated = total_bytes; |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 470 | if (kTraceRosAlloc) { |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 471 | LOG(INFO) << "RosAlloc::AllocLargeObject() : 0x" << std::hex << reinterpret_cast<intptr_t>(r) |
| 472 | << "-0x" << (reinterpret_cast<intptr_t>(r) + num_pages * kPageSize) |
| 473 | << "(" << std::dec << (num_pages * kPageSize) << ")"; |
| 474 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 475 | // Check if the returned memory is really all zero. |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 476 | if (kCheckZeroMemory) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 477 | CHECK_EQ(total_bytes % sizeof(uintptr_t), 0U); |
| 478 | const uintptr_t* words = reinterpret_cast<uintptr_t*>(r); |
| 479 | for (size_t i = 0; i < total_bytes / sizeof(uintptr_t); ++i) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 480 | CHECK_EQ(words[i], 0U); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 483 | return r; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 486 | size_t RosAlloc::FreeInternal(Thread* self, void* ptr) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 487 | DCHECK_LE(base_, ptr); |
| 488 | DCHECK_LT(ptr, base_ + footprint_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 489 | size_t pm_idx = RoundDownToPageMapIndex(ptr); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 490 | Run* run = nullptr; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 491 | { |
| 492 | MutexLock mu(self, lock_); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 493 | DCHECK_LT(pm_idx, page_map_size_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 494 | uint8_t page_map_entry = page_map_[pm_idx]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 495 | if (kTraceRosAlloc) { |
| 496 | LOG(INFO) << "RosAlloc::FreeInternal() : " << std::hex << ptr << ", pm_idx=" << std::dec << pm_idx |
| 497 | << ", page_map_entry=" << static_cast<int>(page_map_entry); |
| 498 | } |
| 499 | switch (page_map_[pm_idx]) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 500 | case kPageMapLargeObject: |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 501 | return FreePages(self, ptr, false); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 502 | case kPageMapLargeObjectPart: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 503 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 504 | return 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 505 | case kPageMapRunPart: { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 506 | // Find the beginning of the run. |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 507 | do { |
| 508 | --pm_idx; |
| 509 | DCHECK_LT(pm_idx, capacity_ / kPageSize); |
| 510 | } while (page_map_[pm_idx] != kPageMapRun); |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 511 | FALLTHROUGH_INTENDED; |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 512 | case kPageMapRun: |
| 513 | run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 514 | DCHECK_EQ(run->magic_num_, kMagicNum); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 515 | break; |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 516 | case kPageMapReleased: |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 517 | case kPageMapEmpty: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 518 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 519 | return 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 520 | } |
| 521 | default: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 522 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 523 | return 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 526 | DCHECK(run != nullptr); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 527 | return FreeFromRun(self, ptr, run); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 530 | size_t RosAlloc::Free(Thread* self, void* ptr) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 531 | ReaderMutexLock rmu(self, bulk_free_lock_); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 532 | return FreeInternal(self, ptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 535 | RosAlloc::Run* RosAlloc::AllocRun(Thread* self, size_t idx) { |
| 536 | RosAlloc::Run* new_run = nullptr; |
| 537 | { |
| 538 | MutexLock mu(self, lock_); |
| 539 | new_run = reinterpret_cast<Run*>(AllocPages(self, numOfPages[idx], kPageMapRun)); |
| 540 | } |
| 541 | if (LIKELY(new_run != nullptr)) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 542 | if (kIsDebugBuild) { |
| 543 | new_run->magic_num_ = kMagicNum; |
| 544 | } |
| 545 | new_run->size_bracket_idx_ = idx; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 546 | new_run->SetAllocBitMapBitsForInvalidSlots(); |
| 547 | DCHECK(!new_run->IsThreadLocal()); |
| 548 | DCHECK_EQ(new_run->first_search_vec_idx_, 0U); |
| 549 | DCHECK(!new_run->to_be_bulk_freed_); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 550 | if (kUsePrefetchDuringAllocRun && idx < kNumThreadLocalSizeBrackets) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 551 | // Take ownership of the cache lines if we are likely to be thread local run. |
| 552 | if (kPrefetchNewRunDataByZeroing) { |
| 553 | // Zeroing the data is sometimes faster than prefetching but it increases memory usage |
| 554 | // since we end up dirtying zero pages which may have been madvised. |
| 555 | new_run->ZeroData(); |
| 556 | } else { |
| 557 | const size_t num_of_slots = numOfSlots[idx]; |
| 558 | const size_t bracket_size = bracketSizes[idx]; |
| 559 | const size_t num_of_bytes = num_of_slots * bracket_size; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 560 | uint8_t* begin = reinterpret_cast<uint8_t*>(new_run) + headerSizes[idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 561 | for (size_t i = 0; i < num_of_bytes; i += kPrefetchStride) { |
| 562 | __builtin_prefetch(begin + i); |
| 563 | } |
| 564 | } |
| 565 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 566 | } |
| 567 | return new_run; |
| 568 | } |
| 569 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 570 | RosAlloc::Run* RosAlloc::RefillRun(Thread* self, size_t idx) { |
| 571 | // Get the lowest address non-full run from the binary tree. |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 572 | auto* const bt = &non_full_runs_[idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 573 | if (!bt->empty()) { |
| 574 | // If there's one, use it as the current run. |
| 575 | auto it = bt->begin(); |
| 576 | Run* non_full_run = *it; |
| 577 | DCHECK(non_full_run != nullptr); |
| 578 | DCHECK(!non_full_run->IsThreadLocal()); |
| 579 | bt->erase(it); |
| 580 | return non_full_run; |
| 581 | } |
| 582 | // If there's none, allocate a new run and use it as the current run. |
| 583 | return AllocRun(self, idx); |
| 584 | } |
| 585 | |
Hiroshi Yamauchi | 52cf5c0 | 2014-05-02 12:20:36 -0700 | [diff] [blame] | 586 | inline void* RosAlloc::AllocFromCurrentRunUnlocked(Thread* self, size_t idx) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 587 | Run* current_run = current_runs_[idx]; |
| 588 | DCHECK(current_run != nullptr); |
| 589 | void* slot_addr = current_run->AllocSlot(); |
| 590 | if (UNLIKELY(slot_addr == nullptr)) { |
| 591 | // The current run got full. Try to refill it. |
| 592 | DCHECK(current_run->IsFull()); |
| 593 | if (kIsDebugBuild && current_run != dedicated_full_run_) { |
| 594 | full_runs_[idx].insert(current_run); |
| 595 | if (kTraceRosAlloc) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 596 | LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex |
| 597 | << reinterpret_cast<intptr_t>(current_run) |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 598 | << " into full_runs_[" << std::dec << idx << "]"; |
| 599 | } |
| 600 | DCHECK(non_full_runs_[idx].find(current_run) == non_full_runs_[idx].end()); |
| 601 | DCHECK(full_runs_[idx].find(current_run) != full_runs_[idx].end()); |
| 602 | } |
| 603 | current_run = RefillRun(self, idx); |
| 604 | if (UNLIKELY(current_run == nullptr)) { |
| 605 | // Failed to allocate a new run, make sure that it is the dedicated full run. |
| 606 | current_runs_[idx] = dedicated_full_run_; |
| 607 | return nullptr; |
| 608 | } |
| 609 | DCHECK(current_run != nullptr); |
| 610 | DCHECK(non_full_runs_[idx].find(current_run) == non_full_runs_[idx].end()); |
| 611 | DCHECK(full_runs_[idx].find(current_run) == full_runs_[idx].end()); |
| 612 | current_run->SetIsThreadLocal(false); |
| 613 | current_runs_[idx] = current_run; |
| 614 | DCHECK(!current_run->IsFull()); |
| 615 | slot_addr = current_run->AllocSlot(); |
| 616 | // Must succeed now with a new run. |
| 617 | DCHECK(slot_addr != nullptr); |
| 618 | } |
| 619 | return slot_addr; |
| 620 | } |
| 621 | |
| 622 | void* RosAlloc::AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated) { |
| 623 | DCHECK_LE(size, kLargeSizeThreshold); |
| 624 | size_t bracket_size; |
| 625 | size_t idx = SizeToIndexAndBracketSize(size, &bracket_size); |
| 626 | DCHECK_EQ(idx, SizeToIndex(size)); |
| 627 | DCHECK_EQ(bracket_size, IndexToBracketSize(idx)); |
| 628 | DCHECK_EQ(bracket_size, bracketSizes[idx]); |
| 629 | DCHECK_LE(size, bracket_size); |
| 630 | DCHECK(size > 512 || bracket_size - size < 16); |
| 631 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 632 | void* slot_addr = AllocFromCurrentRunUnlocked(self, idx); |
| 633 | if (LIKELY(slot_addr != nullptr)) { |
| 634 | DCHECK(bytes_allocated != nullptr); |
| 635 | *bytes_allocated = bracket_size; |
| 636 | // Caller verifies that it is all 0. |
| 637 | } |
| 638 | return slot_addr; |
| 639 | } |
| 640 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 641 | void* RosAlloc::AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 642 | DCHECK_LE(size, kLargeSizeThreshold); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 643 | size_t bracket_size; |
| 644 | size_t idx = SizeToIndexAndBracketSize(size, &bracket_size); |
| 645 | DCHECK_EQ(idx, SizeToIndex(size)); |
| 646 | DCHECK_EQ(bracket_size, IndexToBracketSize(idx)); |
| 647 | DCHECK_EQ(bracket_size, bracketSizes[idx]); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 648 | DCHECK_LE(size, bracket_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 649 | DCHECK(size > 512 || bracket_size - size < 16); |
| 650 | |
| 651 | void* slot_addr; |
| 652 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 653 | if (LIKELY(idx < kNumThreadLocalSizeBrackets)) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 654 | // Use a thread-local run. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 655 | Run* thread_local_run = reinterpret_cast<Run*>(self->GetRosAllocRun(idx)); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 656 | // Allow invalid since this will always fail the allocation. |
Mathieu Chartier | 4fd2050 | 2014-04-28 09:35:55 -0700 | [diff] [blame] | 657 | if (kIsDebugBuild) { |
| 658 | // Need the lock to prevent race conditions. |
| 659 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 660 | CHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 661 | CHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end()); |
| 662 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 663 | DCHECK(thread_local_run != nullptr); |
| 664 | DCHECK(thread_local_run->IsThreadLocal() || thread_local_run == dedicated_full_run_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 665 | slot_addr = thread_local_run->AllocSlot(); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 666 | // The allocation must fail if the run is invalid. |
| 667 | DCHECK(thread_local_run != dedicated_full_run_ || slot_addr == nullptr) |
| 668 | << "allocated from an invalid run"; |
| 669 | if (UNLIKELY(slot_addr == nullptr)) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 670 | // The run got full. Try to free slots. |
| 671 | DCHECK(thread_local_run->IsFull()); |
| 672 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 673 | bool is_all_free_after_merge; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 674 | // This is safe to do for the dedicated_full_run_ since the bitmaps are empty. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 675 | if (thread_local_run->MergeThreadLocalFreeBitMapToAllocBitMap(&is_all_free_after_merge)) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 676 | DCHECK_NE(thread_local_run, dedicated_full_run_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 677 | // Some slot got freed. Keep it. |
| 678 | DCHECK(!thread_local_run->IsFull()); |
| 679 | DCHECK_EQ(is_all_free_after_merge, thread_local_run->IsAllFree()); |
| 680 | if (is_all_free_after_merge) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 681 | // Check that the bitmap idx is back at 0 if it's all free. |
| 682 | DCHECK_EQ(thread_local_run->first_search_vec_idx_, 0U); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 683 | } |
| 684 | } else { |
| 685 | // No slots got freed. Try to refill the thread-local run. |
| 686 | DCHECK(thread_local_run->IsFull()); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 687 | if (thread_local_run != dedicated_full_run_) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 688 | thread_local_run->SetIsThreadLocal(false); |
| 689 | if (kIsDebugBuild) { |
| 690 | full_runs_[idx].insert(thread_local_run); |
| 691 | if (kTraceRosAlloc) { |
| 692 | LOG(INFO) << "RosAlloc::AllocFromRun() : Inserted run 0x" << std::hex |
| 693 | << reinterpret_cast<intptr_t>(thread_local_run) |
| 694 | << " into full_runs_[" << std::dec << idx << "]"; |
| 695 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 696 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 697 | DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 698 | DCHECK(full_runs_[idx].find(thread_local_run) != full_runs_[idx].end()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 699 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 700 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 701 | thread_local_run = RefillRun(self, idx); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 702 | if (UNLIKELY(thread_local_run == nullptr)) { |
| 703 | self->SetRosAllocRun(idx, dedicated_full_run_); |
| 704 | return nullptr; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 705 | } |
| 706 | DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 707 | DCHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end()); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 708 | thread_local_run->SetIsThreadLocal(true); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 709 | self->SetRosAllocRun(idx, thread_local_run); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 710 | DCHECK(!thread_local_run->IsFull()); |
| 711 | } |
| 712 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 713 | DCHECK(thread_local_run != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 714 | DCHECK(!thread_local_run->IsFull()); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 715 | DCHECK(thread_local_run->IsThreadLocal()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 716 | slot_addr = thread_local_run->AllocSlot(); |
| 717 | // Must succeed now with a new run. |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 718 | DCHECK(slot_addr != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 719 | } |
| 720 | if (kTraceRosAlloc) { |
| 721 | LOG(INFO) << "RosAlloc::AllocFromRun() thread-local : 0x" << std::hex << reinterpret_cast<intptr_t>(slot_addr) |
| 722 | << "-0x" << (reinterpret_cast<intptr_t>(slot_addr) + bracket_size) |
| 723 | << "(" << std::dec << (bracket_size) << ")"; |
| 724 | } |
| 725 | } else { |
| 726 | // Use the (shared) current run. |
| 727 | MutexLock mu(self, *size_bracket_locks_[idx]); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 728 | slot_addr = AllocFromCurrentRunUnlocked(self, idx); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 729 | if (kTraceRosAlloc) { |
| 730 | LOG(INFO) << "RosAlloc::AllocFromRun() : 0x" << std::hex << reinterpret_cast<intptr_t>(slot_addr) |
| 731 | << "-0x" << (reinterpret_cast<intptr_t>(slot_addr) + bracket_size) |
| 732 | << "(" << std::dec << (bracket_size) << ")"; |
| 733 | } |
| 734 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 735 | DCHECK(bytes_allocated != nullptr); |
| 736 | *bytes_allocated = bracket_size; |
| 737 | // Caller verifies that it is all 0. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 738 | return slot_addr; |
| 739 | } |
| 740 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 741 | size_t RosAlloc::FreeFromRun(Thread* self, void* ptr, Run* run) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 742 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| 743 | DCHECK_LT(run, ptr); |
| 744 | DCHECK_LT(ptr, run->End()); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 745 | const size_t idx = run->size_bracket_idx_; |
| 746 | const size_t bracket_size = bracketSizes[idx]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 747 | bool run_was_full = false; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 748 | MutexLock mu(self, *size_bracket_locks_[idx]); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 749 | if (kIsDebugBuild) { |
| 750 | run_was_full = run->IsFull(); |
| 751 | } |
| 752 | if (kTraceRosAlloc) { |
| 753 | LOG(INFO) << "RosAlloc::FreeFromRun() : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr); |
| 754 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 755 | if (LIKELY(run->IsThreadLocal())) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 756 | // It's a thread-local run. Just mark the thread-local free bit map and return. |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 757 | DCHECK_LT(run->size_bracket_idx_, kNumThreadLocalSizeBrackets); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 758 | DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end()); |
| 759 | DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end()); |
| 760 | run->MarkThreadLocalFreeBitMap(ptr); |
| 761 | if (kTraceRosAlloc) { |
| 762 | LOG(INFO) << "RosAlloc::FreeFromRun() : Freed a slot in a thread local run 0x" << std::hex |
| 763 | << reinterpret_cast<intptr_t>(run); |
| 764 | } |
| 765 | // A thread local run will be kept as a thread local even if it's become all free. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 766 | return bracket_size; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 767 | } |
| 768 | // Free the slot in the run. |
| 769 | run->FreeSlot(ptr); |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 770 | auto* non_full_runs = &non_full_runs_[idx]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 771 | if (run->IsAllFree()) { |
| 772 | // It has just become completely free. Free the pages of this run. |
| 773 | std::set<Run*>::iterator pos = non_full_runs->find(run); |
| 774 | if (pos != non_full_runs->end()) { |
| 775 | non_full_runs->erase(pos); |
| 776 | if (kTraceRosAlloc) { |
| 777 | LOG(INFO) << "RosAlloc::FreeFromRun() : Erased run 0x" << std::hex |
| 778 | << reinterpret_cast<intptr_t>(run) << " from non_full_runs_"; |
| 779 | } |
| 780 | } |
| 781 | if (run == current_runs_[idx]) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 782 | current_runs_[idx] = dedicated_full_run_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 783 | } |
| 784 | DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end()); |
| 785 | DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end()); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 786 | run->ZeroHeader(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 787 | { |
| 788 | MutexLock mu(self, lock_); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 789 | FreePages(self, run, true); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 790 | } |
| 791 | } else { |
| 792 | // It is not completely free. If it wasn't the current run or |
| 793 | // already in the non-full run set (i.e., it was full) insert it |
| 794 | // into the non-full run set. |
| 795 | if (run != current_runs_[idx]) { |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 796 | auto* full_runs = kIsDebugBuild ? &full_runs_[idx] : NULL; |
| 797 | auto pos = non_full_runs->find(run); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 798 | if (pos == non_full_runs->end()) { |
| 799 | DCHECK(run_was_full); |
| 800 | DCHECK(full_runs->find(run) != full_runs->end()); |
| 801 | if (kIsDebugBuild) { |
| 802 | full_runs->erase(run); |
| 803 | if (kTraceRosAlloc) { |
| 804 | LOG(INFO) << "RosAlloc::FreeFromRun() : Erased run 0x" << std::hex |
| 805 | << reinterpret_cast<intptr_t>(run) << " from full_runs_"; |
| 806 | } |
| 807 | } |
| 808 | non_full_runs->insert(run); |
| 809 | DCHECK(!run->IsFull()); |
| 810 | if (kTraceRosAlloc) { |
| 811 | LOG(INFO) << "RosAlloc::FreeFromRun() : Inserted run 0x" << std::hex |
| 812 | << reinterpret_cast<intptr_t>(run) |
| 813 | << " into non_full_runs_[" << std::dec << idx << "]"; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 818 | return bracket_size; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 821 | std::string RosAlloc::Run::BitMapToStr(uint32_t* bit_map_base, size_t num_vec) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 822 | std::string bit_map_str; |
| 823 | for (size_t v = 0; v < num_vec; v++) { |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 824 | uint32_t vec = bit_map_base[v]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 825 | if (v != num_vec - 1) { |
| 826 | bit_map_str.append(StringPrintf("%x-", vec)); |
| 827 | } else { |
| 828 | bit_map_str.append(StringPrintf("%x", vec)); |
| 829 | } |
| 830 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 831 | return bit_map_str.c_str(); |
| 832 | } |
| 833 | |
| 834 | std::string RosAlloc::Run::Dump() { |
| 835 | size_t idx = size_bracket_idx_; |
| 836 | size_t num_slots = numOfSlots[idx]; |
| 837 | size_t num_vec = RoundUp(num_slots, 32) / 32; |
| 838 | std::ostringstream stream; |
| 839 | stream << "RosAlloc Run = " << reinterpret_cast<void*>(this) |
| 840 | << "{ magic_num=" << static_cast<int>(magic_num_) |
| 841 | << " size_bracket_idx=" << idx |
| 842 | << " is_thread_local=" << static_cast<int>(is_thread_local_) |
| 843 | << " to_be_bulk_freed=" << static_cast<int>(to_be_bulk_freed_) |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 844 | << " first_search_vec_idx=" << first_search_vec_idx_ |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 845 | << " alloc_bit_map=" << BitMapToStr(alloc_bit_map_, num_vec) |
| 846 | << " bulk_free_bit_map=" << BitMapToStr(BulkFreeBitMap(), num_vec) |
| 847 | << " thread_local_bit_map=" << BitMapToStr(ThreadLocalFreeBitMap(), num_vec) |
| 848 | << " }" << std::endl; |
| 849 | return stream.str(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 852 | inline void* RosAlloc::Run::AllocSlot() { |
| 853 | const size_t idx = size_bracket_idx_; |
| 854 | while (true) { |
| 855 | if (kIsDebugBuild) { |
| 856 | // Make sure that no slots leaked, the bitmap should be full for all previous vectors. |
| 857 | for (size_t i = 0; i < first_search_vec_idx_; ++i) { |
| 858 | CHECK_EQ(~alloc_bit_map_[i], 0U); |
| 859 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 860 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 861 | uint32_t* const alloc_bitmap_ptr = &alloc_bit_map_[first_search_vec_idx_]; |
| 862 | uint32_t ffz1 = __builtin_ffs(~*alloc_bitmap_ptr); |
| 863 | if (LIKELY(ffz1 != 0)) { |
| 864 | const uint32_t ffz = ffz1 - 1; |
| 865 | const uint32_t slot_idx = ffz + first_search_vec_idx_ * sizeof(*alloc_bitmap_ptr) * kBitsPerByte; |
| 866 | const uint32_t mask = 1U << ffz; |
| 867 | DCHECK_LT(slot_idx, numOfSlots[idx]) << "out of range"; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 868 | // Found an empty slot. Set the bit. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 869 | DCHECK_EQ(*alloc_bitmap_ptr & mask, 0U); |
| 870 | *alloc_bitmap_ptr |= mask; |
| 871 | DCHECK_NE(*alloc_bitmap_ptr & mask, 0U); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 872 | uint8_t* slot_addr = reinterpret_cast<uint8_t*>(this) + headerSizes[idx] + slot_idx * bracketSizes[idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 873 | if (kTraceRosAlloc) { |
| 874 | LOG(INFO) << "RosAlloc::Run::AllocSlot() : 0x" << std::hex << reinterpret_cast<intptr_t>(slot_addr) |
| 875 | << ", bracket_size=" << std::dec << bracketSizes[idx] << ", slot_idx=" << slot_idx; |
| 876 | } |
| 877 | return slot_addr; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 878 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 879 | const size_t num_words = RoundUp(numOfSlots[idx], 32) / 32; |
| 880 | if (first_search_vec_idx_ + 1 >= num_words) { |
| 881 | DCHECK(IsFull()); |
| 882 | // Already at the last word, return null. |
| 883 | return nullptr; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 884 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 885 | // Increase the index to the next word and try again. |
| 886 | ++first_search_vec_idx_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 887 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 890 | void RosAlloc::Run::FreeSlot(void* ptr) { |
| 891 | DCHECK(!IsThreadLocal()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 892 | const uint8_t idx = size_bracket_idx_; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 893 | const size_t bracket_size = bracketSizes[idx]; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 894 | const size_t offset_from_slot_base = reinterpret_cast<uint8_t*>(ptr) |
| 895 | - (reinterpret_cast<uint8_t*>(this) + headerSizes[idx]); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 896 | DCHECK_EQ(offset_from_slot_base % bracket_size, static_cast<size_t>(0)); |
| 897 | size_t slot_idx = offset_from_slot_base / bracket_size; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 898 | DCHECK_LT(slot_idx, numOfSlots[idx]); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 899 | size_t vec_idx = slot_idx / 32; |
| 900 | if (kIsDebugBuild) { |
| 901 | size_t num_vec = RoundUp(numOfSlots[idx], 32) / 32; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 902 | DCHECK_LT(vec_idx, num_vec); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 903 | } |
| 904 | size_t vec_off = slot_idx % 32; |
| 905 | uint32_t* vec = &alloc_bit_map_[vec_idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 906 | first_search_vec_idx_ = std::min(first_search_vec_idx_, static_cast<uint32_t>(vec_idx)); |
| 907 | const uint32_t mask = 1U << vec_off; |
| 908 | DCHECK_NE(*vec & mask, 0U); |
| 909 | *vec &= ~mask; |
| 910 | DCHECK_EQ(*vec & mask, 0U); |
| 911 | // Zero out the memory. |
| 912 | // TODO: Investigate alternate memset since ptr is guaranteed to be aligned to 16. |
| 913 | memset(ptr, 0, bracket_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 914 | if (kTraceRosAlloc) { |
| 915 | LOG(INFO) << "RosAlloc::Run::FreeSlot() : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr) |
| 916 | << ", bracket_size=" << std::dec << bracketSizes[idx] << ", slot_idx=" << slot_idx; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | inline bool RosAlloc::Run::MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 921 | DCHECK(IsThreadLocal()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 922 | // Free slots in the alloc bit map based on the thread local free bit map. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 923 | const size_t idx = size_bracket_idx_; |
| 924 | const size_t num_of_slots = numOfSlots[idx]; |
| 925 | const size_t num_vec = RoundUp(num_of_slots, 32) / 32; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 926 | bool changed = false; |
| 927 | uint32_t* vecp = &alloc_bit_map_[0]; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 928 | uint32_t* tl_free_vecp = &ThreadLocalFreeBitMap()[0]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 929 | bool is_all_free_after = true; |
| 930 | for (size_t v = 0; v < num_vec; v++, vecp++, tl_free_vecp++) { |
| 931 | uint32_t tl_free_vec = *tl_free_vecp; |
| 932 | uint32_t vec_before = *vecp; |
| 933 | uint32_t vec_after; |
| 934 | if (tl_free_vec != 0) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 935 | first_search_vec_idx_ = std::min(first_search_vec_idx_, static_cast<uint32_t>(v)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 936 | vec_after = vec_before & ~tl_free_vec; |
| 937 | *vecp = vec_after; |
| 938 | changed = true; |
| 939 | *tl_free_vecp = 0; // clear the thread local free bit map. |
| 940 | } else { |
| 941 | vec_after = vec_before; |
| 942 | } |
| 943 | if (vec_after != 0) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 944 | if (v == num_vec - 1) { |
| 945 | // Only not all free if a bit other than the mask bits are set. |
| 946 | is_all_free_after = |
| 947 | is_all_free_after && GetBitmapLastVectorMask(num_of_slots, num_vec) == vec_after; |
| 948 | } else { |
| 949 | is_all_free_after = false; |
| 950 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 951 | } |
| 952 | DCHECK_EQ(*tl_free_vecp, static_cast<uint32_t>(0)); |
| 953 | } |
| 954 | *is_all_free_after_out = is_all_free_after; |
| 955 | // Return true if there was at least a bit set in the thread-local |
| 956 | // free bit map and at least a bit in the alloc bit map changed. |
| 957 | return changed; |
| 958 | } |
| 959 | |
| 960 | inline void RosAlloc::Run::MergeBulkFreeBitMapIntoAllocBitMap() { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 961 | DCHECK(!IsThreadLocal()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 962 | // Free slots in the alloc bit map based on the bulk free bit map. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 963 | const size_t num_vec = NumberOfBitmapVectors(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 964 | uint32_t* vecp = &alloc_bit_map_[0]; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 965 | uint32_t* free_vecp = &BulkFreeBitMap()[0]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 966 | for (size_t v = 0; v < num_vec; v++, vecp++, free_vecp++) { |
| 967 | uint32_t free_vec = *free_vecp; |
| 968 | if (free_vec != 0) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 969 | first_search_vec_idx_ = std::min(first_search_vec_idx_, static_cast<uint32_t>(v)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 970 | *vecp &= ~free_vec; |
| 971 | *free_vecp = 0; // clear the bulk free bit map. |
| 972 | } |
| 973 | DCHECK_EQ(*free_vecp, static_cast<uint32_t>(0)); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | inline void RosAlloc::Run::UnionBulkFreeBitMapToThreadLocalFreeBitMap() { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 978 | DCHECK(IsThreadLocal()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 979 | // Union the thread local bit map with the bulk free bit map. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 980 | size_t num_vec = NumberOfBitmapVectors(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 981 | uint32_t* to_vecp = &ThreadLocalFreeBitMap()[0]; |
| 982 | uint32_t* from_vecp = &BulkFreeBitMap()[0]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 983 | for (size_t v = 0; v < num_vec; v++, to_vecp++, from_vecp++) { |
| 984 | uint32_t from_vec = *from_vecp; |
| 985 | if (from_vec != 0) { |
| 986 | *to_vecp |= from_vec; |
Hiroshi Yamauchi | 70f6004 | 2014-02-03 12:31:29 -0800 | [diff] [blame] | 987 | *from_vecp = 0; // clear the bulk free bit map. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 988 | } |
| 989 | DCHECK_EQ(*from_vecp, static_cast<uint32_t>(0)); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | inline void RosAlloc::Run::MarkThreadLocalFreeBitMap(void* ptr) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 994 | DCHECK(IsThreadLocal()); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 995 | MarkFreeBitMapShared(ptr, ThreadLocalFreeBitMap(), "MarkThreadLocalFreeBitMap"); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 998 | inline size_t RosAlloc::Run::MarkBulkFreeBitMap(void* ptr) { |
| 999 | return MarkFreeBitMapShared(ptr, BulkFreeBitMap(), "MarkFreeBitMap"); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1002 | inline size_t RosAlloc::Run::MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, |
| 1003 | const char* caller_name) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1004 | const uint8_t idx = size_bracket_idx_; |
| 1005 | const size_t offset_from_slot_base = reinterpret_cast<uint8_t*>(ptr) |
| 1006 | - (reinterpret_cast<uint8_t*>(this) + headerSizes[idx]); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1007 | const size_t bracket_size = bracketSizes[idx]; |
| 1008 | memset(ptr, 0, bracket_size); |
| 1009 | DCHECK_EQ(offset_from_slot_base % bracket_size, static_cast<size_t>(0)); |
| 1010 | size_t slot_idx = offset_from_slot_base / bracket_size; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1011 | DCHECK_LT(slot_idx, numOfSlots[idx]); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1012 | size_t vec_idx = slot_idx / 32; |
| 1013 | if (kIsDebugBuild) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1014 | size_t num_vec = NumberOfBitmapVectors(); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1015 | DCHECK_LT(vec_idx, num_vec); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1016 | } |
| 1017 | size_t vec_off = slot_idx % 32; |
| 1018 | uint32_t* vec = &free_bit_map_base[vec_idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1019 | const uint32_t mask = 1U << vec_off; |
| 1020 | DCHECK_EQ(*vec & mask, 0U); |
| 1021 | *vec |= mask; |
| 1022 | DCHECK_NE(*vec & mask, 0U); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1023 | if (kTraceRosAlloc) { |
| 1024 | LOG(INFO) << "RosAlloc::Run::" << caller_name << "() : 0x" << std::hex |
| 1025 | << reinterpret_cast<intptr_t>(ptr) |
| 1026 | << ", bracket_size=" << std::dec << bracketSizes[idx] << ", slot_idx=" << slot_idx; |
| 1027 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1028 | return bracket_size; |
| 1029 | } |
| 1030 | |
| 1031 | inline uint32_t RosAlloc::Run::GetBitmapLastVectorMask(size_t num_slots, size_t num_vec) { |
| 1032 | const size_t kBitsPerVec = 32; |
| 1033 | DCHECK_GE(num_slots * kBitsPerVec, num_vec); |
| 1034 | size_t remain = num_vec * kBitsPerVec - num_slots; |
| 1035 | DCHECK_NE(remain, kBitsPerVec); |
| 1036 | return ((1U << remain) - 1) << (kBitsPerVec - remain); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | inline bool RosAlloc::Run::IsAllFree() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1040 | const uint8_t idx = size_bracket_idx_; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1041 | const size_t num_slots = numOfSlots[idx]; |
| 1042 | const size_t num_vec = NumberOfBitmapVectors(); |
| 1043 | DCHECK_NE(num_vec, 0U); |
| 1044 | // Check the last vector after the loop since it uses a special case for the masked bits. |
| 1045 | for (size_t v = 0; v < num_vec - 1; v++) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1046 | uint32_t vec = alloc_bit_map_[v]; |
| 1047 | if (vec != 0) { |
| 1048 | return false; |
| 1049 | } |
| 1050 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1051 | // Make sure the last word is equal to the mask, all other bits must be 0. |
| 1052 | return alloc_bit_map_[num_vec - 1] == GetBitmapLastVectorMask(num_slots, num_vec); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | inline bool RosAlloc::Run::IsFull() { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1056 | const size_t num_vec = NumberOfBitmapVectors(); |
| 1057 | for (size_t v = 0; v < num_vec; ++v) { |
| 1058 | if (~alloc_bit_map_[v] != 0) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1059 | return false; |
| 1060 | } |
| 1061 | } |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1065 | inline bool RosAlloc::Run::IsBulkFreeBitmapClean() { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1066 | const size_t num_vec = NumberOfBitmapVectors(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1067 | for (size_t v = 0; v < num_vec; v++) { |
| 1068 | uint32_t vec = BulkFreeBitMap()[v]; |
| 1069 | if (vec != 0) { |
| 1070 | return false; |
| 1071 | } |
| 1072 | } |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
| 1076 | inline bool RosAlloc::Run::IsThreadLocalFreeBitmapClean() { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1077 | const size_t num_vec = NumberOfBitmapVectors(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1078 | for (size_t v = 0; v < num_vec; v++) { |
| 1079 | uint32_t vec = ThreadLocalFreeBitMap()[v]; |
| 1080 | if (vec != 0) { |
| 1081 | return false; |
| 1082 | } |
| 1083 | } |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1087 | inline void RosAlloc::Run::SetAllocBitMapBitsForInvalidSlots() { |
| 1088 | const size_t idx = size_bracket_idx_; |
| 1089 | const size_t num_slots = numOfSlots[idx]; |
| 1090 | const size_t num_vec = RoundUp(num_slots, 32) / 32; |
| 1091 | DCHECK_NE(num_vec, 0U); |
| 1092 | // Make sure to set the bits at the end of the bitmap so that we don't allocate there since they |
| 1093 | // don't represent valid slots. |
| 1094 | alloc_bit_map_[num_vec - 1] |= GetBitmapLastVectorMask(num_slots, num_vec); |
| 1095 | } |
| 1096 | |
| 1097 | inline void RosAlloc::Run::ZeroHeader() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1098 | const uint8_t idx = size_bracket_idx_; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1099 | memset(this, 0, headerSizes[idx]); |
| 1100 | } |
| 1101 | |
| 1102 | inline void RosAlloc::Run::ZeroData() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1103 | const uint8_t idx = size_bracket_idx_; |
| 1104 | uint8_t* slot_begin = reinterpret_cast<uint8_t*>(this) + headerSizes[idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1105 | memset(slot_begin, 0, numOfSlots[idx] * bracketSizes[idx]); |
| 1106 | } |
| 1107 | |
| 1108 | inline void RosAlloc::Run::FillAllocBitMap() { |
| 1109 | size_t num_vec = NumberOfBitmapVectors(); |
| 1110 | memset(alloc_bit_map_, 0xFF, sizeof(uint32_t) * num_vec); |
| 1111 | first_search_vec_idx_ = num_vec - 1; // No free bits in any of the bitmap words. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | void RosAlloc::Run::InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), |
| 1115 | void* arg) { |
| 1116 | size_t idx = size_bracket_idx_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1117 | uint8_t* slot_base = reinterpret_cast<uint8_t*>(this) + headerSizes[idx]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1118 | size_t num_slots = numOfSlots[idx]; |
| 1119 | size_t bracket_size = IndexToBracketSize(idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1120 | DCHECK_EQ(slot_base + num_slots * bracket_size, reinterpret_cast<uint8_t*>(this) + numOfPages[idx] * kPageSize); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1121 | size_t num_vec = RoundUp(num_slots, 32) / 32; |
| 1122 | size_t slots = 0; |
| 1123 | for (size_t v = 0; v < num_vec; v++, slots += 32) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1124 | DCHECK_GE(num_slots, slots); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1125 | uint32_t vec = alloc_bit_map_[v]; |
| 1126 | size_t end = std::min(num_slots - slots, static_cast<size_t>(32)); |
| 1127 | for (size_t i = 0; i < end; ++i) { |
| 1128 | bool is_allocated = ((vec >> i) & 0x1) != 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1129 | uint8_t* slot_addr = slot_base + (slots + i) * bracket_size; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1130 | if (is_allocated) { |
| 1131 | handler(slot_addr, slot_addr + bracket_size, bracket_size, arg); |
| 1132 | } else { |
| 1133 | handler(slot_addr, slot_addr + bracket_size, 0, arg); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1139 | // If true, read the page map entries in BulkFree() without using the |
| 1140 | // lock for better performance, assuming that the existence of an |
| 1141 | // allocated chunk/pointer being freed in BulkFree() guarantees that |
| 1142 | // the page map entry won't change. Disabled for now. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1143 | static constexpr bool kReadPageMapEntryWithoutLockInBulkFree = true; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1144 | |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1145 | size_t RosAlloc::BulkFree(Thread* self, void** ptrs, size_t num_ptrs) { |
| 1146 | size_t freed_bytes = 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1147 | if (false) { |
| 1148 | // Used only to test Free() as GC uses only BulkFree(). |
| 1149 | for (size_t i = 0; i < num_ptrs; ++i) { |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1150 | freed_bytes += FreeInternal(self, ptrs[i]); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1151 | } |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1152 | return freed_bytes; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | WriterMutexLock wmu(self, bulk_free_lock_); |
| 1156 | |
| 1157 | // First mark slots to free in the bulk free bit map without locking the |
Ian Rogers | 5fcfa7d | 2014-05-15 11:43:06 -0700 | [diff] [blame] | 1158 | // size bracket locks. On host, unordered_set is faster than vector + flag. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1159 | #ifdef HAVE_ANDROID_OS |
| 1160 | std::vector<Run*> runs; |
| 1161 | #else |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1162 | std::unordered_set<Run*, hash_run, eq_run> runs; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1163 | #endif |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1164 | for (size_t i = 0; i < num_ptrs; i++) { |
| 1165 | void* ptr = ptrs[i]; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1166 | DCHECK_LE(base_, ptr); |
| 1167 | DCHECK_LT(ptr, base_ + footprint_); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1168 | size_t pm_idx = RoundDownToPageMapIndex(ptr); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1169 | Run* run = nullptr; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1170 | if (kReadPageMapEntryWithoutLockInBulkFree) { |
| 1171 | // Read the page map entries without locking the lock. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1172 | uint8_t page_map_entry = page_map_[pm_idx]; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1173 | if (kTraceRosAlloc) { |
| 1174 | LOG(INFO) << "RosAlloc::BulkFree() : " << std::hex << ptr << ", pm_idx=" |
| 1175 | << std::dec << pm_idx |
| 1176 | << ", page_map_entry=" << static_cast<int>(page_map_entry); |
| 1177 | } |
| 1178 | if (LIKELY(page_map_entry == kPageMapRun)) { |
| 1179 | run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1180 | } else if (LIKELY(page_map_entry == kPageMapRunPart)) { |
| 1181 | size_t pi = pm_idx; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1182 | // Find the beginning of the run. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1183 | do { |
| 1184 | --pi; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1185 | DCHECK_LT(pi, capacity_ / kPageSize); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1186 | } while (page_map_[pi] != kPageMapRun); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1187 | run = reinterpret_cast<Run*>(base_ + pi * kPageSize); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1188 | } else if (page_map_entry == kPageMapLargeObject) { |
| 1189 | MutexLock mu(self, lock_); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1190 | freed_bytes += FreePages(self, ptr, false); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1191 | continue; |
| 1192 | } else { |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1193 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_entry); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1194 | } |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1195 | } else { |
| 1196 | // Read the page map entries with a lock. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1197 | MutexLock mu(self, lock_); |
| 1198 | DCHECK_LT(pm_idx, page_map_size_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1199 | uint8_t page_map_entry = page_map_[pm_idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1200 | if (kTraceRosAlloc) { |
| 1201 | LOG(INFO) << "RosAlloc::BulkFree() : " << std::hex << ptr << ", pm_idx=" |
| 1202 | << std::dec << pm_idx |
| 1203 | << ", page_map_entry=" << static_cast<int>(page_map_entry); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1204 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1205 | if (LIKELY(page_map_entry == kPageMapRun)) { |
| 1206 | run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
| 1207 | } else if (LIKELY(page_map_entry == kPageMapRunPart)) { |
| 1208 | size_t pi = pm_idx; |
| 1209 | // Find the beginning of the run. |
| 1210 | do { |
| 1211 | --pi; |
| 1212 | DCHECK_LT(pi, capacity_ / kPageSize); |
| 1213 | } while (page_map_[pi] != kPageMapRun); |
| 1214 | run = reinterpret_cast<Run*>(base_ + pi * kPageSize); |
| 1215 | } else if (page_map_entry == kPageMapLargeObject) { |
| 1216 | freed_bytes += FreePages(self, ptr, false); |
| 1217 | continue; |
| 1218 | } else { |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1219 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_entry); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1220 | } |
| 1221 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1222 | DCHECK(run != nullptr); |
| 1223 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| 1224 | // Set the bit in the bulk free bit map. |
| 1225 | freed_bytes += run->MarkBulkFreeBitMap(ptr); |
| 1226 | #ifdef HAVE_ANDROID_OS |
| 1227 | if (!run->to_be_bulk_freed_) { |
| 1228 | run->to_be_bulk_freed_ = true; |
| 1229 | runs.push_back(run); |
| 1230 | } |
| 1231 | #else |
| 1232 | runs.insert(run); |
| 1233 | #endif |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | // Now, iterate over the affected runs and update the alloc bit map |
| 1237 | // based on the bulk free bit map (for non-thread-local runs) and |
| 1238 | // union the bulk free bit map into the thread-local free bit map |
| 1239 | // (for thread-local runs.) |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1240 | for (Run* run : runs) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1241 | #ifdef HAVE_ANDROID_OS |
| 1242 | DCHECK(run->to_be_bulk_freed_); |
| 1243 | run->to_be_bulk_freed_ = false; |
| 1244 | #endif |
| 1245 | size_t idx = run->size_bracket_idx_; |
| 1246 | MutexLock mu(self, *size_bracket_locks_[idx]); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1247 | if (run->IsThreadLocal()) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1248 | DCHECK_LT(run->size_bracket_idx_, kNumThreadLocalSizeBrackets); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1249 | DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end()); |
| 1250 | DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end()); |
| 1251 | run->UnionBulkFreeBitMapToThreadLocalFreeBitMap(); |
| 1252 | if (kTraceRosAlloc) { |
| 1253 | LOG(INFO) << "RosAlloc::BulkFree() : Freed slot(s) in a thread local run 0x" |
| 1254 | << std::hex << reinterpret_cast<intptr_t>(run); |
| 1255 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1256 | DCHECK(run->IsThreadLocal()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1257 | // A thread local run will be kept as a thread local even if |
| 1258 | // it's become all free. |
| 1259 | } else { |
| 1260 | bool run_was_full = run->IsFull(); |
| 1261 | run->MergeBulkFreeBitMapIntoAllocBitMap(); |
| 1262 | if (kTraceRosAlloc) { |
| 1263 | LOG(INFO) << "RosAlloc::BulkFree() : Freed slot(s) in a run 0x" << std::hex |
| 1264 | << reinterpret_cast<intptr_t>(run); |
| 1265 | } |
| 1266 | // Check if the run should be moved to non_full_runs_ or |
| 1267 | // free_page_runs_. |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 1268 | auto* non_full_runs = &non_full_runs_[idx]; |
| 1269 | auto* full_runs = kIsDebugBuild ? &full_runs_[idx] : NULL; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1270 | if (run->IsAllFree()) { |
| 1271 | // It has just become completely free. Free the pages of the |
| 1272 | // run. |
| 1273 | bool run_was_current = run == current_runs_[idx]; |
| 1274 | if (run_was_current) { |
| 1275 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1276 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1277 | // If it was a current run, reuse it. |
| 1278 | } else if (run_was_full) { |
| 1279 | // If it was full, remove it from the full run set (debug |
| 1280 | // only.) |
| 1281 | if (kIsDebugBuild) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1282 | std::unordered_set<Run*, hash_run, eq_run>::iterator pos = full_runs->find(run); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1283 | DCHECK(pos != full_runs->end()); |
| 1284 | full_runs->erase(pos); |
| 1285 | if (kTraceRosAlloc) { |
| 1286 | LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex |
| 1287 | << reinterpret_cast<intptr_t>(run) |
| 1288 | << " from full_runs_"; |
| 1289 | } |
| 1290 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1291 | } |
| 1292 | } else { |
| 1293 | // If it was in a non full run set, remove it from the set. |
| 1294 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1295 | DCHECK(non_full_runs->find(run) != non_full_runs->end()); |
| 1296 | non_full_runs->erase(run); |
| 1297 | if (kTraceRosAlloc) { |
| 1298 | LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex |
| 1299 | << reinterpret_cast<intptr_t>(run) |
| 1300 | << " from non_full_runs_"; |
| 1301 | } |
| 1302 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1303 | } |
| 1304 | if (!run_was_current) { |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1305 | run->ZeroHeader(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1306 | MutexLock mu(self, lock_); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1307 | FreePages(self, run, true); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1308 | } |
| 1309 | } else { |
| 1310 | // It is not completely free. If it wasn't the current run or |
| 1311 | // already in the non-full run set (i.e., it was full) insert |
| 1312 | // it into the non-full run set. |
| 1313 | if (run == current_runs_[idx]) { |
| 1314 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1315 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1316 | // If it was a current run, keep it. |
| 1317 | } else if (run_was_full) { |
| 1318 | // If it was full, remove it from the full run set (debug |
| 1319 | // only) and insert into the non-full run set. |
| 1320 | DCHECK(full_runs->find(run) != full_runs->end()); |
| 1321 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1322 | if (kIsDebugBuild) { |
| 1323 | full_runs->erase(run); |
| 1324 | if (kTraceRosAlloc) { |
| 1325 | LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex |
| 1326 | << reinterpret_cast<intptr_t>(run) |
| 1327 | << " from full_runs_"; |
| 1328 | } |
| 1329 | } |
| 1330 | non_full_runs->insert(run); |
| 1331 | if (kTraceRosAlloc) { |
| 1332 | LOG(INFO) << "RosAlloc::BulkFree() : Inserted run 0x" << std::hex |
| 1333 | << reinterpret_cast<intptr_t>(run) |
| 1334 | << " into non_full_runs_[" << std::dec << idx; |
| 1335 | } |
| 1336 | } else { |
| 1337 | // If it was not full, so leave it in the non full run set. |
| 1338 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1339 | DCHECK(non_full_runs->find(run) != non_full_runs->end()); |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | } |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1344 | return freed_bytes; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1347 | std::string RosAlloc::DumpPageMap() { |
| 1348 | std::ostringstream stream; |
| 1349 | stream << "RosAlloc PageMap: " << std::endl; |
| 1350 | lock_.AssertHeld(Thread::Current()); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1351 | size_t end = page_map_size_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1352 | FreePageRun* curr_fpr = NULL; |
| 1353 | size_t curr_fpr_size = 0; |
| 1354 | size_t remaining_curr_fpr_size = 0; |
| 1355 | size_t num_running_empty_pages = 0; |
| 1356 | for (size_t i = 0; i < end; ++i) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1357 | uint8_t pm = page_map_[i]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1358 | switch (pm) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1359 | case kPageMapReleased: |
| 1360 | // Fall-through. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1361 | case kPageMapEmpty: { |
| 1362 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
| 1363 | if (free_page_runs_.find(fpr) != free_page_runs_.end()) { |
| 1364 | // Encountered a fresh free page run. |
| 1365 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1366 | DCHECK(fpr->IsFree()); |
| 1367 | DCHECK(curr_fpr == NULL); |
| 1368 | DCHECK_EQ(curr_fpr_size, static_cast<size_t>(0)); |
| 1369 | curr_fpr = fpr; |
| 1370 | curr_fpr_size = fpr->ByteSize(this); |
| 1371 | DCHECK_EQ(curr_fpr_size % kPageSize, static_cast<size_t>(0)); |
| 1372 | remaining_curr_fpr_size = curr_fpr_size - kPageSize; |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1373 | stream << "[" << i << "]=" << (pm == kPageMapReleased ? "Released" : "Empty") |
| 1374 | << " (FPR start) fpr_size=" << curr_fpr_size |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1375 | << " remaining_fpr_size=" << remaining_curr_fpr_size << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1376 | if (remaining_curr_fpr_size == 0) { |
| 1377 | // Reset at the end of the current free page run. |
| 1378 | curr_fpr = NULL; |
| 1379 | curr_fpr_size = 0; |
| 1380 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1381 | stream << "curr_fpr=0x" << std::hex << reinterpret_cast<intptr_t>(curr_fpr) << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1382 | DCHECK_EQ(num_running_empty_pages, static_cast<size_t>(0)); |
| 1383 | } else { |
| 1384 | // Still part of the current free page run. |
| 1385 | DCHECK_NE(num_running_empty_pages, static_cast<size_t>(0)); |
| 1386 | DCHECK(curr_fpr != NULL && curr_fpr_size > 0 && remaining_curr_fpr_size > 0); |
| 1387 | DCHECK_EQ(remaining_curr_fpr_size % kPageSize, static_cast<size_t>(0)); |
| 1388 | DCHECK_GE(remaining_curr_fpr_size, static_cast<size_t>(kPageSize)); |
| 1389 | remaining_curr_fpr_size -= kPageSize; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1390 | stream << "[" << i << "]=Empty (FPR part)" |
| 1391 | << " remaining_fpr_size=" << remaining_curr_fpr_size << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1392 | if (remaining_curr_fpr_size == 0) { |
| 1393 | // Reset at the end of the current free page run. |
| 1394 | curr_fpr = NULL; |
| 1395 | curr_fpr_size = 0; |
| 1396 | } |
| 1397 | } |
| 1398 | num_running_empty_pages++; |
| 1399 | break; |
| 1400 | } |
| 1401 | case kPageMapLargeObject: { |
| 1402 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1403 | num_running_empty_pages = 0; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1404 | stream << "[" << i << "]=Large (start)" << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | } |
| 1407 | case kPageMapLargeObjectPart: |
| 1408 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1409 | num_running_empty_pages = 0; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1410 | stream << "[" << i << "]=Large (part)" << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1411 | break; |
| 1412 | case kPageMapRun: { |
| 1413 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1414 | num_running_empty_pages = 0; |
| 1415 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
| 1416 | size_t idx = run->size_bracket_idx_; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1417 | stream << "[" << i << "]=Run (start)" |
| 1418 | << " idx=" << idx |
| 1419 | << " numOfPages=" << numOfPages[idx] |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1420 | << " is_thread_local=" << run->is_thread_local_ |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1421 | << " is_all_free=" << (run->IsAllFree() ? 1 : 0) |
| 1422 | << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | case kPageMapRunPart: |
| 1426 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1427 | num_running_empty_pages = 0; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1428 | stream << "[" << i << "]=Run (part)" << std::endl; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | default: |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1431 | stream << "[" << i << "]=Unrecognizable page map type: " << pm; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1432 | break; |
| 1433 | } |
| 1434 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1435 | return stream.str(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | size_t RosAlloc::UsableSize(void* ptr) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1439 | DCHECK_LE(base_, ptr); |
| 1440 | DCHECK_LT(ptr, base_ + footprint_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1441 | size_t pm_idx = RoundDownToPageMapIndex(ptr); |
| 1442 | MutexLock mu(Thread::Current(), lock_); |
| 1443 | switch (page_map_[pm_idx]) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1444 | case kPageMapReleased: |
| 1445 | // Fall-through. |
| 1446 | case kPageMapEmpty: |
| 1447 | LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << ": pm_idx=" << pm_idx << ", ptr=" |
| 1448 | << std::hex << reinterpret_cast<intptr_t>(ptr); |
| 1449 | break; |
| 1450 | case kPageMapLargeObject: { |
| 1451 | size_t num_pages = 1; |
| 1452 | size_t idx = pm_idx + 1; |
| 1453 | size_t end = page_map_size_; |
| 1454 | while (idx < end && page_map_[idx] == kPageMapLargeObjectPart) { |
| 1455 | num_pages++; |
| 1456 | idx++; |
| 1457 | } |
| 1458 | return num_pages * kPageSize; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1459 | } |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1460 | case kPageMapLargeObjectPart: |
| 1461 | LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << ": pm_idx=" << pm_idx << ", ptr=" |
| 1462 | << std::hex << reinterpret_cast<intptr_t>(ptr); |
| 1463 | break; |
| 1464 | case kPageMapRun: |
| 1465 | case kPageMapRunPart: { |
| 1466 | // Find the beginning of the run. |
| 1467 | while (page_map_[pm_idx] != kPageMapRun) { |
| 1468 | pm_idx--; |
| 1469 | DCHECK_LT(pm_idx, capacity_ / kPageSize); |
| 1470 | } |
| 1471 | DCHECK_EQ(page_map_[pm_idx], kPageMapRun); |
| 1472 | Run* run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
| 1473 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| 1474 | size_t idx = run->size_bracket_idx_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1475 | size_t offset_from_slot_base = reinterpret_cast<uint8_t*>(ptr) |
| 1476 | - (reinterpret_cast<uint8_t*>(run) + headerSizes[idx]); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1477 | DCHECK_EQ(offset_from_slot_base % bracketSizes[idx], static_cast<size_t>(0)); |
| 1478 | return IndexToBracketSize(idx); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1479 | } |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1480 | default: { |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1481 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1482 | break; |
| 1483 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1484 | } |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | bool RosAlloc::Trim() { |
| 1489 | MutexLock mu(Thread::Current(), lock_); |
| 1490 | FreePageRun* last_free_page_run; |
| 1491 | DCHECK_EQ(footprint_ % kPageSize, static_cast<size_t>(0)); |
| 1492 | auto it = free_page_runs_.rbegin(); |
| 1493 | if (it != free_page_runs_.rend() && (last_free_page_run = *it)->End(this) == base_ + footprint_) { |
| 1494 | // Remove the last free page run, if any. |
| 1495 | DCHECK(last_free_page_run->IsFree()); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1496 | DCHECK(IsFreePage(ToPageMapIndex(last_free_page_run))); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1497 | DCHECK_EQ(last_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 1498 | DCHECK_EQ(last_free_page_run->End(this), base_ + footprint_); |
| 1499 | free_page_runs_.erase(last_free_page_run); |
| 1500 | size_t decrement = last_free_page_run->ByteSize(this); |
| 1501 | size_t new_footprint = footprint_ - decrement; |
| 1502 | DCHECK_EQ(new_footprint % kPageSize, static_cast<size_t>(0)); |
| 1503 | size_t new_num_of_pages = new_footprint / kPageSize; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1504 | DCHECK_GE(page_map_size_, new_num_of_pages); |
| 1505 | // Zero out the tail of the page map. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1506 | uint8_t* zero_begin = const_cast<uint8_t*>(page_map_) + new_num_of_pages; |
| 1507 | uint8_t* madvise_begin = AlignUp(zero_begin, kPageSize); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1508 | DCHECK_LE(madvise_begin, page_map_mem_map_->End()); |
| 1509 | size_t madvise_size = page_map_mem_map_->End() - madvise_begin; |
| 1510 | if (madvise_size > 0) { |
| 1511 | DCHECK_ALIGNED(madvise_begin, kPageSize); |
| 1512 | DCHECK_EQ(RoundUp(madvise_size, kPageSize), madvise_size); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 1513 | if (!kMadviseZeroes) { |
| 1514 | memset(madvise_begin, 0, madvise_size); |
| 1515 | } |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1516 | CHECK_EQ(madvise(madvise_begin, madvise_size, MADV_DONTNEED), 0); |
| 1517 | } |
| 1518 | if (madvise_begin - zero_begin) { |
| 1519 | memset(zero_begin, 0, madvise_begin - zero_begin); |
| 1520 | } |
| 1521 | page_map_size_ = new_num_of_pages; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1522 | free_page_run_size_map_.resize(new_num_of_pages); |
| 1523 | DCHECK_EQ(free_page_run_size_map_.size(), new_num_of_pages); |
| 1524 | art_heap_rosalloc_morecore(this, -(static_cast<intptr_t>(decrement))); |
| 1525 | if (kTraceRosAlloc) { |
| 1526 | LOG(INFO) << "RosAlloc::Trim() : decreased the footprint from " |
| 1527 | << footprint_ << " to " << new_footprint; |
| 1528 | } |
| 1529 | DCHECK_LT(new_footprint, footprint_); |
| 1530 | DCHECK_LT(new_footprint, capacity_); |
| 1531 | footprint_ = new_footprint; |
| 1532 | return true; |
| 1533 | } |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | void RosAlloc::InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), |
| 1538 | void* arg) { |
| 1539 | // Note: no need to use this to release pages as we already do so in FreePages(). |
| 1540 | if (handler == NULL) { |
| 1541 | return; |
| 1542 | } |
| 1543 | MutexLock mu(Thread::Current(), lock_); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1544 | size_t pm_end = page_map_size_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1545 | size_t i = 0; |
| 1546 | while (i < pm_end) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1547 | uint8_t pm = page_map_[i]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1548 | switch (pm) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1549 | case kPageMapReleased: |
| 1550 | // Fall-through. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1551 | case kPageMapEmpty: { |
| 1552 | // The start of a free page run. |
| 1553 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
| 1554 | DCHECK(free_page_runs_.find(fpr) != free_page_runs_.end()); |
| 1555 | size_t fpr_size = fpr->ByteSize(this); |
| 1556 | DCHECK(IsAligned<kPageSize>(fpr_size)); |
| 1557 | void* start = fpr; |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 1558 | if (kIsDebugBuild) { |
| 1559 | // In the debug build, the first page of a free page run |
| 1560 | // contains a magic number for debugging. Exclude it. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1561 | start = reinterpret_cast<uint8_t*>(fpr) + kPageSize; |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 1562 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1563 | void* end = reinterpret_cast<uint8_t*>(fpr) + fpr_size; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1564 | handler(start, end, 0, arg); |
| 1565 | size_t num_pages = fpr_size / kPageSize; |
| 1566 | if (kIsDebugBuild) { |
| 1567 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1568 | DCHECK(IsFreePage(j)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | i += fpr_size / kPageSize; |
| 1572 | DCHECK_LE(i, pm_end); |
| 1573 | break; |
| 1574 | } |
| 1575 | case kPageMapLargeObject: { |
| 1576 | // The start of a large object. |
| 1577 | size_t num_pages = 1; |
| 1578 | size_t idx = i + 1; |
| 1579 | while (idx < pm_end && page_map_[idx] == kPageMapLargeObjectPart) { |
| 1580 | num_pages++; |
| 1581 | idx++; |
| 1582 | } |
| 1583 | void* start = base_ + i * kPageSize; |
| 1584 | void* end = base_ + (i + num_pages) * kPageSize; |
| 1585 | size_t used_bytes = num_pages * kPageSize; |
| 1586 | handler(start, end, used_bytes, arg); |
| 1587 | if (kIsDebugBuild) { |
| 1588 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| 1589 | DCHECK_EQ(page_map_[j], kPageMapLargeObjectPart); |
| 1590 | } |
| 1591 | } |
| 1592 | i += num_pages; |
| 1593 | DCHECK_LE(i, pm_end); |
| 1594 | break; |
| 1595 | } |
| 1596 | case kPageMapLargeObjectPart: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1597 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1598 | break; |
| 1599 | case kPageMapRun: { |
| 1600 | // The start of a run. |
| 1601 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1602 | DCHECK_EQ(run->magic_num_, kMagicNum); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1603 | // The dedicated full run doesn't contain any real allocations, don't visit the slots in |
| 1604 | // there. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1605 | run->InspectAllSlots(handler, arg); |
| 1606 | size_t num_pages = numOfPages[run->size_bracket_idx_]; |
| 1607 | if (kIsDebugBuild) { |
| 1608 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| 1609 | DCHECK_EQ(page_map_[j], kPageMapRunPart); |
| 1610 | } |
| 1611 | } |
| 1612 | i += num_pages; |
| 1613 | DCHECK_LE(i, pm_end); |
| 1614 | break; |
| 1615 | } |
| 1616 | case kPageMapRunPart: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1617 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1618 | break; |
| 1619 | default: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1620 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1621 | break; |
| 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | size_t RosAlloc::Footprint() { |
| 1627 | MutexLock mu(Thread::Current(), lock_); |
| 1628 | return footprint_; |
| 1629 | } |
| 1630 | |
| 1631 | size_t RosAlloc::FootprintLimit() { |
| 1632 | MutexLock mu(Thread::Current(), lock_); |
| 1633 | return capacity_; |
| 1634 | } |
| 1635 | |
| 1636 | void RosAlloc::SetFootprintLimit(size_t new_capacity) { |
| 1637 | MutexLock mu(Thread::Current(), lock_); |
| 1638 | DCHECK_EQ(RoundUp(new_capacity, kPageSize), new_capacity); |
| 1639 | // Only growing is supported here. But Trim() is supported. |
| 1640 | if (capacity_ < new_capacity) { |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1641 | CHECK_LE(new_capacity, max_capacity_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1642 | capacity_ = new_capacity; |
| 1643 | VLOG(heap) << "new capacity=" << capacity_; |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | void RosAlloc::RevokeThreadLocalRuns(Thread* thread) { |
| 1648 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 70f6004 | 2014-02-03 12:31:29 -0800 | [diff] [blame] | 1649 | // Avoid race conditions on the bulk free bit maps with BulkFree() (GC). |
Mathieu Chartier | a1c1c71 | 2014-06-23 17:53:09 -0700 | [diff] [blame] | 1650 | ReaderMutexLock wmu(self, bulk_free_lock_); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1651 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; idx++) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1652 | MutexLock mu(self, *size_bracket_locks_[idx]); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1653 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(idx)); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1654 | CHECK(thread_local_run != nullptr); |
| 1655 | // Invalid means already revoked. |
| 1656 | DCHECK(thread_local_run->IsThreadLocal()); |
| 1657 | if (thread_local_run != dedicated_full_run_) { |
| 1658 | thread->SetRosAllocRun(idx, dedicated_full_run_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1659 | DCHECK_EQ(thread_local_run->magic_num_, kMagicNum); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1660 | // Note the thread local run may not be full here. |
| 1661 | bool dont_care; |
| 1662 | thread_local_run->MergeThreadLocalFreeBitMapToAllocBitMap(&dont_care); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1663 | thread_local_run->SetIsThreadLocal(false); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1664 | thread_local_run->MergeBulkFreeBitMapIntoAllocBitMap(); |
| 1665 | DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 1666 | DCHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end()); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1667 | RevokeRun(self, idx, thread_local_run); |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | void RosAlloc::RevokeRun(Thread* self, size_t idx, Run* run) { |
| 1673 | size_bracket_locks_[idx]->AssertHeld(self); |
| 1674 | DCHECK(run != dedicated_full_run_); |
| 1675 | if (run->IsFull()) { |
| 1676 | if (kIsDebugBuild) { |
| 1677 | full_runs_[idx].insert(run); |
| 1678 | DCHECK(full_runs_[idx].find(run) != full_runs_[idx].end()); |
| 1679 | if (kTraceRosAlloc) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1680 | LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1681 | << reinterpret_cast<intptr_t>(run) |
| 1682 | << " into full_runs_[" << std::dec << idx << "]"; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1683 | } |
| 1684 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1685 | } else if (run->IsAllFree()) { |
| 1686 | run->ZeroHeader(); |
| 1687 | MutexLock mu(self, lock_); |
| 1688 | FreePages(self, run, true); |
| 1689 | } else { |
| 1690 | non_full_runs_[idx].insert(run); |
| 1691 | DCHECK(non_full_runs_[idx].find(run) != non_full_runs_[idx].end()); |
| 1692 | if (kTraceRosAlloc) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1693 | LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1694 | << reinterpret_cast<intptr_t>(run) |
| 1695 | << " into non_full_runs_[" << std::dec << idx << "]"; |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | void RosAlloc::RevokeThreadUnsafeCurrentRuns() { |
| 1701 | // Revoke the current runs which share the same idx as thread local runs. |
| 1702 | Thread* self = Thread::Current(); |
| 1703 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; ++idx) { |
| 1704 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 1705 | if (current_runs_[idx] != dedicated_full_run_) { |
| 1706 | RevokeRun(self, idx, current_runs_[idx]); |
| 1707 | current_runs_[idx] = dedicated_full_run_; |
| 1708 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | void RosAlloc::RevokeAllThreadLocalRuns() { |
| 1713 | // This is called when a mutator thread won't allocate such as at |
| 1714 | // the Zygote creation time or during the GC pause. |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1715 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 1716 | MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1717 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1718 | for (Thread* thread : thread_list) { |
| 1719 | RevokeThreadLocalRuns(thread); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1720 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1721 | RevokeThreadUnsafeCurrentRuns(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1724 | void RosAlloc::AssertThreadLocalRunsAreRevoked(Thread* thread) { |
| 1725 | if (kIsDebugBuild) { |
| 1726 | Thread* self = Thread::Current(); |
| 1727 | // Avoid race conditions on the bulk free bit maps with BulkFree() (GC). |
Mathieu Chartier | a1c1c71 | 2014-06-23 17:53:09 -0700 | [diff] [blame] | 1728 | ReaderMutexLock wmu(self, bulk_free_lock_); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1729 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; idx++) { |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1730 | MutexLock mu(self, *size_bracket_locks_[idx]); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1731 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(idx)); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1732 | DCHECK(thread_local_run == nullptr || thread_local_run == dedicated_full_run_); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | void RosAlloc::AssertAllThreadLocalRunsAreRevoked() { |
| 1738 | if (kIsDebugBuild) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1739 | Thread* self = Thread::Current(); |
| 1740 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1741 | MutexLock mu2(self, *Locks::thread_list_lock_); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1742 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 1743 | for (Thread* t : thread_list) { |
| 1744 | AssertThreadLocalRunsAreRevoked(t); |
| 1745 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1746 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; ++idx) { |
| 1747 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 1748 | CHECK_EQ(current_runs_[idx], dedicated_full_run_); |
| 1749 | } |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1753 | void RosAlloc::Initialize() { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1754 | // bracketSizes. |
| 1755 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 1756 | if (i < kNumOfSizeBrackets - 2) { |
| 1757 | bracketSizes[i] = 16 * (i + 1); |
| 1758 | } else if (i == kNumOfSizeBrackets - 2) { |
| 1759 | bracketSizes[i] = 1 * KB; |
| 1760 | } else { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1761 | DCHECK_EQ(i, kNumOfSizeBrackets - 1); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1762 | bracketSizes[i] = 2 * KB; |
| 1763 | } |
| 1764 | if (kTraceRosAlloc) { |
| 1765 | LOG(INFO) << "bracketSizes[" << i << "]=" << bracketSizes[i]; |
| 1766 | } |
| 1767 | } |
| 1768 | // numOfPages. |
| 1769 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 1770 | if (i < 4) { |
| 1771 | numOfPages[i] = 1; |
| 1772 | } else if (i < 8) { |
| 1773 | numOfPages[i] = 2; |
| 1774 | } else if (i < 16) { |
| 1775 | numOfPages[i] = 4; |
| 1776 | } else if (i < 32) { |
| 1777 | numOfPages[i] = 8; |
| 1778 | } else if (i == 32) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1779 | DCHECK_EQ(i, kNumOfSizeBrackets - 2); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1780 | numOfPages[i] = 16; |
| 1781 | } else { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1782 | DCHECK_EQ(i, kNumOfSizeBrackets - 1); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1783 | numOfPages[i] = 32; |
| 1784 | } |
| 1785 | if (kTraceRosAlloc) { |
| 1786 | LOG(INFO) << "numOfPages[" << i << "]=" << numOfPages[i]; |
| 1787 | } |
| 1788 | } |
| 1789 | // Compute numOfSlots and slotOffsets. |
| 1790 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 1791 | size_t bracket_size = bracketSizes[i]; |
| 1792 | size_t run_size = kPageSize * numOfPages[i]; |
| 1793 | size_t max_num_of_slots = run_size / bracket_size; |
| 1794 | // Compute the actual number of slots by taking the header and |
| 1795 | // alignment into account. |
| 1796 | size_t fixed_header_size = RoundUp(Run::fixed_header_size(), sizeof(uint32_t)); |
| 1797 | DCHECK_EQ(fixed_header_size, static_cast<size_t>(8)); |
| 1798 | size_t header_size = 0; |
| 1799 | size_t bulk_free_bit_map_offset = 0; |
| 1800 | size_t thread_local_free_bit_map_offset = 0; |
| 1801 | size_t num_of_slots = 0; |
| 1802 | // Search for the maximum number of slots that allows enough space |
| 1803 | // for the header (including the bit maps.) |
| 1804 | for (int s = max_num_of_slots; s >= 0; s--) { |
| 1805 | size_t tmp_slots_size = bracket_size * s; |
| 1806 | size_t tmp_bit_map_size = RoundUp(s, sizeof(uint32_t) * kBitsPerByte) / kBitsPerByte; |
| 1807 | size_t tmp_bulk_free_bit_map_size = tmp_bit_map_size; |
| 1808 | size_t tmp_bulk_free_bit_map_off = fixed_header_size + tmp_bit_map_size; |
| 1809 | size_t tmp_thread_local_free_bit_map_size = tmp_bit_map_size; |
| 1810 | size_t tmp_thread_local_free_bit_map_off = tmp_bulk_free_bit_map_off + tmp_bulk_free_bit_map_size; |
| 1811 | size_t tmp_unaligned_header_size = tmp_thread_local_free_bit_map_off + tmp_thread_local_free_bit_map_size; |
| 1812 | // Align up the unaligned header size. bracket_size may not be a power of two. |
| 1813 | size_t tmp_header_size = (tmp_unaligned_header_size % bracket_size == 0) ? |
| 1814 | tmp_unaligned_header_size : |
| 1815 | tmp_unaligned_header_size + (bracket_size - tmp_unaligned_header_size % bracket_size); |
| 1816 | DCHECK_EQ(tmp_header_size % bracket_size, static_cast<size_t>(0)); |
| 1817 | DCHECK_EQ(tmp_header_size % 8, static_cast<size_t>(0)); |
| 1818 | if (tmp_slots_size + tmp_header_size <= run_size) { |
| 1819 | // Found the right number of slots, that is, there was enough |
| 1820 | // space for the header (including the bit maps.) |
| 1821 | num_of_slots = s; |
| 1822 | header_size = tmp_header_size; |
| 1823 | bulk_free_bit_map_offset = tmp_bulk_free_bit_map_off; |
| 1824 | thread_local_free_bit_map_offset = tmp_thread_local_free_bit_map_off; |
| 1825 | break; |
| 1826 | } |
| 1827 | } |
| 1828 | DCHECK(num_of_slots > 0 && header_size > 0 && bulk_free_bit_map_offset > 0); |
| 1829 | // Add the padding for the alignment remainder. |
| 1830 | header_size += run_size % bracket_size; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1831 | DCHECK_EQ(header_size + num_of_slots * bracket_size, run_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1832 | numOfSlots[i] = num_of_slots; |
| 1833 | headerSizes[i] = header_size; |
| 1834 | bulkFreeBitMapOffsets[i] = bulk_free_bit_map_offset; |
| 1835 | threadLocalFreeBitMapOffsets[i] = thread_local_free_bit_map_offset; |
| 1836 | if (kTraceRosAlloc) { |
| 1837 | LOG(INFO) << "numOfSlots[" << i << "]=" << numOfSlots[i] |
| 1838 | << ", headerSizes[" << i << "]=" << headerSizes[i] |
| 1839 | << ", bulkFreeBitMapOffsets[" << i << "]=" << bulkFreeBitMapOffsets[i] |
| 1840 | << ", threadLocalFreeBitMapOffsets[" << i << "]=" << threadLocalFreeBitMapOffsets[i];; |
| 1841 | } |
| 1842 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1843 | // Fill the alloc bitmap so nobody can successfully allocate from it. |
| 1844 | if (kIsDebugBuild) { |
| 1845 | dedicated_full_run_->magic_num_ = kMagicNum; |
| 1846 | } |
| 1847 | // It doesn't matter which size bracket we use since the main goal is to have the allocation |
| 1848 | // fail 100% of the time you attempt to allocate into the dedicated full run. |
| 1849 | dedicated_full_run_->size_bracket_idx_ = 0; |
| 1850 | dedicated_full_run_->FillAllocBitMap(); |
| 1851 | dedicated_full_run_->SetIsThreadLocal(true); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | void RosAlloc::BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 1855 | if (used_bytes == 0) { |
| 1856 | return; |
| 1857 | } |
| 1858 | size_t* bytes_allocated = reinterpret_cast<size_t*>(arg); |
| 1859 | *bytes_allocated += used_bytes; |
| 1860 | } |
| 1861 | |
| 1862 | void RosAlloc::ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 1863 | if (used_bytes == 0) { |
| 1864 | return; |
| 1865 | } |
| 1866 | size_t* objects_allocated = reinterpret_cast<size_t*>(arg); |
| 1867 | ++(*objects_allocated); |
| 1868 | } |
| 1869 | |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1870 | void RosAlloc::Verify() { |
| 1871 | Thread* self = Thread::Current(); |
| 1872 | CHECK(Locks::mutator_lock_->IsExclusiveHeld(self)) |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1873 | << "The mutator locks isn't exclusively locked at " << __PRETTY_FUNCTION__; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1874 | MutexLock mu(self, *Locks::thread_list_lock_); |
Mathieu Chartier | a1c1c71 | 2014-06-23 17:53:09 -0700 | [diff] [blame] | 1875 | ReaderMutexLock wmu(self, bulk_free_lock_); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1876 | std::vector<Run*> runs; |
| 1877 | { |
| 1878 | MutexLock mu(self, lock_); |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1879 | size_t pm_end = page_map_size_; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1880 | size_t i = 0; |
| 1881 | while (i < pm_end) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1882 | uint8_t pm = page_map_[i]; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1883 | switch (pm) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1884 | case kPageMapReleased: |
| 1885 | // Fall-through. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1886 | case kPageMapEmpty: { |
| 1887 | // The start of a free page run. |
| 1888 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1889 | DCHECK_EQ(fpr->magic_num_, kMagicNumFree); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1890 | CHECK(free_page_runs_.find(fpr) != free_page_runs_.end()) |
| 1891 | << "An empty page must belong to the free page run set"; |
| 1892 | size_t fpr_size = fpr->ByteSize(this); |
| 1893 | CHECK(IsAligned<kPageSize>(fpr_size)) |
| 1894 | << "A free page run size isn't page-aligned : " << fpr_size; |
| 1895 | size_t num_pages = fpr_size / kPageSize; |
| 1896 | CHECK_GT(num_pages, static_cast<uintptr_t>(0)) |
| 1897 | << "A free page run size must be > 0 : " << fpr_size; |
| 1898 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1899 | CHECK(IsFreePage(j)) |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1900 | << "A mismatch between the page map table for kPageMapEmpty " |
| 1901 | << " at page index " << j |
| 1902 | << " and the free page run size : page index range : " |
| 1903 | << i << " to " << (i + num_pages) << std::endl << DumpPageMap(); |
| 1904 | } |
| 1905 | i += num_pages; |
| 1906 | CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end |
| 1907 | << std::endl << DumpPageMap(); |
| 1908 | break; |
| 1909 | } |
| 1910 | case kPageMapLargeObject: { |
| 1911 | // The start of a large object. |
| 1912 | size_t num_pages = 1; |
| 1913 | size_t idx = i + 1; |
| 1914 | while (idx < pm_end && page_map_[idx] == kPageMapLargeObjectPart) { |
| 1915 | num_pages++; |
| 1916 | idx++; |
| 1917 | } |
| 1918 | void* start = base_ + i * kPageSize; |
| 1919 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(start); |
| 1920 | size_t obj_size = obj->SizeOf(); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1921 | CHECK_GT(obj_size, kLargeSizeThreshold) |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1922 | << "A rosalloc large object size must be > " << kLargeSizeThreshold; |
| 1923 | CHECK_EQ(num_pages, RoundUp(obj_size, kPageSize) / kPageSize) |
| 1924 | << "A rosalloc large object size " << obj_size |
| 1925 | << " does not match the page map table " << (num_pages * kPageSize) |
| 1926 | << std::endl << DumpPageMap(); |
| 1927 | i += num_pages; |
| 1928 | CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end |
| 1929 | << std::endl << DumpPageMap(); |
| 1930 | break; |
| 1931 | } |
| 1932 | case kPageMapLargeObjectPart: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1933 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl << DumpPageMap(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1934 | break; |
| 1935 | case kPageMapRun: { |
| 1936 | // The start of a run. |
| 1937 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1938 | DCHECK_EQ(run->magic_num_, kMagicNum); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1939 | size_t idx = run->size_bracket_idx_; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1940 | CHECK_LT(idx, kNumOfSizeBrackets) << "Out of range size bracket index : " << idx; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1941 | size_t num_pages = numOfPages[idx]; |
| 1942 | CHECK_GT(num_pages, static_cast<uintptr_t>(0)) |
| 1943 | << "Run size must be > 0 : " << num_pages; |
| 1944 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| 1945 | CHECK_EQ(page_map_[j], kPageMapRunPart) |
| 1946 | << "A mismatch between the page map table for kPageMapRunPart " |
| 1947 | << " at page index " << j |
| 1948 | << " and the run size : page index range " << i << " to " << (i + num_pages) |
| 1949 | << std::endl << DumpPageMap(); |
| 1950 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1951 | // Don't verify the dedicated_full_run_ since it doesn't have any real allocations. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1952 | runs.push_back(run); |
| 1953 | i += num_pages; |
| 1954 | CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end |
| 1955 | << std::endl << DumpPageMap(); |
| 1956 | break; |
| 1957 | } |
| 1958 | case kPageMapRunPart: |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1959 | // Fall-through. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1960 | default: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1961 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl << DumpPageMap(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1962 | break; |
| 1963 | } |
| 1964 | } |
| 1965 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1966 | std::list<Thread*> threads = Runtime::Current()->GetThreadList()->GetList(); |
| 1967 | for (Thread* thread : threads) { |
| 1968 | for (size_t i = 0; i < kNumThreadLocalSizeBrackets; ++i) { |
| 1969 | MutexLock mu(self, *size_bracket_locks_[i]); |
| 1970 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(i)); |
| 1971 | CHECK(thread_local_run != nullptr); |
| 1972 | CHECK(thread_local_run->IsThreadLocal()); |
| 1973 | CHECK(thread_local_run == dedicated_full_run_ || |
| 1974 | thread_local_run->size_bracket_idx_ == i); |
| 1975 | } |
| 1976 | } |
| 1977 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 1978 | MutexLock mu(self, *size_bracket_locks_[i]); |
| 1979 | Run* current_run = current_runs_[i]; |
| 1980 | CHECK(current_run != nullptr); |
| 1981 | if (current_run != dedicated_full_run_) { |
| 1982 | // The dedicated full run is currently marked as thread local. |
| 1983 | CHECK(!current_run->IsThreadLocal()); |
| 1984 | CHECK_EQ(current_run->size_bracket_idx_, i); |
| 1985 | } |
| 1986 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1987 | // Call Verify() here for the lock order. |
| 1988 | for (auto& run : runs) { |
| 1989 | run->Verify(self, this); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | void RosAlloc::Run::Verify(Thread* self, RosAlloc* rosalloc) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1994 | DCHECK_EQ(magic_num_, kMagicNum) << "Bad magic number : " << Dump(); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1995 | const size_t idx = size_bracket_idx_; |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1996 | CHECK_LT(idx, kNumOfSizeBrackets) << "Out of range size bracket index : " << Dump(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1997 | uint8_t* slot_base = reinterpret_cast<uint8_t*>(this) + headerSizes[idx]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1998 | const size_t num_slots = numOfSlots[idx]; |
| 1999 | const size_t num_vec = RoundUp(num_slots, 32) / 32; |
| 2000 | CHECK_GT(num_vec, 0U); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2001 | size_t bracket_size = IndexToBracketSize(idx); |
| 2002 | CHECK_EQ(slot_base + num_slots * bracket_size, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2003 | reinterpret_cast<uint8_t*>(this) + numOfPages[idx] * kPageSize) |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2004 | << "Mismatch in the end address of the run " << Dump(); |
| 2005 | // Check that the bulk free bitmap is clean. It's only used during BulkFree(). |
| 2006 | CHECK(IsBulkFreeBitmapClean()) << "The bulk free bit map isn't clean " << Dump(); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 2007 | uint32_t last_word_mask = GetBitmapLastVectorMask(num_slots, num_vec); |
| 2008 | // Make sure all the bits at the end of the run are set so that we don't allocate there. |
| 2009 | CHECK_EQ(alloc_bit_map_[num_vec - 1] & last_word_mask, last_word_mask); |
| 2010 | // Ensure that the first bitmap index is valid. |
| 2011 | CHECK_LT(first_search_vec_idx_, num_vec); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2012 | // Check the thread local runs, the current runs, and the run sets. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 2013 | if (IsThreadLocal()) { |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2014 | // If it's a thread local run, then it must be pointed to by an owner thread. |
| 2015 | bool owner_found = false; |
| 2016 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 2017 | for (auto it = thread_list.begin(); it != thread_list.end(); ++it) { |
| 2018 | Thread* thread = *it; |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 2019 | for (size_t i = 0; i < kNumThreadLocalSizeBrackets; i++) { |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2020 | MutexLock mu(self, *rosalloc->size_bracket_locks_[i]); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 2021 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(i)); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2022 | if (thread_local_run == this) { |
| 2023 | CHECK(!owner_found) |
| 2024 | << "A thread local run has more than one owner thread " << Dump(); |
| 2025 | CHECK_EQ(i, idx) |
| 2026 | << "A mismatching size bracket index in a thread local run " << Dump(); |
| 2027 | owner_found = true; |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | CHECK(owner_found) << "A thread local run has no owner thread " << Dump(); |
| 2032 | } else { |
| 2033 | // If it's not thread local, check that the thread local free bitmap is clean. |
| 2034 | CHECK(IsThreadLocalFreeBitmapClean()) |
| 2035 | << "A non-thread-local run's thread local free bitmap isn't clean " |
| 2036 | << Dump(); |
| 2037 | // Check if it's a current run for the size bucket. |
| 2038 | bool is_current_run = false; |
| 2039 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 2040 | MutexLock mu(self, *rosalloc->size_bracket_locks_[i]); |
| 2041 | Run* current_run = rosalloc->current_runs_[i]; |
| 2042 | if (idx == i) { |
| 2043 | if (this == current_run) { |
| 2044 | is_current_run = true; |
| 2045 | } |
| 2046 | } else { |
| 2047 | // If the size bucket index does not match, then it must not |
| 2048 | // be a current run. |
| 2049 | CHECK_NE(this, current_run) |
| 2050 | << "A current run points to a run with a wrong size bracket index " << Dump(); |
| 2051 | } |
| 2052 | } |
| 2053 | // If it's neither a thread local or current run, then it must be |
| 2054 | // in a run set. |
| 2055 | if (!is_current_run) { |
| 2056 | MutexLock mu(self, rosalloc->lock_); |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 2057 | auto& non_full_runs = rosalloc->non_full_runs_[idx]; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2058 | // If it's all free, it must be a free page run rather than a run. |
| 2059 | CHECK(!IsAllFree()) << "A free run must be in a free page run set " << Dump(); |
| 2060 | if (!IsFull()) { |
| 2061 | // If it's not full, it must in the non-full run set. |
| 2062 | CHECK(non_full_runs.find(this) != non_full_runs.end()) |
| 2063 | << "A non-full run isn't in the non-full run set " << Dump(); |
| 2064 | } else { |
| 2065 | // If it's full, it must in the full run set (debug build only.) |
| 2066 | if (kIsDebugBuild) { |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 2067 | auto& full_runs = rosalloc->full_runs_[idx]; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2068 | CHECK(full_runs.find(this) != full_runs.end()) |
| 2069 | << " A full run isn't in the full run set " << Dump(); |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | // Check each slot. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2075 | size_t slots = 0; |
| 2076 | for (size_t v = 0; v < num_vec; v++, slots += 32) { |
Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 2077 | DCHECK_GE(num_slots, slots) << "Out of bounds"; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2078 | uint32_t vec = alloc_bit_map_[v]; |
| 2079 | uint32_t thread_local_free_vec = ThreadLocalFreeBitMap()[v]; |
| 2080 | size_t end = std::min(num_slots - slots, static_cast<size_t>(32)); |
| 2081 | for (size_t i = 0; i < end; ++i) { |
| 2082 | bool is_allocated = ((vec >> i) & 0x1) != 0; |
| 2083 | // If a thread local run, slots may be marked freed in the |
| 2084 | // thread local free bitmap. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 2085 | bool is_thread_local_freed = IsThreadLocal() && ((thread_local_free_vec >> i) & 0x1) != 0; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2086 | if (is_allocated && !is_thread_local_freed) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2087 | uint8_t* slot_addr = slot_base + (slots + i) * bracket_size; |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2088 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(slot_addr); |
| 2089 | size_t obj_size = obj->SizeOf(); |
| 2090 | CHECK_LE(obj_size, kLargeSizeThreshold) |
| 2091 | << "A run slot contains a large object " << Dump(); |
| 2092 | CHECK_EQ(SizeToIndex(obj_size), idx) |
Hiroshi Yamauchi | 4cd662e | 2014-04-03 16:28:10 -0700 | [diff] [blame] | 2093 | << PrettyTypeOf(obj) << " " |
| 2094 | << "obj_size=" << obj_size << ", idx=" << idx << " " |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 2095 | << "A run slot contains an object with wrong size " << Dump(); |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | } |
| 2100 | |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2101 | size_t RosAlloc::ReleasePages() { |
| 2102 | VLOG(heap) << "RosAlloc::ReleasePages()"; |
| 2103 | DCHECK(!DoesReleaseAllPages()); |
| 2104 | Thread* self = Thread::Current(); |
| 2105 | size_t reclaimed_bytes = 0; |
| 2106 | size_t i = 0; |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2107 | // Check the page map size which might have changed due to grow/shrink. |
| 2108 | while (i < page_map_size_) { |
| 2109 | // Reading the page map without a lock is racy but the race is benign since it should only |
| 2110 | // result in occasionally not releasing pages which we could release. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2111 | uint8_t pm = page_map_[i]; |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2112 | switch (pm) { |
Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2113 | case kPageMapReleased: |
| 2114 | // Fall through. |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2115 | case kPageMapEmpty: { |
Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2116 | // This is currently the start of a free page run. |
| 2117 | // Acquire the lock to prevent other threads racing in and modifying the page map. |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2118 | MutexLock mu(self, lock_); |
| 2119 | // Check that it's still empty after we acquired the lock since another thread could have |
| 2120 | // raced in and placed an allocation here. |
Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2121 | if (IsFreePage(i)) { |
| 2122 | // Free page runs can start with a released page if we coalesced a released page free |
| 2123 | // page run with an empty page run. |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2124 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2125 | // There is a race condition where FreePage can coalesce fpr with the previous |
| 2126 | // free page run before we acquire lock_. In that case free_page_runs_.find will not find |
| 2127 | // a run starting at fpr. To handle this race, we skip reclaiming the page range and go |
| 2128 | // to the next page. |
| 2129 | if (free_page_runs_.find(fpr) != free_page_runs_.end()) { |
| 2130 | size_t fpr_size = fpr->ByteSize(this); |
| 2131 | DCHECK(IsAligned<kPageSize>(fpr_size)); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2132 | uint8_t* start = reinterpret_cast<uint8_t*>(fpr); |
Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2133 | reclaimed_bytes += ReleasePageRange(start, start + fpr_size); |
| 2134 | size_t pages = fpr_size / kPageSize; |
| 2135 | CHECK_GT(pages, 0U) << "Infinite loop probable"; |
| 2136 | i += pages; |
| 2137 | DCHECK_LE(i, page_map_size_); |
| 2138 | break; |
| 2139 | } |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2140 | } |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 2141 | FALLTHROUGH_INTENDED; |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2142 | } |
| 2143 | case kPageMapLargeObject: // Fall through. |
| 2144 | case kPageMapLargeObjectPart: // Fall through. |
| 2145 | case kPageMapRun: // Fall through. |
| 2146 | case kPageMapRunPart: // Fall through. |
| 2147 | ++i; |
| 2148 | break; // Skip. |
| 2149 | default: |
Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 2150 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2151 | break; |
| 2152 | } |
| 2153 | } |
| 2154 | return reclaimed_bytes; |
| 2155 | } |
| 2156 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2157 | size_t RosAlloc::ReleasePageRange(uint8_t* start, uint8_t* end) { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2158 | DCHECK_ALIGNED(start, kPageSize); |
| 2159 | DCHECK_ALIGNED(end, kPageSize); |
| 2160 | DCHECK_LT(start, end); |
| 2161 | if (kIsDebugBuild) { |
| 2162 | // In the debug build, the first page of a free page run |
| 2163 | // contains a magic number for debugging. Exclude it. |
| 2164 | start += kPageSize; |
| 2165 | } |
| 2166 | if (!kMadviseZeroes) { |
| 2167 | // TODO: Do this when we resurrect the page instead. |
| 2168 | memset(start, 0, end - start); |
| 2169 | } |
| 2170 | CHECK_EQ(madvise(start, end - start, MADV_DONTNEED), 0); |
| 2171 | size_t pm_idx = ToPageMapIndex(start); |
| 2172 | size_t reclaimed_bytes = 0; |
| 2173 | // Calculate reclaimed bytes and upate page map. |
| 2174 | const size_t max_idx = pm_idx + (end - start) / kPageSize; |
| 2175 | for (; pm_idx < max_idx; ++pm_idx) { |
| 2176 | DCHECK(IsFreePage(pm_idx)); |
| 2177 | if (page_map_[pm_idx] == kPageMapEmpty) { |
| 2178 | // Mark the page as released and update how many bytes we released. |
| 2179 | reclaimed_bytes += kPageSize; |
| 2180 | page_map_[pm_idx] = kPageMapReleased; |
| 2181 | } |
| 2182 | } |
| 2183 | return reclaimed_bytes; |
| 2184 | } |
| 2185 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2186 | void RosAlloc::LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) { |
| 2187 | Thread* self = Thread::Current(); |
| 2188 | size_t largest_continuous_free_pages = 0; |
| 2189 | WriterMutexLock wmu(self, bulk_free_lock_); |
| 2190 | MutexLock mu(self, lock_); |
| 2191 | for (FreePageRun* fpr : free_page_runs_) { |
| 2192 | largest_continuous_free_pages = std::max(largest_continuous_free_pages, |
| 2193 | fpr->ByteSize(this)); |
| 2194 | } |
| 2195 | if (failed_alloc_bytes > kLargeSizeThreshold) { |
| 2196 | // Large allocation. |
| 2197 | size_t required_bytes = RoundUp(failed_alloc_bytes, kPageSize); |
| 2198 | if (required_bytes > largest_continuous_free_pages) { |
| 2199 | os << "; failed due to fragmentation (required continguous free " |
| 2200 | << required_bytes << " bytes where largest contiguous free " |
| 2201 | << largest_continuous_free_pages << " bytes)"; |
| 2202 | } |
| 2203 | } else { |
| 2204 | // Non-large allocation. |
| 2205 | size_t required_bytes = numOfPages[SizeToIndex(failed_alloc_bytes)] * kPageSize; |
| 2206 | if (required_bytes > largest_continuous_free_pages) { |
| 2207 | os << "; failed due to fragmentation (required continguous free " |
| 2208 | << required_bytes << " bytes for a new buffer where largest contiguous free " |
| 2209 | << largest_continuous_free_pages << " bytes)"; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 2214 | } // namespace allocator |
| 2215 | } // namespace gc |
| 2216 | } // namespace art |