14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_REMEMBERED_SET_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_REMEMBERED_SET_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/mem/gc_bitset.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript {
224514f5e3Sopenharmony_ciclass RememberedSet {
234514f5e3Sopenharmony_cipublic:
244514f5e3Sopenharmony_ci    static constexpr size_t GCBITSET_DATA_OFFSET = sizeof(size_t);
254514f5e3Sopenharmony_ci    explicit RememberedSet(size_t size) : size_(size) {}
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(RememberedSet);
284514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(RememberedSet);
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ci    GCBitset *GCBitsetData()
314514f5e3Sopenharmony_ci    {
324514f5e3Sopenharmony_ci        return reinterpret_cast<GCBitset *>(reinterpret_cast<uintptr_t>(this) + GCBITSET_DATA_OFFSET);
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_ci    const GCBitset *GCBitsetData() const
364514f5e3Sopenharmony_ci    {
374514f5e3Sopenharmony_ci        return reinterpret_cast<GCBitset *>(reinterpret_cast<uintptr_t>(this) + GCBITSET_DATA_OFFSET);
384514f5e3Sopenharmony_ci    }
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci    void ClearAll()
414514f5e3Sopenharmony_ci    {
424514f5e3Sopenharmony_ci        GCBitsetData()->Clear(size_);
434514f5e3Sopenharmony_ci    }
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    bool Insert(uintptr_t begin, uintptr_t addr)
464514f5e3Sopenharmony_ci    {
474514f5e3Sopenharmony_ci        return GCBitsetData()->SetBit<AccessType::NON_ATOMIC>((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
484514f5e3Sopenharmony_ci    }
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ci    bool InsertRange(uintptr_t begin, uintptr_t addr, uint32_t mask)
514514f5e3Sopenharmony_ci    {
524514f5e3Sopenharmony_ci        return GCBitsetData()->SetBitRange((addr - begin) >> TAGGED_TYPE_SIZE_LOG, mask);
534514f5e3Sopenharmony_ci    }
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    bool AtomicInsert(uintptr_t begin, uintptr_t addr)
564514f5e3Sopenharmony_ci    {
574514f5e3Sopenharmony_ci        return GCBitsetData()->SetBit<AccessType::ATOMIC>((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
584514f5e3Sopenharmony_ci    }
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    void ClearBit(uintptr_t begin, uintptr_t addr)
614514f5e3Sopenharmony_ci    {
624514f5e3Sopenharmony_ci        GCBitsetData()->ClearBit((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
634514f5e3Sopenharmony_ci    }
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    void ClearRange(uintptr_t begin, uintptr_t start, uintptr_t end)
664514f5e3Sopenharmony_ci    {
674514f5e3Sopenharmony_ci        GCBitsetData()->ClearBitRange<AccessType::NON_ATOMIC>(
684514f5e3Sopenharmony_ci            (start - begin) >> TAGGED_TYPE_SIZE_LOG, (end - begin) >> TAGGED_TYPE_SIZE_LOG);
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    void AtomicClearRange(uintptr_t begin, uintptr_t start, uintptr_t end)
724514f5e3Sopenharmony_ci    {
734514f5e3Sopenharmony_ci        GCBitsetData()->ClearBitRange<AccessType::ATOMIC>(
744514f5e3Sopenharmony_ci            (start - begin) >> TAGGED_TYPE_SIZE_LOG, (end - begin) >> TAGGED_TYPE_SIZE_LOG);
754514f5e3Sopenharmony_ci    }
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    bool TestBit(uintptr_t begin, uintptr_t addr) const
784514f5e3Sopenharmony_ci    {
794514f5e3Sopenharmony_ci        return GCBitsetData()->TestBit((addr - begin) >> TAGGED_TYPE_SIZE_LOG);
804514f5e3Sopenharmony_ci    }
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci    template <typename Visitor>
834514f5e3Sopenharmony_ci    void IterateAllMarkedBits(uintptr_t begin, Visitor visitor)
844514f5e3Sopenharmony_ci    {
854514f5e3Sopenharmony_ci        GCBitsetData()->IterateMarkedBits<Visitor, AccessType::NON_ATOMIC>(begin, size_, visitor);
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    template <typename Visitor>
894514f5e3Sopenharmony_ci    void AtomicIterateAllMarkedBits(uintptr_t begin, Visitor visitor)
904514f5e3Sopenharmony_ci    {
914514f5e3Sopenharmony_ci        GCBitsetData()->IterateMarkedBits<Visitor, AccessType::ATOMIC>(begin, size_, visitor);
924514f5e3Sopenharmony_ci    }
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    template <typename Visitor>
954514f5e3Sopenharmony_ci    void IterateAllMarkedBitsConst(uintptr_t begin, Visitor visitor) const
964514f5e3Sopenharmony_ci    {
974514f5e3Sopenharmony_ci        GCBitsetData()->IterateMarkedBitsConst(begin, size_, visitor);
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    void Merge(RememberedSet *rset)
1014514f5e3Sopenharmony_ci    {
1024514f5e3Sopenharmony_ci        GCBitset *bitset = rset->GCBitsetData();
1034514f5e3Sopenharmony_ci        GCBitsetData()->Merge(bitset, size_);
1044514f5e3Sopenharmony_ci    }
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    size_t Size() const
1074514f5e3Sopenharmony_ci    {
1084514f5e3Sopenharmony_ci        return size_ + GCBITSET_DATA_OFFSET;
1094514f5e3Sopenharmony_ci    }
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ciprivate:
1124514f5e3Sopenharmony_ci    size_t size_;
1134514f5e3Sopenharmony_ci};
1144514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1154514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_REMEMBERED_SET_H
116