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