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