1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | * or more contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. The ASF licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | /* |
20 | * \file src/runtime/object_internal.h |
21 | * \brief Expose a few functions for CFFI purposes. |
22 | * This file is not intended to be used |
23 | */ |
24 | #ifndef TVM_RUNTIME_OBJECT_INTERNAL_H_ |
25 | #define TVM_RUNTIME_OBJECT_INTERNAL_H_ |
26 | |
27 | #include <tvm/runtime/module.h> |
28 | #include <tvm/runtime/object.h> |
29 | |
30 | #include <string> |
31 | #include <utility> |
32 | |
33 | namespace tvm { |
34 | namespace runtime { |
35 | |
36 | /*! |
37 | * \brief Internal object namespace to expose |
38 | * certain util functions for FFI. |
39 | */ |
40 | class ObjectInternal { |
41 | public: |
42 | /*! |
43 | * \brief Retain an object handle. |
44 | */ |
45 | static void ObjectRetain(TVMObjectHandle obj) { |
46 | if (obj != nullptr) { |
47 | static_cast<Object*>(obj)->IncRef(); |
48 | } |
49 | } |
50 | |
51 | /*! |
52 | * \brief Free an object handle. |
53 | */ |
54 | static void ObjectFree(TVMObjectHandle obj) { |
55 | if (obj != nullptr) { |
56 | static_cast<Object*>(obj)->DecRef(); |
57 | } |
58 | } |
59 | /*! |
60 | * \brief Check of obj derives from the type indicated by type index. |
61 | * \param obj The original object. |
62 | * \param type_index The type index of interest. |
63 | * \return The derivation checking result. |
64 | */ |
65 | static bool DerivedFrom(const Object* obj, uint32_t type_index) { |
66 | return obj->DerivedFrom(type_index); |
67 | } |
68 | /*! |
69 | * \brief Expose TypeKey2Index |
70 | * \param type_key The original type key. |
71 | * \return the corresponding index. |
72 | */ |
73 | static uint32_t ObjectTypeKey2Index(const std::string& type_key) { |
74 | return Object::TypeKey2Index(type_key); |
75 | } |
76 | /*! |
77 | * \brief Convert ModuleHandle to module node pointer. |
78 | * \param handle The module handle. |
79 | * \return the corresponding module node pointer. |
80 | */ |
81 | static ModuleNode* GetModuleNode(TVMModuleHandle handle) { |
82 | // NOTE: we will need to convert to Object |
83 | // then to ModuleNode in order to get the correct |
84 | // address translation |
85 | return static_cast<ModuleNode*>(static_cast<Object*>(handle)); |
86 | } |
87 | /*! |
88 | * \brief Move the ObjectPtr inside ObjectRef out |
89 | * \param obj The ObjectRef |
90 | * \return The result ObjectPtr |
91 | */ |
92 | static ObjectPtr<Object> MoveObjectPtr(ObjectRef* obj) { |
93 | ObjectPtr<Object> data = std::move(obj->data_); |
94 | return data; |
95 | } |
96 | }; |
97 | |
98 | } // namespace runtime |
99 | } // namespace tvm |
100 | #endif // TVM_RUNTIME_OBJECT_INTERNAL_H_ |
101 | |