blob: 5c8e4b9299ab6ad14d1babac2cf0678c347d22e5 [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
40 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070041 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
42 OVERRIDE {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070043 mirror::Object* obj =
44 LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070045 usable_size, bytes_tl_bulk_allocated);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070046 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
47 reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes);
48 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes);
Ian Rogers13735952014-10-08 12:43:28 -070049 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes,
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070050 kValgrindRedZoneBytes);
51 if (usable_size != nullptr) {
52 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
53 }
54 return object_without_rdz;
55 }
56
57 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
58 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
59 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
60 return LargeObjectMapSpace::AllocationSize(object_with_rdz, usable_size);
61 }
62
63 virtual size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
64 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
65 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
66 VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
67 return LargeObjectMapSpace::Free(self, object_with_rdz);
68 }
69
70 bool Contains(const mirror::Object* obj) const OVERRIDE {
71 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
72 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
73 return LargeObjectMapSpace::Contains(object_with_rdz);
74 }
75
76 private:
77 static constexpr size_t kValgrindRedZoneBytes = kPageSize;
78};
79
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070080void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070081 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070082 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070083 std::string temp_name = live_bitmap_->GetName();
84 live_bitmap_->SetName(mark_bitmap_->GetName());
85 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070086}
87
Ian Rogers13735952014-10-08 12:43:28 -070088LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070089 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
90 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070091 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070092}
93
94
95void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070096 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070097}
98
99LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700100 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700101 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700102
103LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700104 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700105 return new ValgrindLargeObjectMapSpace(name);
106 } else {
107 return new LargeObjectMapSpace(name);
108 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700109}
110
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700112 size_t* bytes_allocated, size_t* usable_size,
113 size_t* bytes_tl_bulk_allocated) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700114 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000115 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", nullptr, num_bytes,
116 PROT_READ | PROT_WRITE, true, false, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 if (UNLIKELY(mem_map == NULL)) {
118 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700119 return NULL;
120 }
121 MutexLock mu(self, lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700123 large_objects_.push_back(obj);
124 mem_maps_.Put(obj, mem_map);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700125 const size_t allocation_size = mem_map->BaseSize();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700126 DCHECK(bytes_allocated != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700127 begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj));
128 uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700129 if (end_ == nullptr || obj_end > end_) {
130 end_ = obj_end;
131 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700132 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800133 if (usable_size != nullptr) {
134 *usable_size = allocation_size;
135 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700136 DCHECK(bytes_tl_bulk_allocated != nullptr);
137 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700138 num_bytes_allocated_ += allocation_size;
139 total_bytes_allocated_ += allocation_size;
140 ++num_objects_allocated_;
141 ++total_objects_allocated_;
142 return obj;
143}
144
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700146 MutexLock mu(self, lock_);
147 MemMaps::iterator found = mem_maps_.find(ptr);
Mathieu Chartierd07a9132014-05-23 16:42:20 -0700148 if (UNLIKELY(found == mem_maps_.end())) {
149 Runtime::Current()->GetHeap()->DumpSpaces(LOG(ERROR));
150 LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
151 }
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700152 const size_t map_size = found->second->BaseSize();
153 DCHECK_GE(num_bytes_allocated_, map_size);
154 size_t allocation_size = map_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700155 num_bytes_allocated_ -= allocation_size;
156 --num_objects_allocated_;
157 delete found->second;
158 mem_maps_.erase(found);
159 return allocation_size;
160}
161
Ian Rogers6fac4472014-02-25 17:01:10 -0800162size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700163 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800164 auto found = mem_maps_.find(obj);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700165 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700166 size_t alloc_size = found->second->BaseSize();
167 if (usable_size != nullptr) {
168 *usable_size = alloc_size;
169 }
170 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700171}
172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700174 size_t total = 0;
175 for (size_t i = 0; i < num_ptrs; ++i) {
176 if (kDebugSpaces) {
177 CHECK(Contains(ptrs[i]));
178 }
179 total += Free(self, ptrs[i]);
180 }
181 return total;
182}
183
184void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
185 MutexLock mu(Thread::Current(), lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800186 for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700187 MemMap* mem_map = it->second;
188 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
189 callback(NULL, NULL, 0, arg);
190 }
191}
192
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700194 Thread* self = Thread::Current();
195 if (lock_.IsExclusiveHeld(self)) {
196 // We hold lock_ so do the check.
197 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
198 } else {
199 MutexLock mu(self, lock_);
200 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
201 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700202}
203
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700204// Keeps track of allocation sizes + whether or not the previous allocation is free.
205// Used to coalesce free blocks and find the best fit block for an allocation.
206class AllocationInfo {
207 public:
208 AllocationInfo() : prev_free_(0), alloc_size_(0) {
209 }
210 // Return the number of pages that the allocation info covers.
211 size_t AlignSize() const {
212 return alloc_size_ & ~kFlagFree;
213 }
214 // Returns the allocation size in bytes.
215 size_t ByteSize() const {
216 return AlignSize() * FreeListSpace::kAlignment;
217 }
218 // Updates the allocation size and whether or not it is free.
219 void SetByteSize(size_t size, bool free) {
220 DCHECK_ALIGNED(size, FreeListSpace::kAlignment);
221 alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0U);
222 }
223 bool IsFree() const {
224 return (alloc_size_ & kFlagFree) != 0;
225 }
226 // Finds and returns the next non free allocation info after ourself.
227 AllocationInfo* GetNextInfo() {
228 return this + AlignSize();
229 }
230 const AllocationInfo* GetNextInfo() const {
231 return this + AlignSize();
232 }
233 // Returns the previous free allocation info by using the prev_free_ member to figure out
234 // where it is. This is only used for coalescing so we only need to be able to do it if the
235 // previous allocation info is free.
236 AllocationInfo* GetPrevFreeInfo() {
237 DCHECK_NE(prev_free_, 0U);
238 return this - prev_free_;
239 }
240 // Returns the address of the object associated with this allocation info.
241 mirror::Object* GetObjectAddress() {
242 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
243 }
244 // Return how many kAlignment units there are before the free block.
245 size_t GetPrevFree() const {
246 return prev_free_;
247 }
248 // Returns how many free bytes there is before the block.
249 size_t GetPrevFreeBytes() const {
250 return GetPrevFree() * FreeListSpace::kAlignment;
251 }
252 // Update the size of the free block prior to the allocation.
253 void SetPrevFreeBytes(size_t bytes) {
254 DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment);
255 prev_free_ = bytes / FreeListSpace::kAlignment;
256 }
257
258 private:
259 // Used to implement best fit object allocation. Each allocation has an AllocationInfo which
260 // contains the size of the previous free block preceding it. Implemented in such a way that we
261 // can also find the iterator for any allocation info pointer.
262 static constexpr uint32_t kFlagFree = 0x8000000;
263 // Contains the size of the previous free block with kAlignment as the unit. If 0 then the
264 // allocation before us is not free.
265 // These variables are undefined in the middle of allocations / free blocks.
266 uint32_t prev_free_;
267 // Allocation size of this object in kAlignment as the unit.
268 uint32_t alloc_size_;
269};
270
271size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const {
272 DCHECK_GE(info, allocation_info_);
273 DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End()));
274 return info - allocation_info_;
275}
276
277AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) {
278 return &allocation_info_[GetSlotIndexForAddress(address)];
279}
280
281const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const {
282 return &allocation_info_[GetSlotIndexForAddress(address)];
283}
284
285inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a,
286 const AllocationInfo* b) const {
287 if (a->GetPrevFree() < b->GetPrevFree()) return true;
288 if (a->GetPrevFree() > b->GetPrevFree()) return false;
289 if (a->AlignSize() < b->AlignSize()) return true;
290 if (a->AlignSize() > b->AlignSize()) return false;
291 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
292}
293
Ian Rogers13735952014-10-08 12:43:28 -0700294FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700295 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700297 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000298 PROT_READ | PROT_WRITE, true, false, &error_msg);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700299 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700300 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
301}
302
Ian Rogers13735952014-10-08 12:43:28 -0700303FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700304 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700305 mem_map_(mem_map),
306 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700307 const size_t space_capacity = end - begin;
308 free_end_ = space_capacity;
309 CHECK_ALIGNED(space_capacity, kAlignment);
310 const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment);
311 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000312 allocation_info_map_.reset(
313 MemMap::MapAnonymous("large object free list space allocation info map",
314 nullptr, alloc_info_size, PROT_READ | PROT_WRITE,
315 false, false, &error_msg));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700316 CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map"
317 << error_msg;
318 allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700319}
320
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700321FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700322
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700323void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
324 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700325 const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
326 AllocationInfo* cur_info = &allocation_info_[0];
327 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
328 while (cur_info < end_info) {
329 if (!cur_info->IsFree()) {
330 size_t alloc_size = cur_info->ByteSize();
Ian Rogers13735952014-10-08 12:43:28 -0700331 uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info));
332 uint8_t* byte_end = byte_start + alloc_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700333 callback(byte_start, byte_end, alloc_size, arg);
334 callback(nullptr, nullptr, 0, arg);
335 }
336 cur_info = cur_info->GetNextInfo();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700337 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700338 CHECK_EQ(cur_info, end_info);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700339}
340
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700341void FreeListSpace::RemoveFreePrev(AllocationInfo* info) {
342 CHECK_GT(info->GetPrevFree(), 0U);
343 auto it = free_blocks_.lower_bound(info);
344 CHECK(it != free_blocks_.end());
345 CHECK_EQ(*it, info);
346 free_blocks_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700347}
348
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700350 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700351 DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " "
352 << reinterpret_cast<void*>(End());
353 DCHECK_ALIGNED(obj, kAlignment);
354 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
355 DCHECK(!info->IsFree());
356 const size_t allocation_size = info->ByteSize();
357 DCHECK_GT(allocation_size, 0U);
358 DCHECK_ALIGNED(allocation_size, kAlignment);
359 info->SetByteSize(allocation_size, true); // Mark as free.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700360 // Look at the next chunk.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700361 AllocationInfo* next_info = info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700362 // Calculate the start of the end free block.
363 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700364 size_t prev_free_bytes = info->GetPrevFreeBytes();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700365 size_t new_free_size = allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700366 if (prev_free_bytes != 0) {
367 // Coalesce with previous free chunk.
368 new_free_size += prev_free_bytes;
369 RemoveFreePrev(info);
370 info = info->GetPrevFreeInfo();
371 // The previous allocation info must not be free since we are supposed to always coalesce.
372 DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
Ian Rogers22a20862013-03-16 16:34:57 -0700373 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700374 uintptr_t next_addr = GetAddressForAllocationInfo(next_info);
375 if (next_addr >= free_end_start) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700376 // Easy case, the next chunk is the end free region.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700377 CHECK_EQ(next_addr, free_end_start);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700378 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700379 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700380 AllocationInfo* new_free_info;
381 if (next_info->IsFree()) {
382 AllocationInfo* next_next_info = next_info->GetNextInfo();
383 // Next next info can't be free since we always coalesce.
384 DCHECK(!next_next_info->IsFree());
385 DCHECK(IsAligned<kAlignment>(next_next_info->ByteSize()));
386 new_free_info = next_next_info;
387 new_free_size += next_next_info->GetPrevFreeBytes();
388 RemoveFreePrev(next_next_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700389 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700390 new_free_info = next_info;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700391 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700392 new_free_info->SetPrevFreeBytes(new_free_size);
393 free_blocks_.insert(new_free_info);
394 info->SetByteSize(new_free_size, true);
395 DCHECK_EQ(info->GetNextInfo(), new_free_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700396 }
397 --num_objects_allocated_;
398 DCHECK_LE(allocation_size, num_bytes_allocated_);
399 num_bytes_allocated_ -= allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700400 madvise(obj, allocation_size, MADV_DONTNEED);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700401 if (kIsDebugBuild) {
402 // Can't disallow reads since we use them to find next chunks during coalescing.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700403 mprotect(obj, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700404 }
405 return allocation_size;
406}
407
Ian Rogers6fac4472014-02-25 17:01:10 -0800408size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700409 DCHECK(Contains(obj));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700410 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
411 DCHECK(!info->IsFree());
412 size_t alloc_size = info->ByteSize();
Ian Rogers6fac4472014-02-25 17:01:10 -0800413 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700414 *usable_size = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800415 }
416 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700417}
418
Ian Rogers6fac4472014-02-25 17:01:10 -0800419mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700420 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700421 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700422 const size_t allocation_size = RoundUp(num_bytes, kAlignment);
423 AllocationInfo temp_info;
424 temp_info.SetPrevFreeBytes(allocation_size);
425 temp_info.SetByteSize(0, false);
426 AllocationInfo* new_info;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700427 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700428 auto it = free_blocks_.lower_bound(&temp_info);
429 if (it != free_blocks_.end()) {
430 AllocationInfo* info = *it;
431 free_blocks_.erase(it);
432 // Fit our object in the previous allocation info free space.
433 new_info = info->GetPrevFreeInfo();
434 // Remove the newly allocated block from the info and update the prev_free_.
435 info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size);
436 if (info->GetPrevFreeBytes() > 0) {
437 AllocationInfo* new_free = info - info->GetPrevFree();
438 new_free->SetPrevFreeBytes(0);
439 new_free->SetByteSize(info->GetPrevFreeBytes(), true);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700440 // If there is remaining space, insert back into the free set.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700441 free_blocks_.insert(info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700442 }
443 } else {
444 // Try to steal some memory from the free space at the end of the space.
445 if (LIKELY(free_end_ >= allocation_size)) {
446 // Fit our object at the start of the end free block.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700447 new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700448 free_end_ -= allocation_size;
449 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800450 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700451 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700452 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800453 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700454 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800455 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700456 *usable_size = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800457 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700458 DCHECK(bytes_tl_bulk_allocated != nullptr);
459 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700460 // Need to do these inside of the lock.
461 ++num_objects_allocated_;
462 ++total_objects_allocated_;
463 num_bytes_allocated_ += allocation_size;
464 total_bytes_allocated_ += allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700465 mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info));
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700466 // We always put our object at the start of the free block, there can not be another free block
467 // before it.
468 if (kIsDebugBuild) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700469 mprotect(obj, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700470 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700471 new_info->SetPrevFreeBytes(0);
472 new_info->SetByteSize(allocation_size, false);
473 return obj;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700474}
475
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700476void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700477 MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_));
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700478 os << GetName() << " -"
479 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700480 << " end: " << reinterpret_cast<void*>(End()) << "\n";
481 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700482 const AllocationInfo* cur_info =
483 GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin()));
484 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
485 while (cur_info < end_info) {
486 size_t size = cur_info->ByteSize();
487 uintptr_t address = GetAddressForAllocationInfo(cur_info);
488 if (cur_info->IsFree()) {
489 os << "Free block at address: " << reinterpret_cast<const void*>(address)
490 << " of length " << size << " bytes\n";
491 } else {
492 os << "Large object at address: " << reinterpret_cast<const void*>(address)
493 << " of length " << size << " bytes\n";
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700494 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700495 cur_info = cur_info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700496 }
497 if (free_end_) {
498 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
499 << " of length " << free_end_ << " bytes\n";
500 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700501}
502
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700503void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
504 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
505 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
506 Thread* self = context->self;
507 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
508 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
509 // the bitmaps as an optimization.
510 if (!context->swap_bitmaps) {
511 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
512 for (size_t i = 0; i < num_ptrs; ++i) {
513 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800514 }
515 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700516 context->freed.objects += num_ptrs;
517 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700518}
519
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700520collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700521 if (Begin() >= End()) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700522 return collector::ObjectBytePair(0, 0);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700523 }
524 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
525 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
526 if (swap_bitmaps) {
527 std::swap(live_bitmap, mark_bitmap);
528 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700529 AllocSpace::SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700530 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
531 reinterpret_cast<uintptr_t>(Begin()),
532 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700533 return scc.freed;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800534}
535
Mathieu Chartierb363f662014-07-16 13:28:58 -0700536void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/,
537 size_t /*failed_alloc_bytes*/) {
538 UNIMPLEMENTED(FATAL);
539}
540
Ian Rogers1d54e732013-05-02 21:10:01 -0700541} // namespace space
542} // namespace gc
543} // namespace art