/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_MEM_VISITOR_H #define ECMASCRIPT_MEM_VISITOR_H #include #include #include namespace panda::ecmascript { enum class Root { ROOT_FRAME, ROOT_HANDLE, ROOT_VM, ROOT_STRING, ROOT_INTERNAL_CALL_PARAMS, }; enum class VisitObjectArea { NORMAL, NATIVE_POINTER, IN_OBJECT, RAW_DATA }; enum class VisitType : size_t { SEMI_GC_VISIT, OLD_GC_VISIT, SNAPSHOT_VISIT, ALL_VISIT }; using RootVisitor = std::function; using RootRangeVisitor = std::function; using RootBaseAndDerivedVisitor = std::function; using EcmaObjectRangeVisitor = std::function; using WeakRootVisitor = std::function; template class PrimitiveObjectBodyIterator { public: static inline void IterateBody(TaggedObject *root, const EcmaObjectRangeVisitor& visitor) { if (visitType == VisitType::ALL_VISIT) { size_t hclassEnd = sizeof(JSTaggedType); visitor(root, ObjectSlot(ToUintPtr(root)), ObjectSlot(ToUintPtr(root) + hclassEnd), VisitObjectArea::NORMAL); if (size > hclassEnd) { visitor(root, ObjectSlot(ToUintPtr(root) + hclassEnd), ObjectSlot(ToUintPtr(root) + size), VisitObjectArea::RAW_DATA); } } } }; template class ObjectBodyIterator { public: template static inline void IterateBody(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t startSize) { if (visitType == VisitType::ALL_VISIT) { if (visitHClass) { IterateHClass(root, visitor); } IterateBefore(root, visitor, startSize); } visitor(root, ObjectSlot(ToUintPtr(root) + startOffset), ObjectSlot(ToUintPtr(root) + endOffset), area); if (visitType == VisitType::ALL_VISIT) { IterateAfter(root, visitor); } } static inline void IterateRefBody(TaggedObject *root, const EcmaObjectRangeVisitor& visitor) { IterateBody(root, visitor, sizeof(JSTaggedType)); } static inline void IterateNativeBody(TaggedObject *root, const EcmaObjectRangeVisitor& visitor) { IterateBody(root, visitor, sizeof(JSTaggedType)); } static inline void IterateRefBody(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t parentSize) { IterateBody(root, visitor, parentSize); } static inline void IterateHClass(TaggedObject *root, const EcmaObjectRangeVisitor& visitor) { size_t hclassEnd = sizeof(JSTaggedType); visitor(root, ObjectSlot(ToUintPtr(root)), ObjectSlot(ToUintPtr(root) + hclassEnd), VisitObjectArea::NORMAL); } static inline void IterateBefore(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t startSize) { if (startOffset > startSize) { IteratorRange(root, visitor, startSize, startOffset); } } static inline void IterateAfter(TaggedObject *root, const EcmaObjectRangeVisitor& visitor) { if (size > endOffset) { IteratorRange(root, visitor, endOffset, size); } } static inline void IteratorRange(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t start, size_t end) { visitor(root, ObjectSlot(ToUintPtr(root) + start), ObjectSlot(ToUintPtr(root) + end), VisitObjectArea::RAW_DATA); } }; template class ArrayBodyIterator { public: static inline void IterateBody(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t refLength, size_t length) { if (visitType == VisitType::ALL_VISIT) { IterateBefore(root, visitor); } if (LIKELY(refLength != 0)) { size_t endOffset = startOffset + refLength * JSTaggedValue::TaggedTypeSize(); visitor(root, ObjectSlot(ToUintPtr(root) + startOffset), ObjectSlot(ToUintPtr(root) + endOffset), VisitObjectArea::NORMAL); } if (visitType == VisitType::ALL_VISIT) { IterateAfter(root, visitor, refLength, length); } } static inline void IterateBefore(TaggedObject *root, const EcmaObjectRangeVisitor& visitor) { size_t hclassEnd = sizeof(JSTaggedType); ASSERT(startOffset > hclassEnd); visitor(root, ObjectSlot(ToUintPtr(root)), ObjectSlot(ToUintPtr(root) + hclassEnd), VisitObjectArea::NORMAL); IteratorRange(root, visitor, hclassEnd, startOffset); } static inline void IterateAfter(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t refLength, size_t length) { if (length > refLength) { size_t endOffset = startOffset + refLength * JSTaggedValue::TaggedTypeSize(); size_t size = startOffset + length * JSTaggedValue::TaggedTypeSize(); IteratorRange(root, visitor, endOffset, size); } } static inline void IteratorRange(TaggedObject *root, const EcmaObjectRangeVisitor& visitor, size_t start, size_t end) { visitor(root, ObjectSlot(ToUintPtr(root) + start), ObjectSlot(ToUintPtr(root) + end), VisitObjectArea::RAW_DATA); } }; } // namespace panda::ecmascript #endif // ECMASCRIPT_MEM_VISITOR_H