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