blob: fdded028e160c468e26d28e853bfc72051213c50 [file] [log] [blame]
Mathieu Chartier4858a932015-01-23 13:18:53 -08001/*
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
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Mathieu Chartier4858a932015-01-23 13:18:53 -080020#include "card_table.h"
21#include "mem_map.h"
22
23namespace art {
24namespace gc {
25namespace accounting {
26
27Bitmap* Bitmap::CreateFromMemMap(MemMap* mem_map, size_t num_bits) {
28 CHECK(mem_map != nullptr);
29 return new Bitmap(mem_map, num_bits);
30}
31
32Bitmap::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
Vladimir Marko3481ba22015-04-13 12:22:36 +010039Bitmap::~Bitmap() {
40 // Destroys MemMap via std::unique_ptr<>.
41}
42
Mathieu Chartier4858a932015-01-23 13:18:53 -080043MemMap* 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,
Vladimir Marko5c42c292015-02-25 12:02:49 +000048 PROT_READ | PROT_WRITE, false, false,
49 &error_msg));
Mathieu Chartier4858a932015-01-23 13:18:53 -080050 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
57Bitmap* 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
65void Bitmap::Clear() {
66 if (bitmap_begin_ != nullptr) {
67 mem_map_->MadviseDontNeedAndZero();
68 }
69}
70
71void 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
77template<size_t kAlignment>
78MemoryRangeBitmap<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
87template<size_t kAlignment>
88MemoryRangeBitmap<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
93template class MemoryRangeBitmap<CardTable::kCardSize>;
94
95} // namespace accounting
96} // namespace gc
97} // namespace art
98