Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | #include "bump_pointer_space.h" |
| 18 | #include "bump_pointer_space-inl.h" |
| 19 | #include "mirror/object-inl.h" |
| 20 | #include "mirror/class-inl.h" |
| 21 | |
| 22 | namespace art { |
| 23 | namespace gc { |
| 24 | namespace space { |
| 25 | |
| 26 | BumpPointerSpace* BumpPointerSpace::Create(const std::string& name, size_t capacity, |
| 27 | byte* requested_begin) { |
| 28 | capacity = RoundUp(capacity, kPageSize); |
| 29 | std::string error_msg; |
| 30 | UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity, |
| 31 | PROT_READ | PROT_WRITE, &error_msg)); |
| 32 | if (mem_map.get() == nullptr) { |
| 33 | LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size " |
| 34 | << PrettySize(capacity) << " with message " << error_msg; |
| 35 | return nullptr; |
| 36 | } |
| 37 | return new BumpPointerSpace(name, mem_map.release()); |
| 38 | } |
| 39 | |
| 40 | BumpPointerSpace::BumpPointerSpace(const std::string& name, byte* begin, byte* limit) |
| 41 | : ContinuousMemMapAllocSpace(name, nullptr, begin, begin, limit, |
| 42 | kGcRetentionPolicyAlwaysCollect), |
| 43 | num_objects_allocated_(0), total_bytes_allocated_(0), total_objects_allocated_(0), |
| 44 | growth_end_(limit) { |
| 45 | } |
| 46 | |
| 47 | BumpPointerSpace::BumpPointerSpace(const std::string& name, MemMap* mem_map) |
| 48 | : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->Begin(), mem_map->End(), |
| 49 | kGcRetentionPolicyAlwaysCollect), |
| 50 | num_objects_allocated_(0), total_bytes_allocated_(0), total_objects_allocated_(0), |
| 51 | growth_end_(mem_map->End()) { |
| 52 | } |
| 53 | |
| 54 | mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated) { |
| 55 | mirror::Object* ret = AllocNonvirtual(num_bytes); |
| 56 | if (LIKELY(ret != nullptr)) { |
| 57 | *bytes_allocated = num_bytes; |
| 58 | } |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | size_t BumpPointerSpace::AllocationSize(const mirror::Object* obj) { |
| 63 | return AllocationSizeNonvirtual(obj); |
| 64 | } |
| 65 | |
| 66 | void BumpPointerSpace::Clear() { |
| 67 | // Release the pages back to the operating system. |
| 68 | CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed"; |
| 69 | // Reset the end of the space back to the beginning, we move the end forward as we allocate |
| 70 | // objects. |
| 71 | SetEnd(Begin()); |
| 72 | growth_end_ = Limit(); |
| 73 | num_objects_allocated_ = 0; |
| 74 | } |
| 75 | |
| 76 | void BumpPointerSpace::Dump(std::ostream& os) const { |
| 77 | os << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - " |
| 78 | << reinterpret_cast<void*>(Limit()); |
| 79 | } |
| 80 | |
| 81 | mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) { |
| 82 | const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf(); |
| 83 | return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment)); |
| 84 | } |
| 85 | |
| 86 | } // namespace space |
| 87 | } // namespace gc |
| 88 | } // namespace art |