1 /** 2 * Copyright (c) 2024-2025 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 PANDA_RUNTIME_MEM_NEW_OBJECT_HELPERS_H 17 #define PANDA_RUNTIME_MEM_NEW_OBJECT_HELPERS_H 18 19 #include "runtime/include/coretypes/array.h" 20 #include "runtime/include/coretypes/dyn_objects.h" 21 #include "runtime/include/class.h" 22 #include "runtime/include/hclass.h" 23 24 namespace ark::mem { 25 template <LangTypeT LANG_TYPE> 26 class ObjectIterator; 27 28 /// Provides functionality to iterate through references in object for static languages 29 template <> 30 class ObjectIterator<LANG_TYPE_STATIC> { 31 public: 32 /// Iterates references in object and passes pointers of them to the handler 33 template <bool INTERRUPTIBLE, typename Handler> 34 static bool Iterate(ObjectHeader *obj, Handler *handler, void *begin, void *end); 35 36 template <typename Handler> 37 static bool IterateAndDiscoverReferences(GC *gc, ObjectHeader *obj, Handler *handler); 38 39 template <typename Handler> 40 static bool IterateAndDiscoverReferences(GC *gc, ObjectHeader *obj, Handler *handler, void *begin, void *end); 41 42 template <bool INTERRUPTIBLE, typename Handler> 43 static bool Iterate(Class *cls, ObjectHeader *obj, Handler *handler); 44 45 private: 46 template <bool INTERRUPTIBLE, typename Handler> 47 static bool Iterate(Class *cls, ObjectHeader *obj, Handler *handler, void *begin, void *end); 48 49 template <bool INTERRUPTIBLE, typename Handler> 50 static bool IterateObjectReferences(ObjectHeader *object, Class *objClass, Handler *handler); 51 52 template <bool INTERRUPTIBLE, typename Handler> 53 static bool IterateObjectReferences(ObjectHeader *object, Class *cls, Handler *handler, void *begin, void *end); 54 55 template <bool INTERRUPTIBLE, typename Handler> 56 static bool IterateClassReferences(Class *cls, Handler *handler); 57 58 template <bool INTERRUPTIBLE, typename Handler> 59 static bool IterateClassReferences(Class *cls, Handler *handler, void *begin, void *end); 60 61 template <bool INTERRUPTIBLE, typename Handler> 62 static bool IterateRange(ObjectPointerType *refStart, const ObjectPointerType *refEnd, Handler *handler); 63 64 template <bool INTERRUPTIBLE, typename Handler> 65 static bool IterateRange(ObjectPointerType *refStart, ObjectPointerType *refEnd, Handler *handler, void *begin, 66 void *end); 67 }; 68 69 /// Provides functionality to iterate through references in object for dynamic languages 70 template <> 71 class ObjectIterator<LANG_TYPE_DYNAMIC> { 72 public: 73 /// Iterates references in object and passes pointers of them to the handler 74 template <bool INTERRUPTIBLE, typename Handler> 75 static bool Iterate(ObjectHeader *obj, Handler *handler, void *begin, void *end); 76 77 template <typename Handler> 78 static bool IterateAndDiscoverReferences(GC *gc, ObjectHeader *obj, Handler *handler); 79 80 template <typename Handler> 81 static bool IterateAndDiscoverReferences(GC *gc, ObjectHeader *obj, Handler *handler, void *begin, void *end); 82 83 private: 84 template <bool INTERRUPTIBLE, typename Handler> 85 static bool Iterate(HClass *cls, ObjectHeader *obj, Handler *handler); 86 87 template <bool INTERRUPTIBLE, typename Handler> 88 static bool Iterate(HClass *cls, ObjectHeader *obj, Handler *handler, void *begin, void *end); 89 90 template <bool INTERRUPTIBLE, typename Handler> 91 static bool IterateObjectReferences(ObjectHeader *object, HClass *cls, Handler *handler); 92 93 template <bool INTERRUPTIBLE, typename Handler> 94 static bool IterateObjectReferences(ObjectHeader *object, HClass *cls, Handler *handler, void *begin, void *end); 95 96 template <bool INTERRUPTIBLE, typename Handler> 97 static bool IterateClassReferences(coretypes::DynClass *dynClass, Handler *handler); 98 99 template <bool INTERRUPTIBLE, typename Handler> 100 static bool IterateClassReferences(coretypes::DynClass *dynClass, Handler *handler, void *begin, void *end); 101 }; 102 } // namespace ark::mem 103 104 #endif // PANDA_RUNTIME_MEM_NEW_OBJECT_HELPERS_H 105