blob: dc2769e1c861cbef8638e086a84ea786a904f595 [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
97LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070098 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070099 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700100
101LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700102 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700103 return new ValgrindLargeObjectMapSpace(name);
104 } else {
105 return new LargeObjectMapSpace(name);
106 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700107}
108
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700109mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Ian Rogers6fac4472014-02-25 17:01:10 -0800110 size_t* bytes_allocated, size_t* usable_size) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111 std::string error_msg;
Ian Rogersa40307e2013-02-22 11:32:44 -0800112 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", NULL, num_bytes,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113 PROT_READ | PROT_WRITE, true, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700114 if (UNLIKELY(mem_map == NULL)) {
115 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700116 return NULL;
117 }
118 MutexLock mu(self, lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700120 large_objects_.push_back(obj);
121 mem_maps_.Put(obj, mem_map);
122 size_t allocation_size = mem_map->Size();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700123 DCHECK(bytes_allocated != nullptr);
124 begin_ = std::min(begin_, reinterpret_cast<byte*>(obj));
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700125 byte* obj_end = reinterpret_cast<byte*>(obj) + allocation_size;
126 if (end_ == nullptr || obj_end > end_) {
127 end_ = obj_end;
128 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700129 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800130 if (usable_size != nullptr) {
131 *usable_size = allocation_size;
132 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700133 num_bytes_allocated_ += allocation_size;
134 total_bytes_allocated_ += allocation_size;
135 ++num_objects_allocated_;
136 ++total_objects_allocated_;
137 return obj;
138}
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700141 MutexLock mu(self, lock_);
142 MemMaps::iterator found = mem_maps_.find(ptr);
143 CHECK(found != mem_maps_.end()) << "Attempted to free large object which was not live";
144 DCHECK_GE(num_bytes_allocated_, found->second->Size());
145 size_t allocation_size = found->second->Size();
146 num_bytes_allocated_ -= allocation_size;
147 --num_objects_allocated_;
148 delete found->second;
149 mem_maps_.erase(found);
150 return allocation_size;
151}
152
Ian Rogers6fac4472014-02-25 17:01:10 -0800153size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700154 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800155 auto found = mem_maps_.find(obj);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700156 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
157 return found->second->Size();
158}
159
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700161 size_t total = 0;
162 for (size_t i = 0; i < num_ptrs; ++i) {
163 if (kDebugSpaces) {
164 CHECK(Contains(ptrs[i]));
165 }
166 total += Free(self, ptrs[i]);
167 }
168 return total;
169}
170
171void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
172 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800173 for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700174 MemMap* mem_map = it->second;
175 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
176 callback(NULL, NULL, 0, arg);
177 }
178}
179
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700181 Thread* self = Thread::Current();
182 if (lock_.IsExclusiveHeld(self)) {
183 // We hold lock_ so do the check.
184 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
185 } else {
186 MutexLock mu(self, lock_);
187 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
188 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700189}
190
191FreeListSpace* FreeListSpace::Create(const std::string& name, byte* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700192 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700194 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195 PROT_READ | PROT_WRITE, true, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700196 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700197 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
198}
199
200FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700201 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700202 mem_map_(mem_map),
203 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700204 free_end_ = end - begin;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700205}
206
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700207FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700208
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700209void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
210 MutexLock mu(Thread::Current(), lock_);
211 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
212 AllocationHeader* cur_header = reinterpret_cast<AllocationHeader*>(Begin());
213 while (reinterpret_cast<uintptr_t>(cur_header) < free_end_start) {
214 cur_header = cur_header->GetNextNonFree();
215 size_t alloc_size = cur_header->AllocationSize();
216 byte* byte_start = reinterpret_cast<byte*>(cur_header->GetObjectAddress());
217 byte* byte_end = byte_start + alloc_size - sizeof(AllocationHeader);
218 callback(byte_start, byte_end, alloc_size, arg);
219 callback(NULL, NULL, 0, arg);
220 cur_header = reinterpret_cast<AllocationHeader*>(byte_end);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700221 }
222}
223
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700224void FreeListSpace::RemoveFreePrev(AllocationHeader* header) {
225 CHECK(!header->IsFree());
226 CHECK_GT(header->GetPrevFree(), size_t(0));
227 FreeBlocks::iterator found = free_blocks_.lower_bound(header);
228 CHECK(found != free_blocks_.end());
229 CHECK_EQ(*found, header);
230 free_blocks_.erase(found);
231}
232
233FreeListSpace::AllocationHeader* FreeListSpace::GetAllocationHeader(const mirror::Object* obj) {
234 DCHECK(Contains(obj));
235 return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(obj) -
236 sizeof(AllocationHeader));
237}
238
239FreeListSpace::AllocationHeader* FreeListSpace::AllocationHeader::GetNextNonFree() {
240 // We know that there has to be at least one object after us or else we would have
241 // coalesced with the free end region. May be worth investigating a better way to do this
242 // as it may be expensive for large allocations.
243 for (uintptr_t pos = reinterpret_cast<uintptr_t>(this);; pos += kAlignment) {
244 AllocationHeader* cur = reinterpret_cast<AllocationHeader*>(pos);
245 if (!cur->IsFree()) return cur;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700246 }
247}
248
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700250 MutexLock mu(self, lock_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700251 DCHECK(Contains(obj));
252 AllocationHeader* header = GetAllocationHeader(obj);
253 CHECK(IsAligned<kAlignment>(header));
254 size_t allocation_size = header->AllocationSize();
255 DCHECK_GT(allocation_size, size_t(0));
256 DCHECK(IsAligned<kAlignment>(allocation_size));
257 // Look at the next chunk.
258 AllocationHeader* next_header = header->GetNextAllocationHeader();
259 // Calculate the start of the end free block.
260 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
261 size_t header_prev_free = header->GetPrevFree();
262 size_t new_free_size = allocation_size;
263 if (header_prev_free) {
264 new_free_size += header_prev_free;
265 RemoveFreePrev(header);
Ian Rogers22a20862013-03-16 16:34:57 -0700266 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700267 if (reinterpret_cast<uintptr_t>(next_header) >= free_end_start) {
268 // Easy case, the next chunk is the end free region.
269 CHECK_EQ(reinterpret_cast<uintptr_t>(next_header), free_end_start);
270 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700271 } else {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700272 AllocationHeader* new_free_header;
273 DCHECK(IsAligned<kAlignment>(next_header));
274 if (next_header->IsFree()) {
275 // Find the next chunk by reading each page until we hit one with non-zero chunk.
276 AllocationHeader* next_next_header = next_header->GetNextNonFree();
277 DCHECK(IsAligned<kAlignment>(next_next_header));
278 DCHECK(IsAligned<kAlignment>(next_next_header->AllocationSize()));
279 RemoveFreePrev(next_next_header);
280 new_free_header = next_next_header;
281 new_free_size += next_next_header->GetPrevFree();
282 } else {
283 new_free_header = next_header;
284 }
285 new_free_header->prev_free_ = new_free_size;
286 free_blocks_.insert(new_free_header);
287 }
288 --num_objects_allocated_;
289 DCHECK_LE(allocation_size, num_bytes_allocated_);
290 num_bytes_allocated_ -= allocation_size;
291 madvise(header, allocation_size, MADV_DONTNEED);
292 if (kIsDebugBuild) {
293 // Can't disallow reads since we use them to find next chunks during coalescing.
294 mprotect(header, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700295 }
296 return allocation_size;
297}
298
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299bool FreeListSpace::Contains(const mirror::Object* obj) const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700300 return mem_map_->HasAddress(obj);
301}
302
Ian Rogers6fac4472014-02-25 17:01:10 -0800303size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700304 AllocationHeader* header = GetAllocationHeader(obj);
305 DCHECK(Contains(obj));
306 DCHECK(!header->IsFree());
Ian Rogers6fac4472014-02-25 17:01:10 -0800307 size_t alloc_size = header->AllocationSize();
308 if (usable_size != nullptr) {
309 *usable_size = alloc_size - sizeof(AllocationHeader);
310 }
311 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700312}
313
Ian Rogers6fac4472014-02-25 17:01:10 -0800314mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
315 size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700316 MutexLock mu(self, lock_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700317 size_t allocation_size = RoundUp(num_bytes + sizeof(AllocationHeader), kAlignment);
318 AllocationHeader temp;
319 temp.SetPrevFree(allocation_size);
320 temp.SetAllocationSize(0);
321 AllocationHeader* new_header;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700322 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700323 FreeBlocks::iterator found = free_blocks_.lower_bound(&temp);
324 if (found != free_blocks_.end()) {
325 AllocationHeader* header = *found;
326 free_blocks_.erase(found);
327
328 // Fit our object in the previous free header space.
329 new_header = header->GetPrevFreeAllocationHeader();
330
331 // Remove the newly allocated block from the header and update the prev_free_.
332 header->prev_free_ -= allocation_size;
333 if (header->prev_free_ > 0) {
334 // If there is remaining space, insert back into the free set.
335 free_blocks_.insert(header);
336 }
337 } else {
338 // Try to steal some memory from the free space at the end of the space.
339 if (LIKELY(free_end_ >= allocation_size)) {
340 // Fit our object at the start of the end free block.
341 new_header = reinterpret_cast<AllocationHeader*>(end_ - free_end_);
342 free_end_ -= allocation_size;
343 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800344 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700345 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700346 }
347
Ian Rogers6fac4472014-02-25 17:01:10 -0800348 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700349 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800350 if (usable_size != nullptr) {
351 *usable_size = allocation_size - sizeof(AllocationHeader);
352 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700353 // Need to do these inside of the lock.
354 ++num_objects_allocated_;
355 ++total_objects_allocated_;
356 num_bytes_allocated_ += allocation_size;
357 total_bytes_allocated_ += allocation_size;
358
359 // We always put our object at the start of the free block, there can not be another free block
360 // before it.
361 if (kIsDebugBuild) {
362 mprotect(new_header, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700363 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700364 new_header->SetPrevFree(0);
365 new_header->SetAllocationSize(allocation_size);
366 return new_header->GetObjectAddress();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700367}
368
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700370 MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_));
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700371 os << GetName() << " -"
372 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700373 << " end: " << reinterpret_cast<void*>(End()) << "\n";
374 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
375 AllocationHeader* cur_header = reinterpret_cast<AllocationHeader*>(Begin());
376 while (reinterpret_cast<uintptr_t>(cur_header) < free_end_start) {
377 byte* free_start = reinterpret_cast<byte*>(cur_header);
378 cur_header = cur_header->GetNextNonFree();
379 byte* free_end = reinterpret_cast<byte*>(cur_header);
380 if (free_start != free_end) {
381 os << "Free block at address: " << reinterpret_cast<const void*>(free_start)
382 << " of length " << free_end - free_start << " bytes\n";
383 }
384 size_t alloc_size = cur_header->AllocationSize();
385 byte* byte_start = reinterpret_cast<byte*>(cur_header->GetObjectAddress());
386 byte* byte_end = byte_start + alloc_size - sizeof(AllocationHeader);
387 os << "Large object at address: " << reinterpret_cast<const void*>(free_start)
388 << " of length " << byte_end - byte_start << " bytes\n";
389 cur_header = reinterpret_cast<AllocationHeader*>(byte_end);
390 }
391 if (free_end_) {
392 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
393 << " of length " << free_end_ << " bytes\n";
394 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700395}
396
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700397void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
398 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
399 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
400 Thread* self = context->self;
401 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
402 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
403 // the bitmaps as an optimization.
404 if (!context->swap_bitmaps) {
405 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
406 for (size_t i = 0; i < num_ptrs; ++i) {
407 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800408 }
409 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700410 context->freed_objects += num_ptrs;
411 context->freed_bytes += space->FreeList(self, num_ptrs, ptrs);
412}
413
414void LargeObjectSpace::Sweep(bool swap_bitmaps, size_t* out_freed_objects,
415 size_t* out_freed_bytes) {
416 if (Begin() >= End()) {
417 return;
418 }
419 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
420 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
421 if (swap_bitmaps) {
422 std::swap(live_bitmap, mark_bitmap);
423 }
424 DCHECK(out_freed_objects != nullptr);
425 DCHECK(out_freed_bytes != nullptr);
426 SweepCallbackContext scc(swap_bitmaps, this);
427 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
428 reinterpret_cast<uintptr_t>(Begin()),
429 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
430 *out_freed_objects += scc.freed_objects;
431 *out_freed_bytes += scc.freed_bytes;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800432}
433
Ian Rogers1d54e732013-05-02 21:10:01 -0700434} // namespace space
435} // namespace gc
436} // namespace art