blob: ce11b3d72cd487e8e5ca99afb66df75cb7a3599d [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 Chartierbbd695c2014-04-16 09:48:48 -070019#include "gc/accounting/space_bitmap-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070021#include "base/mutex-inl.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070023#include "UniquePtr.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070024#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070025#include "os.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070026#include "space-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080027#include "thread-inl.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070028#include "utils.h"
29
30namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070031namespace gc {
32namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070033
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070034class ValgrindLargeObjectMapSpace FINAL : public LargeObjectMapSpace {
35 public:
36 explicit ValgrindLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
37 }
38
39 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
40 size_t* usable_size) OVERRIDE {
41 mirror::Object* obj =
42 LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated,
43 usable_size);
44 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
45 reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes);
46 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes);
47 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(object_without_rdz) + num_bytes,
48 kValgrindRedZoneBytes);
49 if (usable_size != nullptr) {
50 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
51 }
52 return object_without_rdz;
53 }
54
55 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
56 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
57 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
58 return LargeObjectMapSpace::AllocationSize(object_with_rdz, usable_size);
59 }
60
61 virtual size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
62 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
63 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
64 VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
65 return LargeObjectMapSpace::Free(self, object_with_rdz);
66 }
67
68 bool Contains(const mirror::Object* obj) const OVERRIDE {
69 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
70 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
71 return LargeObjectMapSpace::Contains(object_with_rdz);
72 }
73
74 private:
75 static constexpr size_t kValgrindRedZoneBytes = kPageSize;
76};
77
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070078void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070079 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070080 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070081 std::string temp_name = live_bitmap_->GetName();
82 live_bitmap_->SetName(mark_bitmap_->GetName());
83 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070084}
85
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070086LargeObjectSpace::LargeObjectSpace(const std::string& name, byte* begin, byte* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070087 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
88 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070089 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070090}
91
92
93void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070094 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070095}
96
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070097// TODO: Use something cleaner than 0xFFFFFFFF.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070098LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070099 : LargeObjectSpace(name, reinterpret_cast<byte*>(0xFFFFFFFF), nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700100 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101
102LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700103 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700104 return new ValgrindLargeObjectMapSpace(name);
105 } else {
106 return new LargeObjectMapSpace(name);
107 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700108}
109
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700110mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Ian Rogers6fac4472014-02-25 17:01:10 -0800111 size_t* bytes_allocated, size_t* usable_size) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700112 std::string error_msg;
Ian Rogersa40307e2013-02-22 11:32:44 -0800113 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", NULL, num_bytes,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114 PROT_READ | PROT_WRITE, true, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115 if (UNLIKELY(mem_map == NULL)) {
116 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700117 return NULL;
118 }
119 MutexLock mu(self, lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700121 large_objects_.push_back(obj);
122 mem_maps_.Put(obj, mem_map);
123 size_t allocation_size = mem_map->Size();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700124 DCHECK(bytes_allocated != nullptr);
125 begin_ = std::min(begin_, reinterpret_cast<byte*>(obj));
126 end_ = std::max(end_, reinterpret_cast<byte*>(obj) + allocation_size);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700127 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800128 if (usable_size != nullptr) {
129 *usable_size = allocation_size;
130 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700131 num_bytes_allocated_ += allocation_size;
132 total_bytes_allocated_ += allocation_size;
133 ++num_objects_allocated_;
134 ++total_objects_allocated_;
135 return obj;
136}
137
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700139 MutexLock mu(self, lock_);
140 MemMaps::iterator found = mem_maps_.find(ptr);
141 CHECK(found != mem_maps_.end()) << "Attempted to free large object which was not live";
142 DCHECK_GE(num_bytes_allocated_, found->second->Size());
143 size_t allocation_size = found->second->Size();
144 num_bytes_allocated_ -= allocation_size;
145 --num_objects_allocated_;
146 delete found->second;
147 mem_maps_.erase(found);
148 return allocation_size;
149}
150
Ian Rogers6fac4472014-02-25 17:01:10 -0800151size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700152 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800153 auto found = mem_maps_.find(obj);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700154 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
155 return found->second->Size();
156}
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700159 size_t total = 0;
160 for (size_t i = 0; i < num_ptrs; ++i) {
161 if (kDebugSpaces) {
162 CHECK(Contains(ptrs[i]));
163 }
164 total += Free(self, ptrs[i]);
165 }
166 return total;
167}
168
169void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
170 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800171 for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700172 MemMap* mem_map = it->second;
173 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
174 callback(NULL, NULL, 0, arg);
175 }
176}
177
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700179 Thread* self = Thread::Current();
180 if (lock_.IsExclusiveHeld(self)) {
181 // We hold lock_ so do the check.
182 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
183 } else {
184 MutexLock mu(self, lock_);
185 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
186 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700187}
188
189FreeListSpace* FreeListSpace::Create(const std::string& name, byte* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700190 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700191 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700192 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193 PROT_READ | PROT_WRITE, true, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700194 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700195 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
196}
197
198FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700199 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700200 mem_map_(mem_map),
201 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700202 free_end_ = end - begin;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700203}
204
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700205FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700206
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700207void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
208 MutexLock mu(Thread::Current(), lock_);
209 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
210 AllocationHeader* cur_header = reinterpret_cast<AllocationHeader*>(Begin());
211 while (reinterpret_cast<uintptr_t>(cur_header) < free_end_start) {
212 cur_header = cur_header->GetNextNonFree();
213 size_t alloc_size = cur_header->AllocationSize();
214 byte* byte_start = reinterpret_cast<byte*>(cur_header->GetObjectAddress());
215 byte* byte_end = byte_start + alloc_size - sizeof(AllocationHeader);
216 callback(byte_start, byte_end, alloc_size, arg);
217 callback(NULL, NULL, 0, arg);
218 cur_header = reinterpret_cast<AllocationHeader*>(byte_end);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700219 }
220}
221
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700222void FreeListSpace::RemoveFreePrev(AllocationHeader* header) {
223 CHECK(!header->IsFree());
224 CHECK_GT(header->GetPrevFree(), size_t(0));
225 FreeBlocks::iterator found = free_blocks_.lower_bound(header);
226 CHECK(found != free_blocks_.end());
227 CHECK_EQ(*found, header);
228 free_blocks_.erase(found);
229}
230
231FreeListSpace::AllocationHeader* FreeListSpace::GetAllocationHeader(const mirror::Object* obj) {
232 DCHECK(Contains(obj));
233 return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(obj) -
234 sizeof(AllocationHeader));
235}
236
237FreeListSpace::AllocationHeader* FreeListSpace::AllocationHeader::GetNextNonFree() {
238 // We know that there has to be at least one object after us or else we would have
239 // coalesced with the free end region. May be worth investigating a better way to do this
240 // as it may be expensive for large allocations.
241 for (uintptr_t pos = reinterpret_cast<uintptr_t>(this);; pos += kAlignment) {
242 AllocationHeader* cur = reinterpret_cast<AllocationHeader*>(pos);
243 if (!cur->IsFree()) return cur;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700244 }
245}
246
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700248 MutexLock mu(self, lock_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700249 DCHECK(Contains(obj));
250 AllocationHeader* header = GetAllocationHeader(obj);
251 CHECK(IsAligned<kAlignment>(header));
252 size_t allocation_size = header->AllocationSize();
253 DCHECK_GT(allocation_size, size_t(0));
254 DCHECK(IsAligned<kAlignment>(allocation_size));
255 // Look at the next chunk.
256 AllocationHeader* next_header = header->GetNextAllocationHeader();
257 // Calculate the start of the end free block.
258 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
259 size_t header_prev_free = header->GetPrevFree();
260 size_t new_free_size = allocation_size;
261 if (header_prev_free) {
262 new_free_size += header_prev_free;
263 RemoveFreePrev(header);
Ian Rogers22a20862013-03-16 16:34:57 -0700264 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700265 if (reinterpret_cast<uintptr_t>(next_header) >= free_end_start) {
266 // Easy case, the next chunk is the end free region.
267 CHECK_EQ(reinterpret_cast<uintptr_t>(next_header), free_end_start);
268 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700269 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700270 AllocationHeader* new_free_header;
271 DCHECK(IsAligned<kAlignment>(next_header));
272 if (next_header->IsFree()) {
273 // Find the next chunk by reading each page until we hit one with non-zero chunk.
274 AllocationHeader* next_next_header = next_header->GetNextNonFree();
275 DCHECK(IsAligned<kAlignment>(next_next_header));
276 DCHECK(IsAligned<kAlignment>(next_next_header->AllocationSize()));
277 RemoveFreePrev(next_next_header);
278 new_free_header = next_next_header;
279 new_free_size += next_next_header->GetPrevFree();
280 } else {
281 new_free_header = next_header;
282 }
283 new_free_header->prev_free_ = new_free_size;
284 free_blocks_.insert(new_free_header);
285 }
286 --num_objects_allocated_;
287 DCHECK_LE(allocation_size, num_bytes_allocated_);
288 num_bytes_allocated_ -= allocation_size;
289 madvise(header, allocation_size, MADV_DONTNEED);
290 if (kIsDebugBuild) {
291 // Can't disallow reads since we use them to find next chunks during coalescing.
292 mprotect(header, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700293 }
294 return allocation_size;
295}
296
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297bool FreeListSpace::Contains(const mirror::Object* obj) const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700298 return mem_map_->HasAddress(obj);
299}
300
Ian Rogers6fac4472014-02-25 17:01:10 -0800301size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700302 AllocationHeader* header = GetAllocationHeader(obj);
303 DCHECK(Contains(obj));
304 DCHECK(!header->IsFree());
Ian Rogers6fac4472014-02-25 17:01:10 -0800305 size_t alloc_size = header->AllocationSize();
306 if (usable_size != nullptr) {
307 *usable_size = alloc_size - sizeof(AllocationHeader);
308 }
309 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700310}
311
Ian Rogers6fac4472014-02-25 17:01:10 -0800312mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
313 size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700314 MutexLock mu(self, lock_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700315 size_t allocation_size = RoundUp(num_bytes + sizeof(AllocationHeader), kAlignment);
316 AllocationHeader temp;
317 temp.SetPrevFree(allocation_size);
318 temp.SetAllocationSize(0);
319 AllocationHeader* new_header;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700320 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700321 FreeBlocks::iterator found = free_blocks_.lower_bound(&temp);
322 if (found != free_blocks_.end()) {
323 AllocationHeader* header = *found;
324 free_blocks_.erase(found);
325
326 // Fit our object in the previous free header space.
327 new_header = header->GetPrevFreeAllocationHeader();
328
329 // Remove the newly allocated block from the header and update the prev_free_.
330 header->prev_free_ -= allocation_size;
331 if (header->prev_free_ > 0) {
332 // If there is remaining space, insert back into the free set.
333 free_blocks_.insert(header);
334 }
335 } else {
336 // Try to steal some memory from the free space at the end of the space.
337 if (LIKELY(free_end_ >= allocation_size)) {
338 // Fit our object at the start of the end free block.
339 new_header = reinterpret_cast<AllocationHeader*>(end_ - free_end_);
340 free_end_ -= allocation_size;
341 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800342 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700343 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700344 }
345
Ian Rogers6fac4472014-02-25 17:01:10 -0800346 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700347 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800348 if (usable_size != nullptr) {
349 *usable_size = allocation_size - sizeof(AllocationHeader);
350 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700351 // Need to do these inside of the lock.
352 ++num_objects_allocated_;
353 ++total_objects_allocated_;
354 num_bytes_allocated_ += allocation_size;
355 total_bytes_allocated_ += allocation_size;
356
357 // We always put our object at the start of the free block, there can not be another free block
358 // before it.
359 if (kIsDebugBuild) {
360 mprotect(new_header, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700361 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700362 new_header->SetPrevFree(0);
363 new_header->SetAllocationSize(allocation_size);
364 return new_header->GetObjectAddress();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700365}
366
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700368 MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_));
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700369 os << GetName() << " -"
370 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700371 << " end: " << reinterpret_cast<void*>(End()) << "\n";
372 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
373 AllocationHeader* cur_header = reinterpret_cast<AllocationHeader*>(Begin());
374 while (reinterpret_cast<uintptr_t>(cur_header) < free_end_start) {
375 byte* free_start = reinterpret_cast<byte*>(cur_header);
376 cur_header = cur_header->GetNextNonFree();
377 byte* free_end = reinterpret_cast<byte*>(cur_header);
378 if (free_start != free_end) {
379 os << "Free block at address: " << reinterpret_cast<const void*>(free_start)
380 << " of length " << free_end - free_start << " bytes\n";
381 }
382 size_t alloc_size = cur_header->AllocationSize();
383 byte* byte_start = reinterpret_cast<byte*>(cur_header->GetObjectAddress());
384 byte* byte_end = byte_start + alloc_size - sizeof(AllocationHeader);
385 os << "Large object at address: " << reinterpret_cast<const void*>(free_start)
386 << " of length " << byte_end - byte_start << " bytes\n";
387 cur_header = reinterpret_cast<AllocationHeader*>(byte_end);
388 }
389 if (free_end_) {
390 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
391 << " of length " << free_end_ << " bytes\n";
392 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700393}
394
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700395void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
396 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
397 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
398 Thread* self = context->self;
399 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
400 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
401 // the bitmaps as an optimization.
402 if (!context->swap_bitmaps) {
403 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
404 for (size_t i = 0; i < num_ptrs; ++i) {
405 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800406 }
407 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700408 context->freed_objects += num_ptrs;
409 context->freed_bytes += space->FreeList(self, num_ptrs, ptrs);
410}
411
412void LargeObjectSpace::Sweep(bool swap_bitmaps, size_t* out_freed_objects,
413 size_t* out_freed_bytes) {
414 if (Begin() >= End()) {
415 return;
416 }
417 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
418 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
419 if (swap_bitmaps) {
420 std::swap(live_bitmap, mark_bitmap);
421 }
422 DCHECK(out_freed_objects != nullptr);
423 DCHECK(out_freed_bytes != nullptr);
424 SweepCallbackContext scc(swap_bitmaps, this);
425 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
426 reinterpret_cast<uintptr_t>(Begin()),
427 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
428 *out_freed_objects += scc.freed_objects;
429 *out_freed_bytes += scc.freed_bytes;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800430}
431
Ian Rogers1d54e732013-05-02 21:10:01 -0700432} // namespace space
433} // namespace gc
434} // namespace art