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