blob: a4a9d80fa0d20a95f9d47c156cfd23337e0bec6f [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 Chartierbbd695c2014-04-16 09:48:48 -070021#include "gc/accounting/space_bitmap-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070023#include "base/mutex-inl.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080024#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070025#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070026#include "os.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070027#include "space-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080028#include "thread-inl.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "utils.h"
30
31namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070032namespace gc {
33namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070034
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070035class ValgrindLargeObjectMapSpace FINAL : public LargeObjectMapSpace {
36 public:
37 explicit ValgrindLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
38 }
39
Mathieu Chartier9086b652015-04-14 09:35:18 -070040 ~ValgrindLargeObjectMapSpace() OVERRIDE {
41 // Keep valgrind happy if there is any large objects such as dex cache arrays which aren't
42 // freed since they are held live by the class linker.
43 MutexLock mu(Thread::Current(), lock_);
44 for (auto& m : mem_maps_) {
45 delete m.second;
46 }
47 }
48
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070049 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070050 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
51 OVERRIDE {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070052 mirror::Object* obj =
53 LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070054 usable_size, bytes_tl_bulk_allocated);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070055 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
56 reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes);
57 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes);
Ian Rogers13735952014-10-08 12:43:28 -070058 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes,
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070059 kValgrindRedZoneBytes);
60 if (usable_size != nullptr) {
61 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
62 }
63 return object_without_rdz;
64 }
65
66 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
67 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
68 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
69 return LargeObjectMapSpace::AllocationSize(object_with_rdz, usable_size);
70 }
71
72 virtual size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
73 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
74 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
75 VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
76 return LargeObjectMapSpace::Free(self, object_with_rdz);
77 }
78
79 bool Contains(const mirror::Object* obj) const OVERRIDE {
80 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
81 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
82 return LargeObjectMapSpace::Contains(object_with_rdz);
83 }
84
85 private:
86 static constexpr size_t kValgrindRedZoneBytes = kPageSize;
87};
88
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070089void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070090 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070091 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070092 std::string temp_name = live_bitmap_->GetName();
93 live_bitmap_->SetName(mark_bitmap_->GetName());
94 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070095}
96
Ian Rogers13735952014-10-08 12:43:28 -070097LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070098 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
99 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700100 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101}
102
103
104void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700105 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700106}
107
108LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700109 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700110 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700111
112LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700113 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700114 return new ValgrindLargeObjectMapSpace(name);
115 } else {
116 return new LargeObjectMapSpace(name);
117 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118}
119
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700120mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700121 size_t* bytes_allocated, size_t* usable_size,
122 size_t* bytes_tl_bulk_allocated) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700123 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000124 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", nullptr, num_bytes,
125 PROT_READ | PROT_WRITE, true, false, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700126 if (UNLIKELY(mem_map == NULL)) {
127 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700128 return NULL;
129 }
130 MutexLock mu(self, lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700132 large_objects_.push_back(obj);
133 mem_maps_.Put(obj, mem_map);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700134 const size_t allocation_size = mem_map->BaseSize();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700135 DCHECK(bytes_allocated != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700136 begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj));
137 uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700138 if (end_ == nullptr || obj_end > end_) {
139 end_ = obj_end;
140 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700141 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800142 if (usable_size != nullptr) {
143 *usable_size = allocation_size;
144 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700145 DCHECK(bytes_tl_bulk_allocated != nullptr);
146 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700147 num_bytes_allocated_ += allocation_size;
148 total_bytes_allocated_ += allocation_size;
149 ++num_objects_allocated_;
150 ++total_objects_allocated_;
151 return obj;
152}
153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700155 MutexLock mu(self, lock_);
156 MemMaps::iterator found = mem_maps_.find(ptr);
Mathieu Chartierd07a9132014-05-23 16:42:20 -0700157 if (UNLIKELY(found == mem_maps_.end())) {
158 Runtime::Current()->GetHeap()->DumpSpaces(LOG(ERROR));
159 LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
160 }
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700161 const size_t map_size = found->second->BaseSize();
162 DCHECK_GE(num_bytes_allocated_, map_size);
163 size_t allocation_size = map_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700164 num_bytes_allocated_ -= allocation_size;
165 --num_objects_allocated_;
166 delete found->second;
167 mem_maps_.erase(found);
168 return allocation_size;
169}
170
Ian Rogers6fac4472014-02-25 17:01:10 -0800171size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700172 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800173 auto found = mem_maps_.find(obj);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700174 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700175 size_t alloc_size = found->second->BaseSize();
176 if (usable_size != nullptr) {
177 *usable_size = alloc_size;
178 }
179 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700180}
181
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700183 size_t total = 0;
184 for (size_t i = 0; i < num_ptrs; ++i) {
185 if (kDebugSpaces) {
186 CHECK(Contains(ptrs[i]));
187 }
188 total += Free(self, ptrs[i]);
189 }
190 return total;
191}
192
193void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
194 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800195 for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700196 MemMap* mem_map = it->second;
197 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
198 callback(NULL, NULL, 0, arg);
199 }
200}
201
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700203 Thread* self = Thread::Current();
204 if (lock_.IsExclusiveHeld(self)) {
205 // We hold lock_ so do the check.
206 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
207 } else {
208 MutexLock mu(self, lock_);
209 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
210 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700211}
212
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700213// Keeps track of allocation sizes + whether or not the previous allocation is free.
214// Used to coalesce free blocks and find the best fit block for an allocation.
215class AllocationInfo {
216 public:
217 AllocationInfo() : prev_free_(0), alloc_size_(0) {
218 }
219 // Return the number of pages that the allocation info covers.
220 size_t AlignSize() const {
221 return alloc_size_ & ~kFlagFree;
222 }
223 // Returns the allocation size in bytes.
224 size_t ByteSize() const {
225 return AlignSize() * FreeListSpace::kAlignment;
226 }
227 // Updates the allocation size and whether or not it is free.
228 void SetByteSize(size_t size, bool free) {
229 DCHECK_ALIGNED(size, FreeListSpace::kAlignment);
230 alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0U);
231 }
232 bool IsFree() const {
233 return (alloc_size_ & kFlagFree) != 0;
234 }
235 // Finds and returns the next non free allocation info after ourself.
236 AllocationInfo* GetNextInfo() {
237 return this + AlignSize();
238 }
239 const AllocationInfo* GetNextInfo() const {
240 return this + AlignSize();
241 }
242 // Returns the previous free allocation info by using the prev_free_ member to figure out
243 // where it is. This is only used for coalescing so we only need to be able to do it if the
244 // previous allocation info is free.
245 AllocationInfo* GetPrevFreeInfo() {
246 DCHECK_NE(prev_free_, 0U);
247 return this - prev_free_;
248 }
249 // Returns the address of the object associated with this allocation info.
250 mirror::Object* GetObjectAddress() {
251 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
252 }
253 // Return how many kAlignment units there are before the free block.
254 size_t GetPrevFree() const {
255 return prev_free_;
256 }
257 // Returns how many free bytes there is before the block.
258 size_t GetPrevFreeBytes() const {
259 return GetPrevFree() * FreeListSpace::kAlignment;
260 }
261 // Update the size of the free block prior to the allocation.
262 void SetPrevFreeBytes(size_t bytes) {
263 DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment);
264 prev_free_ = bytes / FreeListSpace::kAlignment;
265 }
266
267 private:
268 // Used to implement best fit object allocation. Each allocation has an AllocationInfo which
269 // contains the size of the previous free block preceding it. Implemented in such a way that we
270 // can also find the iterator for any allocation info pointer.
271 static constexpr uint32_t kFlagFree = 0x8000000;
272 // Contains the size of the previous free block with kAlignment as the unit. If 0 then the
273 // allocation before us is not free.
274 // These variables are undefined in the middle of allocations / free blocks.
275 uint32_t prev_free_;
276 // Allocation size of this object in kAlignment as the unit.
277 uint32_t alloc_size_;
278};
279
280size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const {
281 DCHECK_GE(info, allocation_info_);
282 DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End()));
283 return info - allocation_info_;
284}
285
286AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) {
287 return &allocation_info_[GetSlotIndexForAddress(address)];
288}
289
290const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const {
291 return &allocation_info_[GetSlotIndexForAddress(address)];
292}
293
294inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a,
295 const AllocationInfo* b) const {
296 if (a->GetPrevFree() < b->GetPrevFree()) return true;
297 if (a->GetPrevFree() > b->GetPrevFree()) return false;
298 if (a->AlignSize() < b->AlignSize()) return true;
299 if (a->AlignSize() > b->AlignSize()) return false;
300 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
301}
302
Ian Rogers13735952014-10-08 12:43:28 -0700303FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700304 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700305 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700306 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000307 PROT_READ | PROT_WRITE, true, false, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700308 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700309 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
310}
311
Ian Rogers13735952014-10-08 12:43:28 -0700312FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700313 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700314 mem_map_(mem_map),
315 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700316 const size_t space_capacity = end - begin;
317 free_end_ = space_capacity;
318 CHECK_ALIGNED(space_capacity, kAlignment);
319 const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment);
320 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000321 allocation_info_map_.reset(
322 MemMap::MapAnonymous("large object free list space allocation info map",
323 nullptr, alloc_info_size, PROT_READ | PROT_WRITE,
324 false, false, &error_msg));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700325 CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map"
326 << error_msg;
327 allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700328}
329
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700330FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700331
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700332void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
333 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700334 const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
335 AllocationInfo* cur_info = &allocation_info_[0];
336 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
337 while (cur_info < end_info) {
338 if (!cur_info->IsFree()) {
339 size_t alloc_size = cur_info->ByteSize();
Ian Rogers13735952014-10-08 12:43:28 -0700340 uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info));
341 uint8_t* byte_end = byte_start + alloc_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700342 callback(byte_start, byte_end, alloc_size, arg);
343 callback(nullptr, nullptr, 0, arg);
344 }
345 cur_info = cur_info->GetNextInfo();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700346 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700347 CHECK_EQ(cur_info, end_info);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700348}
349
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700350void FreeListSpace::RemoveFreePrev(AllocationInfo* info) {
351 CHECK_GT(info->GetPrevFree(), 0U);
352 auto it = free_blocks_.lower_bound(info);
353 CHECK(it != free_blocks_.end());
354 CHECK_EQ(*it, info);
355 free_blocks_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700356}
357
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700359 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700360 DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " "
361 << reinterpret_cast<void*>(End());
362 DCHECK_ALIGNED(obj, kAlignment);
363 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
364 DCHECK(!info->IsFree());
365 const size_t allocation_size = info->ByteSize();
366 DCHECK_GT(allocation_size, 0U);
367 DCHECK_ALIGNED(allocation_size, kAlignment);
368 info->SetByteSize(allocation_size, true); // Mark as free.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700369 // Look at the next chunk.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700370 AllocationInfo* next_info = info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700371 // Calculate the start of the end free block.
372 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700373 size_t prev_free_bytes = info->GetPrevFreeBytes();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700374 size_t new_free_size = allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700375 if (prev_free_bytes != 0) {
376 // Coalesce with previous free chunk.
377 new_free_size += prev_free_bytes;
378 RemoveFreePrev(info);
379 info = info->GetPrevFreeInfo();
380 // The previous allocation info must not be free since we are supposed to always coalesce.
381 DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
Ian Rogers22a20862013-03-16 16:34:57 -0700382 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700383 uintptr_t next_addr = GetAddressForAllocationInfo(next_info);
384 if (next_addr >= free_end_start) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700385 // Easy case, the next chunk is the end free region.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700386 CHECK_EQ(next_addr, free_end_start);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700387 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700388 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700389 AllocationInfo* new_free_info;
390 if (next_info->IsFree()) {
391 AllocationInfo* next_next_info = next_info->GetNextInfo();
392 // Next next info can't be free since we always coalesce.
393 DCHECK(!next_next_info->IsFree());
394 DCHECK(IsAligned<kAlignment>(next_next_info->ByteSize()));
395 new_free_info = next_next_info;
396 new_free_size += next_next_info->GetPrevFreeBytes();
397 RemoveFreePrev(next_next_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700398 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700399 new_free_info = next_info;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700400 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700401 new_free_info->SetPrevFreeBytes(new_free_size);
402 free_blocks_.insert(new_free_info);
403 info->SetByteSize(new_free_size, true);
404 DCHECK_EQ(info->GetNextInfo(), new_free_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700405 }
406 --num_objects_allocated_;
407 DCHECK_LE(allocation_size, num_bytes_allocated_);
408 num_bytes_allocated_ -= allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700409 madvise(obj, allocation_size, MADV_DONTNEED);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700410 if (kIsDebugBuild) {
411 // Can't disallow reads since we use them to find next chunks during coalescing.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700412 mprotect(obj, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700413 }
414 return allocation_size;
415}
416
Ian Rogers6fac4472014-02-25 17:01:10 -0800417size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700418 DCHECK(Contains(obj));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700419 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
420 DCHECK(!info->IsFree());
421 size_t alloc_size = info->ByteSize();
Ian Rogers6fac4472014-02-25 17:01:10 -0800422 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700423 *usable_size = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800424 }
425 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700426}
427
Ian Rogers6fac4472014-02-25 17:01:10 -0800428mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700429 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700430 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700431 const size_t allocation_size = RoundUp(num_bytes, kAlignment);
432 AllocationInfo temp_info;
433 temp_info.SetPrevFreeBytes(allocation_size);
434 temp_info.SetByteSize(0, false);
435 AllocationInfo* new_info;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700436 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700437 auto it = free_blocks_.lower_bound(&temp_info);
438 if (it != free_blocks_.end()) {
439 AllocationInfo* info = *it;
440 free_blocks_.erase(it);
441 // Fit our object in the previous allocation info free space.
442 new_info = info->GetPrevFreeInfo();
443 // Remove the newly allocated block from the info and update the prev_free_.
444 info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size);
445 if (info->GetPrevFreeBytes() > 0) {
446 AllocationInfo* new_free = info - info->GetPrevFree();
447 new_free->SetPrevFreeBytes(0);
448 new_free->SetByteSize(info->GetPrevFreeBytes(), true);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700449 // If there is remaining space, insert back into the free set.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700450 free_blocks_.insert(info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700451 }
452 } else {
453 // Try to steal some memory from the free space at the end of the space.
454 if (LIKELY(free_end_ >= allocation_size)) {
455 // Fit our object at the start of the end free block.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700456 new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700457 free_end_ -= allocation_size;
458 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800459 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700460 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700461 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800462 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700463 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800464 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700465 *usable_size = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800466 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700467 DCHECK(bytes_tl_bulk_allocated != nullptr);
468 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700469 // Need to do these inside of the lock.
470 ++num_objects_allocated_;
471 ++total_objects_allocated_;
472 num_bytes_allocated_ += allocation_size;
473 total_bytes_allocated_ += allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700474 mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info));
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700475 // We always put our object at the start of the free block, there can not be another free block
476 // before it.
477 if (kIsDebugBuild) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700478 mprotect(obj, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700479 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700480 new_info->SetPrevFreeBytes(0);
481 new_info->SetByteSize(allocation_size, false);
482 return obj;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700483}
484
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700485void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700486 MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_));
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700487 os << GetName() << " -"
488 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700489 << " end: " << reinterpret_cast<void*>(End()) << "\n";
490 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700491 const AllocationInfo* cur_info =
492 GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin()));
493 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
494 while (cur_info < end_info) {
495 size_t size = cur_info->ByteSize();
496 uintptr_t address = GetAddressForAllocationInfo(cur_info);
497 if (cur_info->IsFree()) {
498 os << "Free block at address: " << reinterpret_cast<const void*>(address)
499 << " of length " << size << " bytes\n";
500 } else {
501 os << "Large object at address: " << reinterpret_cast<const void*>(address)
502 << " of length " << size << " bytes\n";
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700503 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700504 cur_info = cur_info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700505 }
506 if (free_end_) {
507 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
508 << " of length " << free_end_ << " bytes\n";
509 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700510}
511
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700512void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
513 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
514 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
515 Thread* self = context->self;
516 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
517 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
518 // the bitmaps as an optimization.
519 if (!context->swap_bitmaps) {
520 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
521 for (size_t i = 0; i < num_ptrs; ++i) {
522 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800523 }
524 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700525 context->freed.objects += num_ptrs;
526 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700527}
528
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700529collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700530 if (Begin() >= End()) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700531 return collector::ObjectBytePair(0, 0);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700532 }
533 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
534 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
535 if (swap_bitmaps) {
536 std::swap(live_bitmap, mark_bitmap);
537 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700538 AllocSpace::SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700539 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
540 reinterpret_cast<uintptr_t>(Begin()),
541 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700542 return scc.freed;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800543}
544
Mathieu Chartierb363f662014-07-16 13:28:58 -0700545void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/,
546 size_t /*failed_alloc_bytes*/) {
547 UNIMPLEMENTED(FATAL);
548}
549
Ian Rogers1d54e732013-05-02 21:10:01 -0700550} // namespace space
551} // namespace gc
552} // namespace art