blob: 7353c83b024bb659304080c3538181d3274a7c03 [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
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Mathieu Chartierc8980de2015-04-19 13:36:11 -070021#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070022#include "gc/accounting/space_bitmap-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070024#include "base/mutex-inl.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070026#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070027#include "os.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070028#include "space-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080029#include "thread-inl.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030#include "utils.h"
31
32namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070033namespace gc {
34namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070035
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070036class ValgrindLargeObjectMapSpace FINAL : public LargeObjectMapSpace {
37 public:
38 explicit ValgrindLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
39 }
40
Mathieu Chartier9086b652015-04-14 09:35:18 -070041 ~ValgrindLargeObjectMapSpace() OVERRIDE {
42 // Keep valgrind happy if there is any large objects such as dex cache arrays which aren't
43 // freed since they are held live by the class linker.
44 MutexLock mu(Thread::Current(), lock_);
45 for (auto& m : mem_maps_) {
46 delete m.second;
47 }
48 }
49
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070050 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070051 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
52 OVERRIDE {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070053 mirror::Object* obj =
54 LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070055 usable_size, bytes_tl_bulk_allocated);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070056 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
57 reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes);
58 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes);
Ian Rogers13735952014-10-08 12:43:28 -070059 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes,
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070060 kValgrindRedZoneBytes);
61 if (usable_size != nullptr) {
62 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
63 }
64 return object_without_rdz;
65 }
66
67 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
68 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
69 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
70 return LargeObjectMapSpace::AllocationSize(object_with_rdz, usable_size);
71 }
72
73 virtual size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
74 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
75 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
76 VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
77 return LargeObjectMapSpace::Free(self, object_with_rdz);
78 }
79
80 bool Contains(const mirror::Object* obj) const OVERRIDE {
81 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
82 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
83 return LargeObjectMapSpace::Contains(object_with_rdz);
84 }
85
86 private:
87 static constexpr size_t kValgrindRedZoneBytes = kPageSize;
88};
89
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070090void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070091 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070092 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070093 std::string temp_name = live_bitmap_->GetName();
94 live_bitmap_->SetName(mark_bitmap_->GetName());
95 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070096}
97
Ian Rogers13735952014-10-08 12:43:28 -070098LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070099 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
100 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700101 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700102}
103
104
105void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700106 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700107}
108
109LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700110 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700111 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700112
113LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700114 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700115 return new ValgrindLargeObjectMapSpace(name);
116 } else {
117 return new LargeObjectMapSpace(name);
118 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700119}
120
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700121mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700122 size_t* bytes_allocated, size_t* usable_size,
123 size_t* bytes_tl_bulk_allocated) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700124 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000125 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", nullptr, num_bytes,
126 PROT_READ | PROT_WRITE, true, false, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 if (UNLIKELY(mem_map == NULL)) {
128 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700129 return NULL;
130 }
Mathieu Chartierc8980de2015-04-19 13:36:11 -0700131 mirror::Object* const obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
132 if (kIsDebugBuild) {
133 ReaderMutexLock mu2(Thread::Current(), *Locks::heap_bitmap_lock_);
134 auto* heap = Runtime::Current()->GetHeap();
135 auto* live_bitmap = heap->GetLiveBitmap();
136 auto* space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj);
137 CHECK(space_bitmap == nullptr) << obj << " overlaps with bitmap " << *space_bitmap;
138 auto* obj_end = reinterpret_cast<mirror::Object*>(mem_map->End());
139 space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj_end - 1);
140 CHECK(space_bitmap == nullptr) << obj_end << " overlaps with bitmap " << *space_bitmap;
141 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700142 MutexLock mu(self, lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700143 large_objects_.push_back(obj);
144 mem_maps_.Put(obj, mem_map);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700145 const size_t allocation_size = mem_map->BaseSize();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700146 DCHECK(bytes_allocated != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700147 begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj));
148 uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700149 if (end_ == nullptr || obj_end > end_) {
150 end_ = obj_end;
151 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700152 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800153 if (usable_size != nullptr) {
154 *usable_size = allocation_size;
155 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700156 DCHECK(bytes_tl_bulk_allocated != nullptr);
157 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700158 num_bytes_allocated_ += allocation_size;
159 total_bytes_allocated_ += allocation_size;
160 ++num_objects_allocated_;
161 ++total_objects_allocated_;
162 return obj;
163}
164
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700166 MutexLock mu(self, lock_);
167 MemMaps::iterator found = mem_maps_.find(ptr);
Mathieu Chartierd07a9132014-05-23 16:42:20 -0700168 if (UNLIKELY(found == mem_maps_.end())) {
169 Runtime::Current()->GetHeap()->DumpSpaces(LOG(ERROR));
170 LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
171 }
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700172 const size_t map_size = found->second->BaseSize();
173 DCHECK_GE(num_bytes_allocated_, map_size);
174 size_t allocation_size = map_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700175 num_bytes_allocated_ -= allocation_size;
176 --num_objects_allocated_;
177 delete found->second;
178 mem_maps_.erase(found);
179 return allocation_size;
180}
181
Ian Rogers6fac4472014-02-25 17:01:10 -0800182size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700183 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800184 auto found = mem_maps_.find(obj);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700185 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700186 size_t alloc_size = found->second->BaseSize();
187 if (usable_size != nullptr) {
188 *usable_size = alloc_size;
189 }
190 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700191}
192
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700194 size_t total = 0;
195 for (size_t i = 0; i < num_ptrs; ++i) {
196 if (kDebugSpaces) {
197 CHECK(Contains(ptrs[i]));
198 }
199 total += Free(self, ptrs[i]);
200 }
201 return total;
202}
203
204void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
205 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800206 for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700207 MemMap* mem_map = it->second;
208 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
209 callback(NULL, NULL, 0, arg);
210 }
211}
212
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700214 Thread* self = Thread::Current();
215 if (lock_.IsExclusiveHeld(self)) {
216 // We hold lock_ so do the check.
217 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
218 } else {
219 MutexLock mu(self, lock_);
220 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
221 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700222}
223
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700224// Keeps track of allocation sizes + whether or not the previous allocation is free.
225// Used to coalesce free blocks and find the best fit block for an allocation.
226class AllocationInfo {
227 public:
228 AllocationInfo() : prev_free_(0), alloc_size_(0) {
229 }
230 // Return the number of pages that the allocation info covers.
231 size_t AlignSize() const {
232 return alloc_size_ & ~kFlagFree;
233 }
234 // Returns the allocation size in bytes.
235 size_t ByteSize() const {
236 return AlignSize() * FreeListSpace::kAlignment;
237 }
238 // Updates the allocation size and whether or not it is free.
239 void SetByteSize(size_t size, bool free) {
240 DCHECK_ALIGNED(size, FreeListSpace::kAlignment);
241 alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0U);
242 }
243 bool IsFree() const {
244 return (alloc_size_ & kFlagFree) != 0;
245 }
246 // Finds and returns the next non free allocation info after ourself.
247 AllocationInfo* GetNextInfo() {
248 return this + AlignSize();
249 }
250 const AllocationInfo* GetNextInfo() const {
251 return this + AlignSize();
252 }
253 // Returns the previous free allocation info by using the prev_free_ member to figure out
254 // where it is. This is only used for coalescing so we only need to be able to do it if the
255 // previous allocation info is free.
256 AllocationInfo* GetPrevFreeInfo() {
257 DCHECK_NE(prev_free_, 0U);
258 return this - prev_free_;
259 }
260 // Returns the address of the object associated with this allocation info.
261 mirror::Object* GetObjectAddress() {
262 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
263 }
264 // Return how many kAlignment units there are before the free block.
265 size_t GetPrevFree() const {
266 return prev_free_;
267 }
268 // Returns how many free bytes there is before the block.
269 size_t GetPrevFreeBytes() const {
270 return GetPrevFree() * FreeListSpace::kAlignment;
271 }
272 // Update the size of the free block prior to the allocation.
273 void SetPrevFreeBytes(size_t bytes) {
274 DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment);
275 prev_free_ = bytes / FreeListSpace::kAlignment;
276 }
277
278 private:
279 // Used to implement best fit object allocation. Each allocation has an AllocationInfo which
280 // contains the size of the previous free block preceding it. Implemented in such a way that we
281 // can also find the iterator for any allocation info pointer.
282 static constexpr uint32_t kFlagFree = 0x8000000;
283 // Contains the size of the previous free block with kAlignment as the unit. If 0 then the
284 // allocation before us is not free.
285 // These variables are undefined in the middle of allocations / free blocks.
286 uint32_t prev_free_;
287 // Allocation size of this object in kAlignment as the unit.
288 uint32_t alloc_size_;
289};
290
291size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const {
292 DCHECK_GE(info, allocation_info_);
293 DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End()));
294 return info - allocation_info_;
295}
296
297AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) {
298 return &allocation_info_[GetSlotIndexForAddress(address)];
299}
300
301const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const {
302 return &allocation_info_[GetSlotIndexForAddress(address)];
303}
304
305inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a,
306 const AllocationInfo* b) const {
307 if (a->GetPrevFree() < b->GetPrevFree()) return true;
308 if (a->GetPrevFree() > b->GetPrevFree()) return false;
309 if (a->AlignSize() < b->AlignSize()) return true;
310 if (a->AlignSize() > b->AlignSize()) return false;
311 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
312}
313
Ian Rogers13735952014-10-08 12:43:28 -0700314FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700315 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700316 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700317 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000318 PROT_READ | PROT_WRITE, true, false, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700319 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700320 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
321}
322
Ian Rogers13735952014-10-08 12:43:28 -0700323FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700324 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700325 mem_map_(mem_map),
326 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700327 const size_t space_capacity = end - begin;
328 free_end_ = space_capacity;
329 CHECK_ALIGNED(space_capacity, kAlignment);
330 const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment);
331 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000332 allocation_info_map_.reset(
333 MemMap::MapAnonymous("large object free list space allocation info map",
334 nullptr, alloc_info_size, PROT_READ | PROT_WRITE,
335 false, false, &error_msg));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700336 CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map"
337 << error_msg;
338 allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700339}
340
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700341FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700342
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700343void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
344 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700345 const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
346 AllocationInfo* cur_info = &allocation_info_[0];
347 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
348 while (cur_info < end_info) {
349 if (!cur_info->IsFree()) {
350 size_t alloc_size = cur_info->ByteSize();
Ian Rogers13735952014-10-08 12:43:28 -0700351 uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info));
352 uint8_t* byte_end = byte_start + alloc_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700353 callback(byte_start, byte_end, alloc_size, arg);
354 callback(nullptr, nullptr, 0, arg);
355 }
356 cur_info = cur_info->GetNextInfo();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700357 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700358 CHECK_EQ(cur_info, end_info);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700359}
360
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700361void FreeListSpace::RemoveFreePrev(AllocationInfo* info) {
362 CHECK_GT(info->GetPrevFree(), 0U);
363 auto it = free_blocks_.lower_bound(info);
364 CHECK(it != free_blocks_.end());
365 CHECK_EQ(*it, info);
366 free_blocks_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700367}
368
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700370 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700371 DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " "
372 << reinterpret_cast<void*>(End());
373 DCHECK_ALIGNED(obj, kAlignment);
374 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
375 DCHECK(!info->IsFree());
376 const size_t allocation_size = info->ByteSize();
377 DCHECK_GT(allocation_size, 0U);
378 DCHECK_ALIGNED(allocation_size, kAlignment);
379 info->SetByteSize(allocation_size, true); // Mark as free.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700380 // Look at the next chunk.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700381 AllocationInfo* next_info = info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700382 // Calculate the start of the end free block.
383 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700384 size_t prev_free_bytes = info->GetPrevFreeBytes();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700385 size_t new_free_size = allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700386 if (prev_free_bytes != 0) {
387 // Coalesce with previous free chunk.
388 new_free_size += prev_free_bytes;
389 RemoveFreePrev(info);
390 info = info->GetPrevFreeInfo();
391 // The previous allocation info must not be free since we are supposed to always coalesce.
392 DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
Ian Rogers22a20862013-03-16 16:34:57 -0700393 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700394 uintptr_t next_addr = GetAddressForAllocationInfo(next_info);
395 if (next_addr >= free_end_start) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700396 // Easy case, the next chunk is the end free region.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700397 CHECK_EQ(next_addr, free_end_start);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700398 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700399 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700400 AllocationInfo* new_free_info;
401 if (next_info->IsFree()) {
402 AllocationInfo* next_next_info = next_info->GetNextInfo();
403 // Next next info can't be free since we always coalesce.
404 DCHECK(!next_next_info->IsFree());
405 DCHECK(IsAligned<kAlignment>(next_next_info->ByteSize()));
406 new_free_info = next_next_info;
407 new_free_size += next_next_info->GetPrevFreeBytes();
408 RemoveFreePrev(next_next_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700409 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700410 new_free_info = next_info;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700411 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700412 new_free_info->SetPrevFreeBytes(new_free_size);
413 free_blocks_.insert(new_free_info);
414 info->SetByteSize(new_free_size, true);
415 DCHECK_EQ(info->GetNextInfo(), new_free_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700416 }
417 --num_objects_allocated_;
418 DCHECK_LE(allocation_size, num_bytes_allocated_);
419 num_bytes_allocated_ -= allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700420 madvise(obj, allocation_size, MADV_DONTNEED);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700421 if (kIsDebugBuild) {
422 // Can't disallow reads since we use them to find next chunks during coalescing.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700423 mprotect(obj, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700424 }
425 return allocation_size;
426}
427
Ian Rogers6fac4472014-02-25 17:01:10 -0800428size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700429 DCHECK(Contains(obj));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700430 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
431 DCHECK(!info->IsFree());
432 size_t alloc_size = info->ByteSize();
Ian Rogers6fac4472014-02-25 17:01:10 -0800433 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700434 *usable_size = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800435 }
436 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700437}
438
Ian Rogers6fac4472014-02-25 17:01:10 -0800439mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700440 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700441 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700442 const size_t allocation_size = RoundUp(num_bytes, kAlignment);
443 AllocationInfo temp_info;
444 temp_info.SetPrevFreeBytes(allocation_size);
445 temp_info.SetByteSize(0, false);
446 AllocationInfo* new_info;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700447 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700448 auto it = free_blocks_.lower_bound(&temp_info);
449 if (it != free_blocks_.end()) {
450 AllocationInfo* info = *it;
451 free_blocks_.erase(it);
452 // Fit our object in the previous allocation info free space.
453 new_info = info->GetPrevFreeInfo();
454 // Remove the newly allocated block from the info and update the prev_free_.
455 info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size);
456 if (info->GetPrevFreeBytes() > 0) {
457 AllocationInfo* new_free = info - info->GetPrevFree();
458 new_free->SetPrevFreeBytes(0);
459 new_free->SetByteSize(info->GetPrevFreeBytes(), true);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700460 // If there is remaining space, insert back into the free set.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700461 free_blocks_.insert(info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700462 }
463 } else {
464 // Try to steal some memory from the free space at the end of the space.
465 if (LIKELY(free_end_ >= allocation_size)) {
466 // Fit our object at the start of the end free block.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700467 new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700468 free_end_ -= allocation_size;
469 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800470 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700471 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700472 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800473 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700474 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800475 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700476 *usable_size = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800477 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700478 DCHECK(bytes_tl_bulk_allocated != nullptr);
479 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700480 // Need to do these inside of the lock.
481 ++num_objects_allocated_;
482 ++total_objects_allocated_;
483 num_bytes_allocated_ += allocation_size;
484 total_bytes_allocated_ += allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700485 mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info));
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700486 // We always put our object at the start of the free block, there can not be another free block
487 // before it.
488 if (kIsDebugBuild) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700489 mprotect(obj, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700490 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700491 new_info->SetPrevFreeBytes(0);
492 new_info->SetByteSize(allocation_size, false);
493 return obj;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700494}
495
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700496void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700497 MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_));
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700498 os << GetName() << " -"
499 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700500 << " end: " << reinterpret_cast<void*>(End()) << "\n";
501 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700502 const AllocationInfo* cur_info =
503 GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin()));
504 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
505 while (cur_info < end_info) {
506 size_t size = cur_info->ByteSize();
507 uintptr_t address = GetAddressForAllocationInfo(cur_info);
508 if (cur_info->IsFree()) {
509 os << "Free block at address: " << reinterpret_cast<const void*>(address)
510 << " of length " << size << " bytes\n";
511 } else {
512 os << "Large object at address: " << reinterpret_cast<const void*>(address)
513 << " of length " << size << " bytes\n";
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700514 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700515 cur_info = cur_info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700516 }
517 if (free_end_) {
518 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
519 << " of length " << free_end_ << " bytes\n";
520 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700521}
522
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700523void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
524 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
525 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
526 Thread* self = context->self;
527 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
528 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
529 // the bitmaps as an optimization.
530 if (!context->swap_bitmaps) {
531 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
532 for (size_t i = 0; i < num_ptrs; ++i) {
533 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800534 }
535 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700536 context->freed.objects += num_ptrs;
537 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700538}
539
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700540collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700541 if (Begin() >= End()) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700542 return collector::ObjectBytePair(0, 0);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700543 }
544 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
545 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
546 if (swap_bitmaps) {
547 std::swap(live_bitmap, mark_bitmap);
548 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700549 AllocSpace::SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700550 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
551 reinterpret_cast<uintptr_t>(Begin()),
552 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700553 return scc.freed;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800554}
555
Mathieu Chartierb363f662014-07-16 13:28:58 -0700556void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/,
557 size_t /*failed_alloc_bytes*/) {
558 UNIMPLEMENTED(FATAL);
559}
560
Ian Rogers1d54e732013-05-02 21:10:01 -0700561} // namespace space
562} // namespace gc
563} // namespace art