| 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 "malloc_space.h" |
| 18 | |
| Andreas Gampe | 8764dc3 | 2019-01-07 15:20:12 -0800 | [diff] [blame] | 19 | #include <ostream> |
| 20 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
| Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 23 | #include "base/logging.h" // For VLOG |
| Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 24 | #include "base/mutex-inl.h" |
| David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 25 | #include "base/utils.h" |
| Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 26 | #include "gc/accounting/card_table-inl.h" |
| 27 | #include "gc/accounting/space_bitmap-inl.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 28 | #include "gc/heap.h" |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 29 | #include "gc/space/space-inl.h" |
| 30 | #include "gc/space/zygote_space.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 31 | #include "handle_scope-inl.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 32 | #include "mirror/class-inl.h" |
| 33 | #include "mirror/object-inl.h" |
| 34 | #include "runtime.h" |
| 35 | #include "thread.h" |
| 36 | #include "thread_list.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | namespace gc { |
| 40 | namespace space { |
| 41 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 44 | size_t MallocSpace::bitmap_index_ = 0; |
| 45 | |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 46 | MallocSpace::MallocSpace(const std::string& name, |
| 47 | MemMap&& mem_map, |
| 48 | uint8_t* begin, |
| 49 | uint8_t* end, |
| 50 | uint8_t* limit, |
| 51 | size_t growth_limit, |
| 52 | bool create_bitmaps, |
| 53 | bool can_move_objects, |
| 54 | size_t starting_size, |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 55 | size_t initial_size) |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 56 | : ContinuousMemMapAllocSpace( |
| 57 | name, std::move(mem_map), begin, end, limit, kGcRetentionPolicyAlwaysCollect), |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 58 | recent_free_pos_(0), lock_("allocation space lock", kAllocSpaceLock), |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 59 | growth_limit_(growth_limit), can_move_objects_(can_move_objects), |
| 60 | starting_size_(starting_size), initial_size_(initial_size) { |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 61 | if (create_bitmaps) { |
| 62 | size_t bitmap_index = bitmap_index_++; |
| 63 | static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize); |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 64 | CHECK_ALIGNED(reinterpret_cast<uintptr_t>(mem_map_.Begin()), kGcCardSize); |
| 65 | CHECK_ALIGNED(reinterpret_cast<uintptr_t>(mem_map_.End()), kGcCardSize); |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 66 | live_bitmap_ = accounting::ContinuousSpaceBitmap::Create( |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 67 | StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)), |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 68 | Begin(), NonGrowthLimitCapacity()); |
| 69 | CHECK(live_bitmap_.IsValid()) << "could not create allocspace live bitmap #" |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 70 | << bitmap_index; |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 71 | mark_bitmap_ = accounting::ContinuousSpaceBitmap::Create( |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 72 | StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)), |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 73 | Begin(), NonGrowthLimitCapacity()); |
| 74 | CHECK(mark_bitmap_.IsValid()) << "could not create allocspace mark bitmap #" << bitmap_index; |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 75 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 76 | for (auto& freed : recent_freed_objects_) { |
| 77 | freed.first = nullptr; |
| 78 | freed.second = nullptr; |
| 79 | } |
| 80 | } |
| 81 | |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 82 | MemMap MallocSpace::CreateMemMap(const std::string& name, |
| 83 | size_t starting_size, |
| 84 | size_t* initial_size, |
| 85 | size_t* growth_limit, |
| Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 86 | size_t* capacity) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 87 | // Sanity check arguments |
| 88 | if (starting_size > *initial_size) { |
| 89 | *initial_size = starting_size; |
| 90 | } |
| 91 | if (*initial_size > *growth_limit) { |
| 92 | LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size (" |
| 93 | << PrettySize(*initial_size) << ") is larger than its capacity (" |
| 94 | << PrettySize(*growth_limit) << ")"; |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 95 | return MemMap::Invalid(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 96 | } |
| 97 | if (*growth_limit > *capacity) { |
| 98 | LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity (" |
| 99 | << PrettySize(*growth_limit) << ") is larger than the capacity (" |
| 100 | << PrettySize(*capacity) << ")"; |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 101 | return MemMap::Invalid(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Page align growth limit and capacity which will be used to manage mmapped storage |
| 105 | *growth_limit = RoundUp(*growth_limit, kPageSize); |
| 106 | *capacity = RoundUp(*capacity, kPageSize); |
| 107 | |
| 108 | std::string error_msg; |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 109 | MemMap mem_map = MemMap::MapAnonymous(name.c_str(), |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 110 | *capacity, |
| 111 | PROT_READ | PROT_WRITE, |
| Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 112 | /*low_4gb=*/ true, |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 113 | &error_msg); |
| 114 | if (!mem_map.IsValid()) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 115 | LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size " |
| 116 | << PrettySize(*capacity) << ": " << error_msg; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 117 | } |
| 118 | return mem_map; |
| 119 | } |
| 120 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 121 | mirror::Class* MallocSpace::FindRecentFreedObject(const mirror::Object* obj) { |
| 122 | size_t pos = recent_free_pos_; |
| 123 | // Start at the most recently freed object and work our way back since there may be duplicates |
| 124 | // caused by dlmalloc reusing memory. |
| 125 | if (kRecentFreeCount > 0) { |
| 126 | for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) { |
| 127 | pos = pos != 0 ? pos - 1 : kRecentFreeMask; |
| 128 | if (recent_freed_objects_[pos].first == obj) { |
| 129 | return recent_freed_objects_[pos].second; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
| 136 | void MallocSpace::RegisterRecentFree(mirror::Object* ptr) { |
| Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 137 | // No verification since the object is dead. |
| 138 | recent_freed_objects_[recent_free_pos_] = std::make_pair(ptr, ptr->GetClass<kVerifyNone>()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 139 | recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask; |
| 140 | } |
| 141 | |
| 142 | void MallocSpace::SetGrowthLimit(size_t growth_limit) { |
| 143 | growth_limit = RoundUp(growth_limit, kPageSize); |
| 144 | growth_limit_ = growth_limit; |
| 145 | if (Size() > growth_limit_) { |
| Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 146 | SetEnd(begin_ + growth_limit); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void* MallocSpace::MoreCore(intptr_t increment) { |
| 151 | CheckMoreCoreForPrecondition(); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 152 | uint8_t* original_end = End(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 153 | if (increment != 0) { |
| 154 | VLOG(heap) << "MallocSpace::MoreCore " << PrettySize(increment); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 155 | uint8_t* new_end = original_end + increment; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 156 | if (increment > 0) { |
| 157 | // Should never be asked to increase the allocation beyond the capacity of the space. Enforced |
| 158 | // by mspace_set_footprint_limit. |
| 159 | CHECK_LE(new_end, Begin() + Capacity()); |
| Mathieu Chartier | 3425d02 | 2017-10-03 16:22:05 -0700 | [diff] [blame] | 160 | CheckedCall(mprotect, GetName(), original_end, increment, PROT_READ | PROT_WRITE); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 161 | } else { |
| 162 | // Should never be asked for negative footprint (ie before begin). Zero footprint is ok. |
| 163 | CHECK_GE(original_end + increment, Begin()); |
| 164 | // Advise we don't need the pages and protect them |
| 165 | // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be |
| 166 | // expensive (note the same isn't true for giving permissions to a page as the protected |
| 167 | // page shouldn't be in a TLB). We should investigate performance impact of just |
| 168 | // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's |
| 169 | // likely just a useful debug feature. |
| 170 | size_t size = -increment; |
| Mathieu Chartier | 3425d02 | 2017-10-03 16:22:05 -0700 | [diff] [blame] | 171 | CheckedCall(madvise, GetName(), new_end, size, MADV_DONTNEED); |
| 172 | CheckedCall(mprotect, GetName(), new_end, size, PROT_NONE); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 173 | } |
| Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 174 | // Update end_. |
| 175 | SetEnd(new_end); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 176 | } |
| 177 | return original_end; |
| 178 | } |
| 179 | |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 180 | ZygoteSpace* MallocSpace::CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode, |
| 181 | MallocSpace** out_malloc_space) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 182 | // For RosAlloc, revoke thread local runs before creating a new |
| 183 | // alloc space so that we won't mix thread local runs from different |
| 184 | // alloc spaces. |
| 185 | RevokeAllThreadLocalBuffers(); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 186 | SetEnd(reinterpret_cast<uint8_t*>(RoundUp(reinterpret_cast<uintptr_t>(End()), kPageSize))); |
| Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 187 | DCHECK_ALIGNED(begin_, accounting::CardTable::kCardSize); |
| 188 | DCHECK_ALIGNED(End(), accounting::CardTable::kCardSize); |
| 189 | DCHECK_ALIGNED(begin_, kPageSize); |
| 190 | DCHECK_ALIGNED(End(), kPageSize); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 191 | size_t size = RoundUp(Size(), kPageSize); |
| Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 192 | // Trimming the heap should be done by the caller since we may have invalidated the accounting |
| 193 | // stored in between objects. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 194 | // Remaining size is for the new alloc space. |
| 195 | const size_t growth_limit = growth_limit_ - size; |
| Lin Zang | d0e0d4c | 2014-12-12 21:54:47 +0800 | [diff] [blame] | 196 | // Use mem map limit in case error for clear growth limit. |
| 197 | const size_t capacity = NonGrowthLimitCapacity() - size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 198 | VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n" |
| Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 199 | << "End " << reinterpret_cast<const void*>(End()) << "\n" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 200 | << "Size " << size << "\n" |
| 201 | << "GrowthLimit " << growth_limit_ << "\n" |
| 202 | << "Capacity " << Capacity(); |
| 203 | SetGrowthLimit(RoundUp(size, kPageSize)); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 204 | // FIXME: Do we need reference counted pointers here? |
| 205 | // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces. |
| 206 | VLOG(heap) << "Creating new AllocSpace: "; |
| 207 | VLOG(heap) << "Size " << GetMemMap()->Size(); |
| 208 | VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit); |
| 209 | VLOG(heap) << "Capacity " << PrettySize(capacity); |
| Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 210 | // Remap the tail. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 211 | std::string error_msg; |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 212 | MemMap mem_map = GetMemMap()->RemapAtEnd( |
| 213 | End(), alloc_space_name, PROT_READ | PROT_WRITE, &error_msg); |
| 214 | CHECK(mem_map.IsValid()) << error_msg; |
| 215 | void* allocator = |
| 216 | CreateAllocator(End(), starting_size_, initial_size_, capacity, low_memory_mode); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 217 | // Protect memory beyond the initial size. |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 218 | uint8_t* end = mem_map.Begin() + starting_size_; |
| Mathieu Chartier | c4d095b | 2014-04-15 12:01:58 -0700 | [diff] [blame] | 219 | if (capacity > initial_size_) { |
| Mathieu Chartier | 3425d02 | 2017-10-03 16:22:05 -0700 | [diff] [blame] | 220 | CheckedCall(mprotect, alloc_space_name, end, capacity - initial_size_, PROT_NONE); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 221 | } |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 222 | *out_malloc_space = CreateInstance(std::move(mem_map), |
| 223 | alloc_space_name, |
| 224 | allocator, |
| 225 | End(), |
| 226 | end, |
| 227 | limit_, |
| 228 | growth_limit, |
| 229 | CanMoveObjects()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 230 | SetLimit(End()); |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 231 | live_bitmap_.SetHeapLimit(reinterpret_cast<uintptr_t>(End())); |
| 232 | CHECK_EQ(live_bitmap_.HeapLimit(), reinterpret_cast<uintptr_t>(End())); |
| 233 | mark_bitmap_.SetHeapLimit(reinterpret_cast<uintptr_t>(End())); |
| 234 | CHECK_EQ(mark_bitmap_.HeapLimit(), reinterpret_cast<uintptr_t>(End())); |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 235 | |
| 236 | // Create the actual zygote space. |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 237 | ZygoteSpace* zygote_space = ZygoteSpace::Create("Zygote space", |
| 238 | ReleaseMemMap(), |
| 239 | std::move(live_bitmap_), |
| 240 | std::move(mark_bitmap_)); |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 241 | if (UNLIKELY(zygote_space == nullptr)) { |
| 242 | VLOG(heap) << "Failed creating zygote space from space " << GetName(); |
| 243 | } else { |
| 244 | VLOG(heap) << "zygote space creation done"; |
| 245 | } |
| 246 | return zygote_space; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void MallocSpace::Dump(std::ostream& os) const { |
| 250 | os << GetType() |
| Hiroshi Yamauchi | 447a914 | 2014-05-23 21:27:30 -0700 | [diff] [blame] | 251 | << " begin=" << reinterpret_cast<void*>(Begin()) |
| 252 | << ",end=" << reinterpret_cast<void*>(End()) |
| 253 | << ",limit=" << reinterpret_cast<void*>(Limit()) |
| 254 | << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity()) |
| 255 | << ",non_growth_limit_capacity=" << PrettySize(NonGrowthLimitCapacity()) |
| 256 | << ",name=\"" << GetName() << "\"]"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 259 | void MallocSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) { |
| Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 260 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
| Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 261 | space::MallocSpace* space = context->space->AsMallocSpace(); |
| Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 262 | Thread* self = context->self; |
| 263 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
| 264 | // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap |
| 265 | // the bitmaps as an optimization. |
| 266 | if (!context->swap_bitmaps) { |
| Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 267 | accounting::ContinuousSpaceBitmap* bitmap = space->GetLiveBitmap(); |
| Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 268 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 269 | bitmap->Clear(ptrs[i]); |
| 270 | } |
| 271 | } |
| 272 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| Roland Levillain | ef07132 | 2018-04-17 14:16:49 +0100 | [diff] [blame] | 273 | // Documentation suggests better free performance with merging, but this may be at the expense |
| Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 274 | // of allocation. |
| Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 275 | context->freed.objects += num_ptrs; |
| 276 | context->freed.bytes += space->FreeList(self, num_ptrs, ptrs); |
| Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 279 | void MallocSpace::ClampGrowthLimit() { |
| 280 | size_t new_capacity = Capacity(); |
| 281 | CHECK_LE(new_capacity, NonGrowthLimitCapacity()); |
| 282 | GetLiveBitmap()->SetHeapSize(new_capacity); |
| 283 | GetMarkBitmap()->SetHeapSize(new_capacity); |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 284 | if (temp_bitmap_.IsValid()) { |
| Mathieu Chartier | ddac423 | 2015-04-02 10:08:03 -0700 | [diff] [blame] | 285 | // If the bitmaps are clamped, then the temp bitmap is actually the mark bitmap. |
| Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 286 | temp_bitmap_.SetHeapSize(new_capacity); |
| Mathieu Chartier | ddac423 | 2015-04-02 10:08:03 -0700 | [diff] [blame] | 287 | } |
| Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 288 | GetMemMap()->SetSize(new_capacity); |
| 289 | limit_ = Begin() + new_capacity; |
| Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 292 | } // namespace space |
| 293 | } // namespace gc |
| 294 | } // namespace art |