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