1 /* 2 * Copyright (C) 2015 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 "bitmap-inl.h" 18 19 #include "base/bit_utils.h" 20 #include "card_table.h" 21 #include "mem_map.h" 22 23 namespace art { 24 namespace gc { 25 namespace accounting { 26 27 Bitmap* Bitmap::CreateFromMemMap(MemMap* mem_map, size_t num_bits) { 28 CHECK(mem_map != nullptr); 29 return new Bitmap(mem_map, num_bits); 30 } 31 32 Bitmap::Bitmap(MemMap* mem_map, size_t bitmap_size) 33 : mem_map_(mem_map), bitmap_begin_(reinterpret_cast<uintptr_t*>(mem_map->Begin())), 34 bitmap_size_(bitmap_size) { 35 CHECK(bitmap_begin_ != nullptr); 36 CHECK_NE(bitmap_size, 0U); 37 } 38 39 Bitmap::~Bitmap() { 40 // Destroys MemMap via std::unique_ptr<>. 41 } 42 43 MemMap* Bitmap::AllocateMemMap(const std::string& name, size_t num_bits) { 44 const size_t bitmap_size = RoundUp( 45 RoundUp(num_bits, kBitsPerBitmapWord) / kBitsPerBitmapWord * sizeof(uintptr_t), kPageSize); 46 std::string error_msg; 47 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size, 48 PROT_READ | PROT_WRITE, false, false, 49 &error_msg)); 50 if (UNLIKELY(mem_map.get() == nullptr)) { 51 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg; 52 return nullptr; 53 } 54 return mem_map.release(); 55 } 56 57 Bitmap* Bitmap::Create(const std::string& name, size_t num_bits) { 58 auto* const mem_map = AllocateMemMap(name, num_bits); 59 if (mem_map == nullptr) { 60 return nullptr; 61 } 62 return CreateFromMemMap(mem_map, num_bits); 63 } 64 65 void Bitmap::Clear() { 66 if (bitmap_begin_ != nullptr) { 67 mem_map_->MadviseDontNeedAndZero(); 68 } 69 } 70 71 void Bitmap::CopyFrom(Bitmap* source_bitmap) { 72 DCHECK_EQ(BitmapSize(), source_bitmap->BitmapSize()); 73 std::copy(source_bitmap->Begin(), 74 source_bitmap->Begin() + BitmapSize() / kBitsPerBitmapWord, Begin()); 75 } 76 77 template<size_t kAlignment> 78 MemoryRangeBitmap<kAlignment>* MemoryRangeBitmap<kAlignment>::Create( 79 const std::string& name, uintptr_t cover_begin, uintptr_t cover_end) { 80 CHECK_ALIGNED(cover_begin, kAlignment); 81 CHECK_ALIGNED(cover_end, kAlignment); 82 const size_t num_bits = (cover_end - cover_begin) / kAlignment; 83 auto* const mem_map = Bitmap::AllocateMemMap(name, num_bits); 84 return CreateFromMemMap(mem_map, cover_begin, num_bits); 85 } 86 87 template<size_t kAlignment> 88 MemoryRangeBitmap<kAlignment>* MemoryRangeBitmap<kAlignment>::CreateFromMemMap( 89 MemMap* mem_map, uintptr_t begin, size_t num_bits) { 90 return new MemoryRangeBitmap(mem_map, begin, num_bits); 91 } 92 93 template class MemoryRangeBitmap<CardTable::kCardSize>; 94 95 } // namespace accounting 96 } // namespace gc 97 } // namespace art 98 99