1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "runtime/mem/gc/bitmap.h"
17
18 #include <atomic>
19
20 namespace panda::mem {
21
ClearBitsInRange(size_t begin,size_t end)22 void Bitmap::ClearBitsInRange(size_t begin, size_t end)
23 {
24 CheckBitRange(begin, end);
25 if (GetWordIdx(end) == GetWordIdx(begin)) { // [begin, end] in the same word
26 ClearRangeWithinWord(begin, end);
27 return;
28 }
29
30 auto begin_roundup = RoundUp(begin, BITSPERWORD);
31 auto fn_rounddown = [](BitmapWordType val) -> BitmapWordType {
32 constexpr BitmapWordType MASK = ~((static_cast<BitmapWordType>(1) << LOG_BITSPERWORD) - 1);
33 return val & MASK;
34 };
35 auto end_rounddown = fn_rounddown(end);
36 ClearRangeWithinWord(begin, begin_roundup);
37 ClearWords(GetWordIdx(begin_roundup), GetWordIdx(end_rounddown));
38 ClearRangeWithinWord(end_rounddown, end);
39 }
40
AtomicTestAndSetBit(size_t bit_offset)41 bool Bitmap::AtomicTestAndSetBit(size_t bit_offset)
42 {
43 CheckBitOffset(bit_offset);
44 auto word_idx = GetWordIdx(bit_offset);
45 auto *word_addr = reinterpret_cast<std::atomic<BitmapWordType> *>(&bitmap_[word_idx]);
46 auto mask = GetBitMask(bit_offset);
47 BitmapWordType old_word;
48 do {
49 // Atomic with acquire order reason: data race with word_addr with dependecies on reads after the load which
50 // should become visible
51 old_word = word_addr->load(std::memory_order_acquire);
52 if ((old_word & mask) != 0) {
53 return true;
54 }
55 } while (!word_addr->compare_exchange_weak(old_word, old_word | mask, std::memory_order_seq_cst));
56 return false;
57 }
58
AtomicTestAndClearBit(size_t bit_offset)59 bool Bitmap::AtomicTestAndClearBit(size_t bit_offset)
60 {
61 CheckBitOffset(bit_offset);
62 auto word_idx = GetWordIdx(bit_offset);
63 auto *word_addr = reinterpret_cast<std::atomic<BitmapWordType> *>(&bitmap_[word_idx]);
64 auto mask = GetBitMask(bit_offset);
65 BitmapWordType old_word;
66 do {
67 // Atomic with acquire order reason: data race with word_addr with dependecies on reads after the load which
68 // should become visible
69 old_word = word_addr->load(std::memory_order_acquire);
70 if ((old_word & mask) == 0) {
71 return false;
72 }
73 } while (!word_addr->compare_exchange_weak(old_word, old_word & (~mask), std::memory_order_seq_cst));
74 return true;
75 }
76
AtomicTestBit(size_t bit_offset)77 bool Bitmap::AtomicTestBit(size_t bit_offset)
78 {
79 CheckBitOffset(bit_offset);
80 auto word_idx = GetWordIdx(bit_offset);
81 auto *word_addr = reinterpret_cast<std::atomic<BitmapWordType> *>(&bitmap_[word_idx]);
82 auto mask = GetBitMask(bit_offset);
83 // Atomic with acquire order reason: data race with word_addr with dependecies on reads after the load which should
84 // become visible
85 BitmapWordType word = word_addr->load(std::memory_order_acquire);
86 return (word & mask) != 0;
87 }
88
89 } // namespace panda::mem
90