blob: e70fe215aba132b4963926b12d357b96f4d4deb8 [file] [log] [blame]
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001/*
2 * Copyright (C) 2012 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
Ian Rogers1d54e732013-05-02 21:10:01 -070017#include "large_object_space.h"
18
Mathieu Chartier3cf22532015-07-09 15:15:09 -070019#include <valgrind.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Mathieu Chartier3cf22532015-07-09 15:15:09 -070021#include <memcheck/memcheck.h>
Ian Rogers700a4022014-05-19 16:49:03 -070022
Mathieu Chartierc8980de2015-04-19 13:36:11 -070023#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070024#include "gc/accounting/space_bitmap-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070026#include "base/mutex-inl.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070028#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "os.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070030#include "space-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080031#include "thread-inl.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070032
33namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070034namespace gc {
35namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070036
Evgenii Stepanov1e133742015-05-20 12:30:59 -070037class MemoryToolLargeObjectMapSpace FINAL : public LargeObjectMapSpace {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070038 public:
Evgenii Stepanov1e133742015-05-20 12:30:59 -070039 explicit MemoryToolLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070040 }
41
Evgenii Stepanov1e133742015-05-20 12:30:59 -070042 ~MemoryToolLargeObjectMapSpace() OVERRIDE {
Mathieu Chartier9086b652015-04-14 09:35:18 -070043 // Keep valgrind happy if there is any large objects such as dex cache arrays which aren't
44 // freed since they are held live by the class linker.
45 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -070046 for (auto& m : large_objects_) {
47 delete m.second.mem_map;
Mathieu Chartier9086b652015-04-14 09:35:18 -070048 }
49 }
50
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070051 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
52 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070053 OVERRIDE {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070054 mirror::Object* obj =
Evgenii Stepanov1e133742015-05-20 12:30:59 -070055 LargeObjectMapSpace::Alloc(self, num_bytes + kMemoryToolRedZoneBytes * 2, bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070056 usable_size, bytes_tl_bulk_allocated);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070057 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
Evgenii Stepanov1e133742015-05-20 12:30:59 -070058 reinterpret_cast<uintptr_t>(obj) + kMemoryToolRedZoneBytes);
59 MEMORY_TOOL_MAKE_NOACCESS(reinterpret_cast<void*>(obj), kMemoryToolRedZoneBytes);
60 MEMORY_TOOL_MAKE_NOACCESS(
61 reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes,
62 kMemoryToolRedZoneBytes);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070063 if (usable_size != nullptr) {
64 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
65 }
66 return object_without_rdz;
67 }
68
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070069 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
70 return LargeObjectMapSpace::AllocationSize(ObjectWithRedzone(obj), usable_size);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070071 }
72
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070073 bool IsZygoteLargeObject(Thread* self, mirror::Object* obj) const OVERRIDE {
74 return LargeObjectMapSpace::IsZygoteLargeObject(self, ObjectWithRedzone(obj));
75 }
76
77 size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
78 mirror::Object* object_with_rdz = ObjectWithRedzone(obj);
Evgenii Stepanov1e133742015-05-20 12:30:59 -070079 MEMORY_TOOL_MAKE_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070080 return LargeObjectMapSpace::Free(self, object_with_rdz);
81 }
82
83 bool Contains(const mirror::Object* obj) const OVERRIDE {
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070084 return LargeObjectMapSpace::Contains(ObjectWithRedzone(obj));
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070085 }
86
87 private:
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070088 static const mirror::Object* ObjectWithRedzone(const mirror::Object* obj) {
89 return reinterpret_cast<const mirror::Object*>(
Evgenii Stepanov1e133742015-05-20 12:30:59 -070090 reinterpret_cast<uintptr_t>(obj) - kMemoryToolRedZoneBytes);
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070091 }
92
93 static mirror::Object* ObjectWithRedzone(mirror::Object* obj) {
94 return reinterpret_cast<mirror::Object*>(
Evgenii Stepanov1e133742015-05-20 12:30:59 -070095 reinterpret_cast<uintptr_t>(obj) - kMemoryToolRedZoneBytes);
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070096 }
97
Evgenii Stepanov1e133742015-05-20 12:30:59 -070098 static constexpr size_t kMemoryToolRedZoneBytes = kPageSize;
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070099};
100
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700102 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700103 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700104 std::string temp_name = live_bitmap_->GetName();
105 live_bitmap_->SetName(mark_bitmap_->GetName());
106 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700107}
108
Ian Rogers13735952014-10-08 12:43:28 -0700109LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700110 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
111 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700112 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700113}
114
115
116void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700117 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118}
119
120LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700121 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700122 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700123
124LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700125 if (Runtime::Current()->IsRunningOnMemoryTool()) {
126 return new MemoryToolLargeObjectMapSpace(name);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700127 } else {
128 return new LargeObjectMapSpace(name);
129 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700130}
131
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700132mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700133 size_t* bytes_allocated, size_t* usable_size,
134 size_t* bytes_tl_bulk_allocated) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700135 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000136 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", nullptr, num_bytes,
137 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 if (UNLIKELY(mem_map == nullptr)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700139 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 return nullptr;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700141 }
Mathieu Chartierc8980de2015-04-19 13:36:11 -0700142 mirror::Object* const obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
143 if (kIsDebugBuild) {
144 ReaderMutexLock mu2(Thread::Current(), *Locks::heap_bitmap_lock_);
145 auto* heap = Runtime::Current()->GetHeap();
146 auto* live_bitmap = heap->GetLiveBitmap();
147 auto* space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj);
148 CHECK(space_bitmap == nullptr) << obj << " overlaps with bitmap " << *space_bitmap;
149 auto* obj_end = reinterpret_cast<mirror::Object*>(mem_map->End());
150 space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj_end - 1);
151 CHECK(space_bitmap == nullptr) << obj_end << " overlaps with bitmap " << *space_bitmap;
152 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700153 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700154 large_objects_.Put(obj, LargeObject {mem_map, false /* not zygote */});
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700155 const size_t allocation_size = mem_map->BaseSize();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700156 DCHECK(bytes_allocated != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700157 begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj));
158 uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700159 if (end_ == nullptr || obj_end > end_) {
160 end_ = obj_end;
161 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700162 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800163 if (usable_size != nullptr) {
164 *usable_size = allocation_size;
165 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700166 DCHECK(bytes_tl_bulk_allocated != nullptr);
167 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700168 num_bytes_allocated_ += allocation_size;
169 total_bytes_allocated_ += allocation_size;
170 ++num_objects_allocated_;
171 ++total_objects_allocated_;
172 return obj;
173}
174
Mathieu Chartiere7158112015-06-03 13:32:15 -0700175bool LargeObjectMapSpace::IsZygoteLargeObject(Thread* self, mirror::Object* obj) const {
176 MutexLock mu(self, lock_);
177 auto it = large_objects_.find(obj);
178 CHECK(it != large_objects_.end());
179 return it->second.is_zygote;
180}
181
182void LargeObjectMapSpace::SetAllLargeObjectsAsZygoteObjects(Thread* self) {
183 MutexLock mu(self, lock_);
184 for (auto& pair : large_objects_) {
185 pair.second.is_zygote = true;
186 }
187}
188
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700190 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700191 auto it = large_objects_.find(ptr);
192 if (UNLIKELY(it == large_objects_.end())) {
193 Runtime::Current()->GetHeap()->DumpSpaces(LOG(INTERNAL_FATAL));
Mathieu Chartierd07a9132014-05-23 16:42:20 -0700194 LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
195 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700196 MemMap* mem_map = it->second.mem_map;
197 const size_t map_size = mem_map->BaseSize();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700198 DCHECK_GE(num_bytes_allocated_, map_size);
199 size_t allocation_size = map_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700200 num_bytes_allocated_ -= allocation_size;
201 --num_objects_allocated_;
Mathieu Chartiere7158112015-06-03 13:32:15 -0700202 delete mem_map;
203 large_objects_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700204 return allocation_size;
205}
206
Ian Rogers6fac4472014-02-25 17:01:10 -0800207size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700208 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700209 auto it = large_objects_.find(obj);
210 CHECK(it != large_objects_.end()) << "Attempted to get size of a large object which is not live";
211 size_t alloc_size = it->second.mem_map->BaseSize();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700212 if (usable_size != nullptr) {
213 *usable_size = alloc_size;
214 }
215 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700216}
217
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700219 size_t total = 0;
220 for (size_t i = 0; i < num_ptrs; ++i) {
221 if (kDebugSpaces) {
222 CHECK(Contains(ptrs[i]));
223 }
224 total += Free(self, ptrs[i]);
225 }
226 return total;
227}
228
229void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
230 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700231 for (auto& pair : large_objects_) {
232 MemMap* mem_map = pair.second.mem_map;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700233 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 callback(nullptr, nullptr, 0, arg);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700235 }
236}
237
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700239 Thread* self = Thread::Current();
240 if (lock_.IsExclusiveHeld(self)) {
241 // We hold lock_ so do the check.
Mathieu Chartiere7158112015-06-03 13:32:15 -0700242 return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700243 } else {
244 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700245 return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700246 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700247}
248
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700249// Keeps track of allocation sizes + whether or not the previous allocation is free.
Mathieu Chartiere7158112015-06-03 13:32:15 -0700250// Used to coalesce free blocks and find the best fit block for an allocation for best fit object
251// allocation. Each allocation has an AllocationInfo which contains the size of the previous free
252// block preceding it. Implemented in such a way that we can also find the iterator for any
253// allocation info pointer.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700254class AllocationInfo {
255 public:
256 AllocationInfo() : prev_free_(0), alloc_size_(0) {
257 }
258 // Return the number of pages that the allocation info covers.
259 size_t AlignSize() const {
Mathieu Chartiere7158112015-06-03 13:32:15 -0700260 return alloc_size_ & kFlagsMask;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700261 }
262 // Returns the allocation size in bytes.
263 size_t ByteSize() const {
264 return AlignSize() * FreeListSpace::kAlignment;
265 }
266 // Updates the allocation size and whether or not it is free.
267 void SetByteSize(size_t size, bool free) {
Mathieu Chartierf6c2a272015-06-03 17:32:42 -0700268 DCHECK_EQ(size & ~kFlagsMask, 0u);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700269 DCHECK_ALIGNED(size, FreeListSpace::kAlignment);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700270 alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0u);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700271 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700272 // Returns true if the block is free.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700273 bool IsFree() const {
274 return (alloc_size_ & kFlagFree) != 0;
275 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700276 // Return true if the large object is a zygote object.
277 bool IsZygoteObject() const {
278 return (alloc_size_ & kFlagZygote) != 0;
279 }
280 // Change the object to be a zygote object.
281 void SetZygoteObject() {
282 alloc_size_ |= kFlagZygote;
283 }
284 // Return true if this is a zygote large object.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700285 // Finds and returns the next non free allocation info after ourself.
286 AllocationInfo* GetNextInfo() {
287 return this + AlignSize();
288 }
289 const AllocationInfo* GetNextInfo() const {
290 return this + AlignSize();
291 }
292 // Returns the previous free allocation info by using the prev_free_ member to figure out
293 // where it is. This is only used for coalescing so we only need to be able to do it if the
294 // previous allocation info is free.
295 AllocationInfo* GetPrevFreeInfo() {
296 DCHECK_NE(prev_free_, 0U);
297 return this - prev_free_;
298 }
299 // Returns the address of the object associated with this allocation info.
300 mirror::Object* GetObjectAddress() {
301 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
302 }
303 // Return how many kAlignment units there are before the free block.
304 size_t GetPrevFree() const {
305 return prev_free_;
306 }
307 // Returns how many free bytes there is before the block.
308 size_t GetPrevFreeBytes() const {
309 return GetPrevFree() * FreeListSpace::kAlignment;
310 }
311 // Update the size of the free block prior to the allocation.
312 void SetPrevFreeBytes(size_t bytes) {
313 DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment);
314 prev_free_ = bytes / FreeListSpace::kAlignment;
315 }
316
317 private:
Mathieu Chartiere7158112015-06-03 13:32:15 -0700318 static constexpr uint32_t kFlagFree = 0x80000000; // If block is free.
319 static constexpr uint32_t kFlagZygote = 0x40000000; // If the large object is a zygote object.
320 static constexpr uint32_t kFlagsMask = ~(kFlagFree | kFlagZygote); // Combined flags for masking.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700321 // Contains the size of the previous free block with kAlignment as the unit. If 0 then the
322 // allocation before us is not free.
323 // These variables are undefined in the middle of allocations / free blocks.
324 uint32_t prev_free_;
325 // Allocation size of this object in kAlignment as the unit.
326 uint32_t alloc_size_;
327};
328
329size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const {
330 DCHECK_GE(info, allocation_info_);
331 DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End()));
332 return info - allocation_info_;
333}
334
335AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) {
336 return &allocation_info_[GetSlotIndexForAddress(address)];
337}
338
339const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const {
340 return &allocation_info_[GetSlotIndexForAddress(address)];
341}
342
343inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a,
344 const AllocationInfo* b) const {
345 if (a->GetPrevFree() < b->GetPrevFree()) return true;
346 if (a->GetPrevFree() > b->GetPrevFree()) return false;
347 if (a->AlignSize() < b->AlignSize()) return true;
348 if (a->AlignSize() > b->AlignSize()) return false;
349 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
350}
351
Ian Rogers13735952014-10-08 12:43:28 -0700352FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700353 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700354 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700355 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000356 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700357 CHECK(mem_map != nullptr) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700358 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
359}
360
Ian Rogers13735952014-10-08 12:43:28 -0700361FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700362 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700363 mem_map_(mem_map),
364 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700365 const size_t space_capacity = end - begin;
366 free_end_ = space_capacity;
367 CHECK_ALIGNED(space_capacity, kAlignment);
368 const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment);
369 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000370 allocation_info_map_.reset(
371 MemMap::MapAnonymous("large object free list space allocation info map",
372 nullptr, alloc_info_size, PROT_READ | PROT_WRITE,
373 false, false, &error_msg));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700374 CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map"
375 << error_msg;
376 allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700377}
378
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700379FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700380
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700381void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
382 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700383 const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
384 AllocationInfo* cur_info = &allocation_info_[0];
385 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
386 while (cur_info < end_info) {
387 if (!cur_info->IsFree()) {
388 size_t alloc_size = cur_info->ByteSize();
Ian Rogers13735952014-10-08 12:43:28 -0700389 uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info));
390 uint8_t* byte_end = byte_start + alloc_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700391 callback(byte_start, byte_end, alloc_size, arg);
392 callback(nullptr, nullptr, 0, arg);
393 }
394 cur_info = cur_info->GetNextInfo();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700395 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700396 CHECK_EQ(cur_info, end_info);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700397}
398
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700399void FreeListSpace::RemoveFreePrev(AllocationInfo* info) {
400 CHECK_GT(info->GetPrevFree(), 0U);
401 auto it = free_blocks_.lower_bound(info);
402 CHECK(it != free_blocks_.end());
403 CHECK_EQ(*it, info);
404 free_blocks_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700405}
406
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700408 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700409 DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " "
410 << reinterpret_cast<void*>(End());
411 DCHECK_ALIGNED(obj, kAlignment);
412 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
413 DCHECK(!info->IsFree());
414 const size_t allocation_size = info->ByteSize();
415 DCHECK_GT(allocation_size, 0U);
416 DCHECK_ALIGNED(allocation_size, kAlignment);
417 info->SetByteSize(allocation_size, true); // Mark as free.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700418 // Look at the next chunk.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700419 AllocationInfo* next_info = info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700420 // Calculate the start of the end free block.
421 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700422 size_t prev_free_bytes = info->GetPrevFreeBytes();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700423 size_t new_free_size = allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700424 if (prev_free_bytes != 0) {
425 // Coalesce with previous free chunk.
426 new_free_size += prev_free_bytes;
427 RemoveFreePrev(info);
428 info = info->GetPrevFreeInfo();
429 // The previous allocation info must not be free since we are supposed to always coalesce.
430 DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
Ian Rogers22a20862013-03-16 16:34:57 -0700431 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700432 uintptr_t next_addr = GetAddressForAllocationInfo(next_info);
433 if (next_addr >= free_end_start) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700434 // Easy case, the next chunk is the end free region.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700435 CHECK_EQ(next_addr, free_end_start);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700436 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700437 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700438 AllocationInfo* new_free_info;
439 if (next_info->IsFree()) {
440 AllocationInfo* next_next_info = next_info->GetNextInfo();
441 // Next next info can't be free since we always coalesce.
442 DCHECK(!next_next_info->IsFree());
Roland Levillain14d90572015-07-16 10:52:26 +0100443 DCHECK_ALIGNED(next_next_info->ByteSize(), kAlignment);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700444 new_free_info = next_next_info;
445 new_free_size += next_next_info->GetPrevFreeBytes();
446 RemoveFreePrev(next_next_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700447 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700448 new_free_info = next_info;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700449 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700450 new_free_info->SetPrevFreeBytes(new_free_size);
451 free_blocks_.insert(new_free_info);
452 info->SetByteSize(new_free_size, true);
453 DCHECK_EQ(info->GetNextInfo(), new_free_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700454 }
455 --num_objects_allocated_;
456 DCHECK_LE(allocation_size, num_bytes_allocated_);
457 num_bytes_allocated_ -= allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700458 madvise(obj, allocation_size, MADV_DONTNEED);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700459 if (kIsDebugBuild) {
460 // Can't disallow reads since we use them to find next chunks during coalescing.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700461 mprotect(obj, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700462 }
463 return allocation_size;
464}
465
Ian Rogers6fac4472014-02-25 17:01:10 -0800466size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700467 DCHECK(Contains(obj));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700468 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
469 DCHECK(!info->IsFree());
470 size_t alloc_size = info->ByteSize();
Ian Rogers6fac4472014-02-25 17:01:10 -0800471 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700472 *usable_size = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800473 }
474 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700475}
476
Ian Rogers6fac4472014-02-25 17:01:10 -0800477mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700478 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700479 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700480 const size_t allocation_size = RoundUp(num_bytes, kAlignment);
481 AllocationInfo temp_info;
482 temp_info.SetPrevFreeBytes(allocation_size);
483 temp_info.SetByteSize(0, false);
484 AllocationInfo* new_info;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700485 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700486 auto it = free_blocks_.lower_bound(&temp_info);
487 if (it != free_blocks_.end()) {
488 AllocationInfo* info = *it;
489 free_blocks_.erase(it);
490 // Fit our object in the previous allocation info free space.
491 new_info = info->GetPrevFreeInfo();
492 // Remove the newly allocated block from the info and update the prev_free_.
493 info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size);
494 if (info->GetPrevFreeBytes() > 0) {
495 AllocationInfo* new_free = info - info->GetPrevFree();
496 new_free->SetPrevFreeBytes(0);
497 new_free->SetByteSize(info->GetPrevFreeBytes(), true);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700498 // If there is remaining space, insert back into the free set.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700499 free_blocks_.insert(info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700500 }
501 } else {
502 // Try to steal some memory from the free space at the end of the space.
503 if (LIKELY(free_end_ >= allocation_size)) {
504 // Fit our object at the start of the end free block.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700505 new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700506 free_end_ -= allocation_size;
507 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800508 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700509 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700510 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800511 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700512 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800513 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700514 *usable_size = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800515 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700516 DCHECK(bytes_tl_bulk_allocated != nullptr);
517 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700518 // Need to do these inside of the lock.
519 ++num_objects_allocated_;
520 ++total_objects_allocated_;
521 num_bytes_allocated_ += allocation_size;
522 total_bytes_allocated_ += allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700523 mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info));
Roland Levillain91d65e02016-01-19 15:59:16 +0000524 // We always put our object at the start of the free block, there cannot be another free block
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700525 // before it.
526 if (kIsDebugBuild) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700527 mprotect(obj, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700528 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700529 new_info->SetPrevFreeBytes(0);
530 new_info->SetByteSize(allocation_size, false);
531 return obj;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700532}
533
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700534void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiere7158112015-06-03 13:32:15 -0700535 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700536 os << GetName() << " -"
537 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700538 << " end: " << reinterpret_cast<void*>(End()) << "\n";
539 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700540 const AllocationInfo* cur_info =
541 GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin()));
542 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
543 while (cur_info < end_info) {
544 size_t size = cur_info->ByteSize();
545 uintptr_t address = GetAddressForAllocationInfo(cur_info);
546 if (cur_info->IsFree()) {
547 os << "Free block at address: " << reinterpret_cast<const void*>(address)
548 << " of length " << size << " bytes\n";
549 } else {
550 os << "Large object at address: " << reinterpret_cast<const void*>(address)
551 << " of length " << size << " bytes\n";
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700552 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700553 cur_info = cur_info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700554 }
555 if (free_end_) {
556 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
557 << " of length " << free_end_ << " bytes\n";
558 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700559}
560
Mathieu Chartiere7158112015-06-03 13:32:15 -0700561bool FreeListSpace::IsZygoteLargeObject(Thread* self ATTRIBUTE_UNUSED, mirror::Object* obj) const {
562 const AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
563 DCHECK(info != nullptr);
564 return info->IsZygoteObject();
565}
566
567void FreeListSpace::SetAllLargeObjectsAsZygoteObjects(Thread* self) {
568 MutexLock mu(self, lock_);
569 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
570 for (AllocationInfo* cur_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin())),
571 *end_info = GetAllocationInfoForAddress(free_end_start); cur_info < end_info;
572 cur_info = cur_info->GetNextInfo()) {
573 if (!cur_info->IsFree()) {
574 cur_info->SetZygoteObject();
575 }
576 }
577}
578
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700579void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
580 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
581 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
582 Thread* self = context->self;
583 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
584 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
585 // the bitmaps as an optimization.
586 if (!context->swap_bitmaps) {
587 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
588 for (size_t i = 0; i < num_ptrs; ++i) {
589 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800590 }
591 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700592 context->freed.objects += num_ptrs;
593 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700594}
595
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700596collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700597 if (Begin() >= End()) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700598 return collector::ObjectBytePair(0, 0);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700599 }
600 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
601 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
602 if (swap_bitmaps) {
603 std::swap(live_bitmap, mark_bitmap);
604 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700605 AllocSpace::SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700606 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
607 reinterpret_cast<uintptr_t>(Begin()),
608 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700609 return scc.freed;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800610}
611
Mathieu Chartierb363f662014-07-16 13:28:58 -0700612void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/,
613 size_t /*failed_alloc_bytes*/) {
614 UNIMPLEMENTED(FATAL);
615}
616
Ian Rogers1d54e732013-05-02 21:10:01 -0700617} // namespace space
618} // namespace gc
619} // namespace art