blob: f7d776fbfb23c921ee68889967f66eec70c2593f [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080020#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070021#include "UniquePtr.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070022#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070023#include "os.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "thread.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070025#include "utils.h"
26
27namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070028namespace gc {
29namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030
31void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080032 live_objects_.swap(mark_objects_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070033 // Swap names to get more descriptive diagnostics.
34 std::string temp_name = live_objects_->GetName();
35 live_objects_->SetName(mark_objects_->GetName());
36 mark_objects_->SetName(temp_name);
37}
38
39LargeObjectSpace::LargeObjectSpace(const std::string& name)
40 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
41 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
42 total_objects_allocated_(0) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070043}
44
45
46void LargeObjectSpace::CopyLiveToMarked() {
47 mark_objects_->CopyFrom(*live_objects_.get());
48}
49
50LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
51 : LargeObjectSpace(name),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070052 lock_("large object map space lock", kAllocSpaceLock) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070053
54}
55
56LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
57 return new LargeObjectMapSpace(name);
58}
59
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes) {
Ian Rogersa40307e2013-02-22 11:32:44 -080061 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", NULL, num_bytes,
62 PROT_READ | PROT_WRITE);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070063 if (mem_map == NULL) {
64 return NULL;
65 }
66 MutexLock mu(self, lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070068 large_objects_.push_back(obj);
69 mem_maps_.Put(obj, mem_map);
70 size_t allocation_size = mem_map->Size();
71 num_bytes_allocated_ += allocation_size;
72 total_bytes_allocated_ += allocation_size;
73 ++num_objects_allocated_;
74 ++total_objects_allocated_;
75 return obj;
76}
77
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070079 MutexLock mu(self, lock_);
80 MemMaps::iterator found = mem_maps_.find(ptr);
81 CHECK(found != mem_maps_.end()) << "Attempted to free large object which was not live";
82 DCHECK_GE(num_bytes_allocated_, found->second->Size());
83 size_t allocation_size = found->second->Size();
84 num_bytes_allocated_ -= allocation_size;
85 --num_objects_allocated_;
86 delete found->second;
87 mem_maps_.erase(found);
88 return allocation_size;
89}
90
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091size_t LargeObjectMapSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070092 MutexLock mu(Thread::Current(), lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 MemMaps::iterator found = mem_maps_.find(const_cast<mirror::Object*>(obj));
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070094 CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live";
95 return found->second->Size();
96}
97
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070099 size_t total = 0;
100 for (size_t i = 0; i < num_ptrs; ++i) {
101 if (kDebugSpaces) {
102 CHECK(Contains(ptrs[i]));
103 }
104 total += Free(self, ptrs[i]);
105 }
106 return total;
107}
108
109void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
110 MutexLock mu(Thread::Current(), lock_);
111 for (MemMaps::iterator it = mem_maps_.begin(); it != mem_maps_.end(); ++it) {
112 MemMap* mem_map = it->second;
113 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
114 callback(NULL, NULL, 0, arg);
115 }
116}
117
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700119 Thread* self = Thread::Current();
120 if (lock_.IsExclusiveHeld(self)) {
121 // We hold lock_ so do the check.
122 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
123 } else {
124 MutexLock mu(self, lock_);
125 return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end();
126 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700127}
128
129FreeListSpace* FreeListSpace::Create(const std::string& name, byte* requested_begin, size_t size) {
130 CHECK(size % kAlignment == 0);
131 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
132 PROT_READ | PROT_WRITE);
133 CHECK(mem_map != NULL) << "Failed to allocate large object space mem map";
134 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
135}
136
137FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end)
138 : LargeObjectSpace(name),
139 begin_(begin),
140 end_(end),
141 mem_map_(mem_map),
142 lock_("free list space lock", kAllocSpaceLock) {
143 chunks_.resize(Size() / kAlignment + 1);
144 // Add a dummy chunk so we don't need to handle chunks having no next chunk.
145 chunks_.back().SetSize(kAlignment, false);
146 // Start out with one large free chunk.
147 AddFreeChunk(begin_, end_ - begin_, NULL);
148}
149
150FreeListSpace::~FreeListSpace() {
151
152}
153
154void FreeListSpace::AddFreeChunk(void* address, size_t size, Chunk* previous) {
155 Chunk* chunk = ChunkFromAddr(address);
156 chunk->SetSize(size, true);
157 chunk->SetPrevious(previous);
158 Chunk* next_chunk = GetNextChunk(chunk);
159 next_chunk->SetPrevious(chunk);
160 free_chunks_.insert(chunk);
161}
162
163FreeListSpace::Chunk* FreeListSpace::ChunkFromAddr(void* address) {
164 size_t offset = reinterpret_cast<byte*>(address) - Begin();
165 DCHECK(IsAligned<kAlignment>(offset));
166 DCHECK_LT(offset, Size());
167 return &chunks_[offset / kAlignment];
168}
169
170void* FreeListSpace::AddrFromChunk(Chunk* chunk) {
171 return reinterpret_cast<void*>(Begin() + (chunk - &chunks_.front()) * kAlignment);
172}
173
174void FreeListSpace::RemoveFreeChunk(Chunk* chunk) {
175 // TODO: C++0x
176 // TODO: Improve performance, this might be slow.
177 std::pair<FreeChunks::iterator, FreeChunks::iterator> range = free_chunks_.equal_range(chunk);
178 for (FreeChunks::iterator it = range.first; it != range.second; ++it) {
179 if (*it == chunk) {
180 free_chunks_.erase(it);
181 return;
182 }
183 }
184}
185
186void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
187 MutexLock mu(Thread::Current(), lock_);
188 for (Chunk* chunk = &chunks_.front(); chunk < &chunks_.back(); ) {
189 if (!chunk->IsFree()) {
190 size_t size = chunk->GetSize();
191 void* begin = AddrFromChunk(chunk);
192 void* end = reinterpret_cast<void*>(reinterpret_cast<byte*>(begin) + size);
193 callback(begin, end, size, arg);
194 callback(NULL, NULL, 0, arg);
195 }
196 chunk = GetNextChunk(chunk);
197 }
198}
199
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700201 MutexLock mu(self, lock_);
202 CHECK(Contains(obj));
203 // Check adjacent chunks to see if we need to combine.
204 Chunk* chunk = ChunkFromAddr(obj);
205 CHECK(!chunk->IsFree());
206
207 size_t allocation_size = chunk->GetSize();
Ian Rogers22a20862013-03-16 16:34:57 -0700208 if (kIsDebugBuild) {
209 memset(obj, 0xEB, allocation_size);
210 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700211 madvise(obj, allocation_size, MADV_DONTNEED);
212 num_objects_allocated_--;
213 num_bytes_allocated_ -= allocation_size;
214 Chunk* prev = chunk->GetPrevious();
215 Chunk* next = GetNextChunk(chunk);
216
217 // Combine any adjacent free chunks
218 size_t extra_size = chunk->GetSize();
219 if (next->IsFree()) {
220 extra_size += next->GetSize();
221 RemoveFreeChunk(next);
222 }
223 if (prev != NULL && prev->IsFree()) {
224 RemoveFreeChunk(prev);
225 AddFreeChunk(AddrFromChunk(prev), prev->GetSize() + extra_size, prev->GetPrevious());
226 } else {
227 AddFreeChunk(AddrFromChunk(chunk), extra_size, prev);
228 }
229 return allocation_size;
230}
231
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232bool FreeListSpace::Contains(const mirror::Object* obj) const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700233 return mem_map_->HasAddress(obj);
234}
235
236FreeListSpace::Chunk* FreeListSpace::GetNextChunk(Chunk* chunk) {
237 return chunk + chunk->GetSize() / kAlignment;
238}
239
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240size_t FreeListSpace::AllocationSize(const mirror::Object* obj) {
241 Chunk* chunk = ChunkFromAddr(const_cast<mirror::Object*>(obj));
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700242 CHECK(!chunk->IsFree());
243 return chunk->GetSize();
244}
245
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700247 MutexLock mu(self, lock_);
248 num_bytes = RoundUp(num_bytes, kAlignment);
249 Chunk temp;
250 temp.SetSize(num_bytes);
251 // Find the smallest chunk at least num_bytes in size.
252 FreeChunks::iterator found = free_chunks_.lower_bound(&temp);
253 if (found == free_chunks_.end()) {
254 // Out of memory, or too much fragmentation.
255 return NULL;
256 }
257 Chunk* chunk = *found;
258 free_chunks_.erase(found);
259 CHECK(chunk->IsFree());
260 void* addr = AddrFromChunk(chunk);
261 size_t chunk_size = chunk->GetSize();
262 chunk->SetSize(num_bytes);
263 if (chunk_size > num_bytes) {
264 // Split the chunk into two chunks.
265 Chunk* new_chunk = GetNextChunk(chunk);
266 AddFreeChunk(AddrFromChunk(new_chunk), chunk_size - num_bytes, chunk);
267 }
268
269 num_objects_allocated_++;
270 total_objects_allocated_++;
271 num_bytes_allocated_ += num_bytes;
272 total_bytes_allocated_ += num_bytes;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 return reinterpret_cast<mirror::Object*>(addr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700274}
275
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700276void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700277 os << GetName() << " -"
278 << " begin: " << reinterpret_cast<void*>(Begin())
279 << " end: " << reinterpret_cast<void*>(End());
280}
281
Ian Rogers1d54e732013-05-02 21:10:01 -0700282} // namespace space
283} // namespace gc
284} // namespace art