1/*
2 * Copyright (c) 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#ifndef ECMASCRIPT_MEM_REMEMBERED_SET_H
17#define ECMASCRIPT_MEM_REMEMBERED_SET_H
18
19#include "ecmascript/mem/gc_bitset.h"
20
21namespace panda::ecmascript {
22class RememberedSet {
23public:
24    static constexpr size_t GCBITSET_DATA_OFFSET = sizeof(size_t);
25    explicit RememberedSet(size_t size) : size_(size) {}
26
27    NO_COPY_SEMANTIC(RememberedSet);
28    NO_MOVE_SEMANTIC(RememberedSet);
29
30    GCBitset *GCBitsetData()
31    {
32        return reinterpret_cast<GCBitset *>(reinterpret_cast<uintptr_t>(this) + GCBITSET_DATA_OFFSET);
33    }
34
35    const GCBitset *GCBitsetData() const
36    {
37        return reinterpret_cast<GCBitset *>(reinterpret_cast<uintptr_t>(this) + GCBITSET_DATA_OFFSET);
38    }
39
40    void ClearAll()
41    {
42        GCBitsetData()->Clear(size_);
43    }
44
45    bool Insert(uintptr_t begin, uintptr_t addr)
46    {
47        return GCBitsetData()->SetBit<AccessType::NON_ATOMIC>((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
48    }
49
50    bool InsertRange(uintptr_t begin, uintptr_t addr, uint32_t mask)
51    {
52        return GCBitsetData()->SetBitRange((addr - begin) >> TAGGED_TYPE_SIZE_LOG, mask);
53    }
54
55    bool AtomicInsert(uintptr_t begin, uintptr_t addr)
56    {
57        return GCBitsetData()->SetBit<AccessType::ATOMIC>((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
58    }
59
60    void ClearBit(uintptr_t begin, uintptr_t addr)
61    {
62        GCBitsetData()->ClearBit((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
63    }
64
65    void ClearRange(uintptr_t begin, uintptr_t start, uintptr_t end)
66    {
67        GCBitsetData()->ClearBitRange<AccessType::NON_ATOMIC>(
68            (start - begin) >> TAGGED_TYPE_SIZE_LOG, (end - begin) >> TAGGED_TYPE_SIZE_LOG);
69    }
70
71    void AtomicClearRange(uintptr_t begin, uintptr_t start, uintptr_t end)
72    {
73        GCBitsetData()->ClearBitRange<AccessType::ATOMIC>(
74            (start - begin) >> TAGGED_TYPE_SIZE_LOG, (end - begin) >> TAGGED_TYPE_SIZE_LOG);
75    }
76
77    bool TestBit(uintptr_t begin, uintptr_t addr) const
78    {
79        return GCBitsetData()->TestBit((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
80    }
81
82    template <typename Visitor>
83    void IterateAllMarkedBits(uintptr_t begin, Visitor visitor)
84    {
85        GCBitsetData()->IterateMarkedBits<Visitor, AccessType::NON_ATOMIC>(begin, size_, visitor);
86    }
87
88    template <typename Visitor>
89    void AtomicIterateAllMarkedBits(uintptr_t begin, Visitor visitor)
90    {
91        GCBitsetData()->IterateMarkedBits<Visitor, AccessType::ATOMIC>(begin, size_, visitor);
92    }
93
94    template <typename Visitor>
95    void IterateAllMarkedBitsConst(uintptr_t begin, Visitor visitor) const
96    {
97        GCBitsetData()->IterateMarkedBitsConst(begin, size_, visitor);
98    }
99
100    void Merge(RememberedSet *rset)
101    {
102        GCBitset *bitset = rset->GCBitsetData();
103        GCBitsetData()->Merge(bitset, size_);
104    }
105
106    size_t Size() const
107    {
108        return size_ + GCBITSET_DATA_OFFSET;
109    }
110
111private:
112    size_t size_;
113};
114}  // namespace panda::ecmascript
115#endif  // ECMASCRIPT_MEM_REMEMBERED_SET_H
116