blob: 2b567fee855b50964fce6e5f554be32020857ea6 [file] [log] [blame]
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers1d54e732013-05-02 21:10:01 -070017#include "large_object_space.h"
18
Mathieu Chartier3cf22532015-07-09 15:15:09 -070019#include <valgrind.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Mathieu Chartier3cf22532015-07-09 15:15:09 -070021#include <memcheck/memcheck.h>
Ian Rogers700a4022014-05-19 16:49:03 -070022
Mathieu Chartierc8980de2015-04-19 13:36:11 -070023#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070024#include "gc/accounting/space_bitmap-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070026#include "base/mutex-inl.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070028#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "os.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070030#include "space-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080031#include "thread-inl.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070032
33namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070034namespace gc {
35namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070036
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070037class ValgrindLargeObjectMapSpace FINAL : public LargeObjectMapSpace {
38 public:
39 explicit ValgrindLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
40 }
41
Mathieu Chartier9086b652015-04-14 09:35:18 -070042 ~ValgrindLargeObjectMapSpace() OVERRIDE {
43 // Keep valgrind happy if there is any large objects such as dex cache arrays which aren't
44 // freed since they are held live by the class linker.
45 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -070046 for (auto& m : large_objects_) {
47 delete m.second.mem_map;
Mathieu Chartier9086b652015-04-14 09:35:18 -070048 }
49 }
50
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070051 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
52 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070053 OVERRIDE {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070054 mirror::Object* obj =
55 LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070056 usable_size, bytes_tl_bulk_allocated);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070057 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
58 reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes);
59 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes);
Ian Rogers13735952014-10-08 12:43:28 -070060 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes,
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070061 kValgrindRedZoneBytes);
62 if (usable_size != nullptr) {
63 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
64 }
65 return object_without_rdz;
66 }
67
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070068 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
69 return LargeObjectMapSpace::AllocationSize(ObjectWithRedzone(obj), usable_size);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070070 }
71
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070072 bool IsZygoteLargeObject(Thread* self, mirror::Object* obj) const OVERRIDE {
73 return LargeObjectMapSpace::IsZygoteLargeObject(self, ObjectWithRedzone(obj));
74 }
75
76 size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
77 mirror::Object* object_with_rdz = ObjectWithRedzone(obj);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070078 VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
79 return LargeObjectMapSpace::Free(self, object_with_rdz);
80 }
81
82 bool Contains(const mirror::Object* obj) const OVERRIDE {
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070083 return LargeObjectMapSpace::Contains(ObjectWithRedzone(obj));
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070084 }
85
86 private:
Mathieu Chartierf6c2a272015-06-03 17:32:42 -070087 static const mirror::Object* ObjectWithRedzone(const mirror::Object* obj) {
88 return reinterpret_cast<const mirror::Object*>(
89 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
90 }
91
92 static mirror::Object* ObjectWithRedzone(mirror::Object* obj) {
93 return reinterpret_cast<mirror::Object*>(
94 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
95 }
96
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070097 static constexpr size_t kValgrindRedZoneBytes = kPageSize;
98};
99
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700100void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700101 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700102 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700103 std::string temp_name = live_bitmap_->GetName();
104 live_bitmap_->SetName(mark_bitmap_->GetName());
105 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700106}
107
Ian Rogers13735952014-10-08 12:43:28 -0700108LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700109 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
110 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700111 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700112}
113
114
115void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700116 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700117}
118
119LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700120 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700121 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700122
123LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700124 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700125 return new ValgrindLargeObjectMapSpace(name);
126 } else {
127 return new LargeObjectMapSpace(name);
128 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700129}
130
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700131mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700132 size_t* bytes_allocated, size_t* usable_size,
133 size_t* bytes_tl_bulk_allocated) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700134 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000135 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", nullptr, num_bytes,
136 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 if (UNLIKELY(mem_map == nullptr)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700138 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 return nullptr;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700140 }
Mathieu Chartierc8980de2015-04-19 13:36:11 -0700141 mirror::Object* const obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
142 if (kIsDebugBuild) {
143 ReaderMutexLock mu2(Thread::Current(), *Locks::heap_bitmap_lock_);
144 auto* heap = Runtime::Current()->GetHeap();
145 auto* live_bitmap = heap->GetLiveBitmap();
146 auto* space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj);
147 CHECK(space_bitmap == nullptr) << obj << " overlaps with bitmap " << *space_bitmap;
148 auto* obj_end = reinterpret_cast<mirror::Object*>(mem_map->End());
149 space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj_end - 1);
150 CHECK(space_bitmap == nullptr) << obj_end << " overlaps with bitmap " << *space_bitmap;
151 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700152 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700153 large_objects_.Put(obj, LargeObject {mem_map, false /* not zygote */});
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700154 const size_t allocation_size = mem_map->BaseSize();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700155 DCHECK(bytes_allocated != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700156 begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj));
157 uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700158 if (end_ == nullptr || obj_end > end_) {
159 end_ = obj_end;
160 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700161 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800162 if (usable_size != nullptr) {
163 *usable_size = allocation_size;
164 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700165 DCHECK(bytes_tl_bulk_allocated != nullptr);
166 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700167 num_bytes_allocated_ += allocation_size;
168 total_bytes_allocated_ += allocation_size;
169 ++num_objects_allocated_;
170 ++total_objects_allocated_;
171 return obj;
172}
173
Mathieu Chartiere7158112015-06-03 13:32:15 -0700174bool LargeObjectMapSpace::IsZygoteLargeObject(Thread* self, mirror::Object* obj) const {
175 MutexLock mu(self, lock_);
176 auto it = large_objects_.find(obj);
177 CHECK(it != large_objects_.end());
178 return it->second.is_zygote;
179}
180
181void LargeObjectMapSpace::SetAllLargeObjectsAsZygoteObjects(Thread* self) {
182 MutexLock mu(self, lock_);
183 for (auto& pair : large_objects_) {
184 pair.second.is_zygote = true;
185 }
186}
187
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700189 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700190 auto it = large_objects_.find(ptr);
191 if (UNLIKELY(it == large_objects_.end())) {
192 Runtime::Current()->GetHeap()->DumpSpaces(LOG(INTERNAL_FATAL));
Mathieu Chartierd07a9132014-05-23 16:42:20 -0700193 LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
194 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700195 MemMap* mem_map = it->second.mem_map;
196 const size_t map_size = mem_map->BaseSize();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700197 DCHECK_GE(num_bytes_allocated_, map_size);
198 size_t allocation_size = map_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700199 num_bytes_allocated_ -= allocation_size;
200 --num_objects_allocated_;
Mathieu Chartiere7158112015-06-03 13:32:15 -0700201 delete mem_map;
202 large_objects_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700203 return allocation_size;
204}
205
Ian Rogers6fac4472014-02-25 17:01:10 -0800206size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700207 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700208 auto it = large_objects_.find(obj);
209 CHECK(it != large_objects_.end()) << "Attempted to get size of a large object which is not live";
210 size_t alloc_size = it->second.mem_map->BaseSize();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700211 if (usable_size != nullptr) {
212 *usable_size = alloc_size;
213 }
214 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700215}
216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700218 size_t total = 0;
219 for (size_t i = 0; i < num_ptrs; ++i) {
220 if (kDebugSpaces) {
221 CHECK(Contains(ptrs[i]));
222 }
223 total += Free(self, ptrs[i]);
224 }
225 return total;
226}
227
228void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
229 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700230 for (auto& pair : large_objects_) {
231 MemMap* mem_map = pair.second.mem_map;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700232 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700233 callback(nullptr, nullptr, 0, arg);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700234 }
235}
236
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700238 Thread* self = Thread::Current();
239 if (lock_.IsExclusiveHeld(self)) {
240 // We hold lock_ so do the check.
Mathieu Chartiere7158112015-06-03 13:32:15 -0700241 return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700242 } else {
243 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700244 return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700245 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700246}
247
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700248// Keeps track of allocation sizes + whether or not the previous allocation is free.
Mathieu Chartiere7158112015-06-03 13:32:15 -0700249// Used to coalesce free blocks and find the best fit block for an allocation for best fit object
250// allocation. Each allocation has an AllocationInfo which contains the size of the previous free
251// block preceding it. Implemented in such a way that we can also find the iterator for any
252// allocation info pointer.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700253class AllocationInfo {
254 public:
255 AllocationInfo() : prev_free_(0), alloc_size_(0) {
256 }
257 // Return the number of pages that the allocation info covers.
258 size_t AlignSize() const {
Mathieu Chartiere7158112015-06-03 13:32:15 -0700259 return alloc_size_ & kFlagsMask;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700260 }
261 // Returns the allocation size in bytes.
262 size_t ByteSize() const {
263 return AlignSize() * FreeListSpace::kAlignment;
264 }
265 // Updates the allocation size and whether or not it is free.
266 void SetByteSize(size_t size, bool free) {
Mathieu Chartierf6c2a272015-06-03 17:32:42 -0700267 DCHECK_EQ(size & ~kFlagsMask, 0u);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700268 DCHECK_ALIGNED(size, FreeListSpace::kAlignment);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700269 alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0u);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700270 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700271 // Returns true if the block is free.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700272 bool IsFree() const {
273 return (alloc_size_ & kFlagFree) != 0;
274 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700275 // Return true if the large object is a zygote object.
276 bool IsZygoteObject() const {
277 return (alloc_size_ & kFlagZygote) != 0;
278 }
279 // Change the object to be a zygote object.
280 void SetZygoteObject() {
281 alloc_size_ |= kFlagZygote;
282 }
283 // Return true if this is a zygote large object.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700284 // Finds and returns the next non free allocation info after ourself.
285 AllocationInfo* GetNextInfo() {
286 return this + AlignSize();
287 }
288 const AllocationInfo* GetNextInfo() const {
289 return this + AlignSize();
290 }
291 // Returns the previous free allocation info by using the prev_free_ member to figure out
292 // where it is. This is only used for coalescing so we only need to be able to do it if the
293 // previous allocation info is free.
294 AllocationInfo* GetPrevFreeInfo() {
295 DCHECK_NE(prev_free_, 0U);
296 return this - prev_free_;
297 }
298 // Returns the address of the object associated with this allocation info.
299 mirror::Object* GetObjectAddress() {
300 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
301 }
302 // Return how many kAlignment units there are before the free block.
303 size_t GetPrevFree() const {
304 return prev_free_;
305 }
306 // Returns how many free bytes there is before the block.
307 size_t GetPrevFreeBytes() const {
308 return GetPrevFree() * FreeListSpace::kAlignment;
309 }
310 // Update the size of the free block prior to the allocation.
311 void SetPrevFreeBytes(size_t bytes) {
312 DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment);
313 prev_free_ = bytes / FreeListSpace::kAlignment;
314 }
315
316 private:
Mathieu Chartiere7158112015-06-03 13:32:15 -0700317 static constexpr uint32_t kFlagFree = 0x80000000; // If block is free.
318 static constexpr uint32_t kFlagZygote = 0x40000000; // If the large object is a zygote object.
319 static constexpr uint32_t kFlagsMask = ~(kFlagFree | kFlagZygote); // Combined flags for masking.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700320 // Contains the size of the previous free block with kAlignment as the unit. If 0 then the
321 // allocation before us is not free.
322 // These variables are undefined in the middle of allocations / free blocks.
323 uint32_t prev_free_;
324 // Allocation size of this object in kAlignment as the unit.
325 uint32_t alloc_size_;
326};
327
328size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const {
329 DCHECK_GE(info, allocation_info_);
330 DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End()));
331 return info - allocation_info_;
332}
333
334AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) {
335 return &allocation_info_[GetSlotIndexForAddress(address)];
336}
337
338const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const {
339 return &allocation_info_[GetSlotIndexForAddress(address)];
340}
341
342inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a,
343 const AllocationInfo* b) const {
344 if (a->GetPrevFree() < b->GetPrevFree()) return true;
345 if (a->GetPrevFree() > b->GetPrevFree()) return false;
346 if (a->AlignSize() < b->AlignSize()) return true;
347 if (a->AlignSize() > b->AlignSize()) return false;
348 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
349}
350
Ian Rogers13735952014-10-08 12:43:28 -0700351FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700352 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700353 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700354 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000355 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700356 CHECK(mem_map != nullptr) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700357 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
358}
359
Ian Rogers13735952014-10-08 12:43:28 -0700360FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700361 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700362 mem_map_(mem_map),
363 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700364 const size_t space_capacity = end - begin;
365 free_end_ = space_capacity;
366 CHECK_ALIGNED(space_capacity, kAlignment);
367 const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment);
368 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000369 allocation_info_map_.reset(
370 MemMap::MapAnonymous("large object free list space allocation info map",
371 nullptr, alloc_info_size, PROT_READ | PROT_WRITE,
372 false, false, &error_msg));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700373 CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map"
374 << error_msg;
375 allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700376}
377
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700378FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700379
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700380void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
381 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700382 const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
383 AllocationInfo* cur_info = &allocation_info_[0];
384 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
385 while (cur_info < end_info) {
386 if (!cur_info->IsFree()) {
387 size_t alloc_size = cur_info->ByteSize();
Ian Rogers13735952014-10-08 12:43:28 -0700388 uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info));
389 uint8_t* byte_end = byte_start + alloc_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700390 callback(byte_start, byte_end, alloc_size, arg);
391 callback(nullptr, nullptr, 0, arg);
392 }
393 cur_info = cur_info->GetNextInfo();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700394 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700395 CHECK_EQ(cur_info, end_info);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700396}
397
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700398void FreeListSpace::RemoveFreePrev(AllocationInfo* info) {
399 CHECK_GT(info->GetPrevFree(), 0U);
400 auto it = free_blocks_.lower_bound(info);
401 CHECK(it != free_blocks_.end());
402 CHECK_EQ(*it, info);
403 free_blocks_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700404}
405
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700407 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700408 DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " "
409 << reinterpret_cast<void*>(End());
410 DCHECK_ALIGNED(obj, kAlignment);
411 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
412 DCHECK(!info->IsFree());
413 const size_t allocation_size = info->ByteSize();
414 DCHECK_GT(allocation_size, 0U);
415 DCHECK_ALIGNED(allocation_size, kAlignment);
416 info->SetByteSize(allocation_size, true); // Mark as free.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700417 // Look at the next chunk.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700418 AllocationInfo* next_info = info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700419 // Calculate the start of the end free block.
420 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700421 size_t prev_free_bytes = info->GetPrevFreeBytes();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700422 size_t new_free_size = allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700423 if (prev_free_bytes != 0) {
424 // Coalesce with previous free chunk.
425 new_free_size += prev_free_bytes;
426 RemoveFreePrev(info);
427 info = info->GetPrevFreeInfo();
428 // The previous allocation info must not be free since we are supposed to always coalesce.
429 DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
Ian Rogers22a20862013-03-16 16:34:57 -0700430 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700431 uintptr_t next_addr = GetAddressForAllocationInfo(next_info);
432 if (next_addr >= free_end_start) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700433 // Easy case, the next chunk is the end free region.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700434 CHECK_EQ(next_addr, free_end_start);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700435 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700436 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700437 AllocationInfo* new_free_info;
438 if (next_info->IsFree()) {
439 AllocationInfo* next_next_info = next_info->GetNextInfo();
440 // Next next info can't be free since we always coalesce.
441 DCHECK(!next_next_info->IsFree());
442 DCHECK(IsAligned<kAlignment>(next_next_info->ByteSize()));
443 new_free_info = next_next_info;
444 new_free_size += next_next_info->GetPrevFreeBytes();
445 RemoveFreePrev(next_next_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700446 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700447 new_free_info = next_info;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700448 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700449 new_free_info->SetPrevFreeBytes(new_free_size);
450 free_blocks_.insert(new_free_info);
451 info->SetByteSize(new_free_size, true);
452 DCHECK_EQ(info->GetNextInfo(), new_free_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700453 }
454 --num_objects_allocated_;
455 DCHECK_LE(allocation_size, num_bytes_allocated_);
456 num_bytes_allocated_ -= allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700457 madvise(obj, allocation_size, MADV_DONTNEED);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700458 if (kIsDebugBuild) {
459 // Can't disallow reads since we use them to find next chunks during coalescing.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700460 mprotect(obj, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700461 }
462 return allocation_size;
463}
464
Ian Rogers6fac4472014-02-25 17:01:10 -0800465size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700466 DCHECK(Contains(obj));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700467 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
468 DCHECK(!info->IsFree());
469 size_t alloc_size = info->ByteSize();
Ian Rogers6fac4472014-02-25 17:01:10 -0800470 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700471 *usable_size = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800472 }
473 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700474}
475
Ian Rogers6fac4472014-02-25 17:01:10 -0800476mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700477 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700478 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700479 const size_t allocation_size = RoundUp(num_bytes, kAlignment);
480 AllocationInfo temp_info;
481 temp_info.SetPrevFreeBytes(allocation_size);
482 temp_info.SetByteSize(0, false);
483 AllocationInfo* new_info;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700484 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700485 auto it = free_blocks_.lower_bound(&temp_info);
486 if (it != free_blocks_.end()) {
487 AllocationInfo* info = *it;
488 free_blocks_.erase(it);
489 // Fit our object in the previous allocation info free space.
490 new_info = info->GetPrevFreeInfo();
491 // Remove the newly allocated block from the info and update the prev_free_.
492 info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size);
493 if (info->GetPrevFreeBytes() > 0) {
494 AllocationInfo* new_free = info - info->GetPrevFree();
495 new_free->SetPrevFreeBytes(0);
496 new_free->SetByteSize(info->GetPrevFreeBytes(), true);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700497 // If there is remaining space, insert back into the free set.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700498 free_blocks_.insert(info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700499 }
500 } else {
501 // Try to steal some memory from the free space at the end of the space.
502 if (LIKELY(free_end_ >= allocation_size)) {
503 // Fit our object at the start of the end free block.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700504 new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700505 free_end_ -= allocation_size;
506 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800507 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700508 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700509 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800510 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700511 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800512 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700513 *usable_size = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800514 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700515 DCHECK(bytes_tl_bulk_allocated != nullptr);
516 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700517 // Need to do these inside of the lock.
518 ++num_objects_allocated_;
519 ++total_objects_allocated_;
520 num_bytes_allocated_ += allocation_size;
521 total_bytes_allocated_ += allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700522 mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info));
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700523 // We always put our object at the start of the free block, there can not be another free block
524 // before it.
525 if (kIsDebugBuild) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700526 mprotect(obj, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700527 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700528 new_info->SetPrevFreeBytes(0);
529 new_info->SetByteSize(allocation_size, false);
530 return obj;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700531}
532
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700533void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiere7158112015-06-03 13:32:15 -0700534 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700535 os << GetName() << " -"
536 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700537 << " end: " << reinterpret_cast<void*>(End()) << "\n";
538 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700539 const AllocationInfo* cur_info =
540 GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin()));
541 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
542 while (cur_info < end_info) {
543 size_t size = cur_info->ByteSize();
544 uintptr_t address = GetAddressForAllocationInfo(cur_info);
545 if (cur_info->IsFree()) {
546 os << "Free block at address: " << reinterpret_cast<const void*>(address)
547 << " of length " << size << " bytes\n";
548 } else {
549 os << "Large object at address: " << reinterpret_cast<const void*>(address)
550 << " of length " << size << " bytes\n";
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700551 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700552 cur_info = cur_info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700553 }
554 if (free_end_) {
555 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
556 << " of length " << free_end_ << " bytes\n";
557 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700558}
559
Mathieu Chartiere7158112015-06-03 13:32:15 -0700560bool FreeListSpace::IsZygoteLargeObject(Thread* self ATTRIBUTE_UNUSED, mirror::Object* obj) const {
561 const AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
562 DCHECK(info != nullptr);
563 return info->IsZygoteObject();
564}
565
566void FreeListSpace::SetAllLargeObjectsAsZygoteObjects(Thread* self) {
567 MutexLock mu(self, lock_);
568 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
569 for (AllocationInfo* cur_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin())),
570 *end_info = GetAllocationInfoForAddress(free_end_start); cur_info < end_info;
571 cur_info = cur_info->GetNextInfo()) {
572 if (!cur_info->IsFree()) {
573 cur_info->SetZygoteObject();
574 }
575 }
576}
577
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700578void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
579 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
580 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
581 Thread* self = context->self;
582 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
583 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
584 // the bitmaps as an optimization.
585 if (!context->swap_bitmaps) {
586 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
587 for (size_t i = 0; i < num_ptrs; ++i) {
588 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800589 }
590 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700591 context->freed.objects += num_ptrs;
592 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700593}
594
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700595collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700596 if (Begin() >= End()) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700597 return collector::ObjectBytePair(0, 0);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700598 }
599 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
600 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
601 if (swap_bitmaps) {
602 std::swap(live_bitmap, mark_bitmap);
603 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700604 AllocSpace::SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700605 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
606 reinterpret_cast<uintptr_t>(Begin()),
607 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700608 return scc.freed;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800609}
610
Mathieu Chartierb363f662014-07-16 13:28:58 -0700611void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/,
612 size_t /*failed_alloc_bytes*/) {
613 UNIMPLEMENTED(FATAL);
614}
615
Ian Rogers1d54e732013-05-02 21:10:01 -0700616} // namespace space
617} // namespace gc
618} // namespace art