|
| 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 tvm/runtime/memory.h |
| 21 | + * \brief Runtime memory management. |
| 22 | + */ |
| 23 | +#ifndef TVM_RUNTIME_MEMORY_H_ |
| 24 | +#define TVM_RUNTIME_MEMORY_H_ |
| 25 | + |
| 26 | +#include <utility> |
| 27 | +#include <type_traits> |
| 28 | +#include "object.h" |
| 29 | + |
| 30 | +namespace tvm { |
| 31 | +namespace runtime { |
| 32 | +/*! |
| 33 | + * \brief Allocate an object using default allocator. |
| 34 | + * \param args arguments to the constructor. |
| 35 | + * \tparam T the node type. |
| 36 | + * \return The NodePtr to the allocated object. |
| 37 | + */ |
| 38 | +template<typename T, typename... Args> |
| 39 | +inline ObjectPtr<T> make_object(Args&&... args); |
| 40 | + |
| 41 | +// Detail implementations after this |
| 42 | +// |
| 43 | +// The current design allows swapping the |
| 44 | +// allocator pattern when necessary. |
| 45 | +// |
| 46 | +// Possible future allocator optimizations: |
| 47 | +// - Arena allocator that gives ownership of memory to arena (deleter_= nullptr) |
| 48 | +// - Thread-local object pools: one pool per size and alignment requirement. |
| 49 | +// - Can specialize by type of object to give the specific allocator to each object. |
| 50 | + |
| 51 | +/*! |
| 52 | + * \brief Base class of object allocators that implements make. |
| 53 | + * Use curiously recurring template pattern. |
| 54 | + * |
| 55 | + * \tparam Derived The derived class. |
| 56 | + */ |
| 57 | +template<typename Derived> |
| 58 | +class ObjAllocatorBase { |
| 59 | + public: |
| 60 | + /*! |
| 61 | + * \tparam T The type to be allocated. |
| 62 | + * \tparam Args The constructor signature. |
| 63 | + * \param args The arguments. |
| 64 | + */ |
| 65 | + template<typename T, typename... Args> |
| 66 | + inline ObjectPtr<T> make(Args&&... args) { |
| 67 | + using Handler = typename Derived::template Handler<T>; |
| 68 | + static_assert(std::is_base_of<Object, T>::value, |
| 69 | + "make_node can only be used to create NodeBase"); |
| 70 | + T* ptr = Handler::New(static_cast<Derived*>(this), |
| 71 | + std::forward<Args>(args)...); |
| 72 | + ptr->type_index_ = T::type_index(); |
| 73 | + ptr->deleter_ = Handler::Deleter(); |
| 74 | + return ObjectPtr<T>(ptr); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +// Simple allocator that uses new/delete. |
| 79 | +class SimpleObjAllocator : |
| 80 | + public ObjAllocatorBase<SimpleObjAllocator> { |
| 81 | + public: |
| 82 | + template<typename T> |
| 83 | + class Handler { |
| 84 | + public: |
| 85 | + template<typename... Args> |
| 86 | + static T* New(SimpleObjAllocator*, Args&&... args) { |
| 87 | + return new T(std::forward<Args>(args)...); |
| 88 | + } |
| 89 | + |
| 90 | + static Object::FDeleter Deleter() { |
| 91 | + return Deleter_; |
| 92 | + } |
| 93 | + |
| 94 | + private: |
| 95 | + static void Deleter_(Object* ptr) { |
| 96 | + delete static_cast<T*>(ptr); |
| 97 | + } |
| 98 | + }; |
| 99 | +}; |
| 100 | + |
| 101 | +template<typename T, typename... Args> |
| 102 | +inline ObjectPtr<T> make_object(Args&&... args) { |
| 103 | + return SimpleObjAllocator().make<T>(std::forward<Args>(args)...); |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace runtime |
| 107 | +} // namespace tvm |
| 108 | +#endif // TVM_RUNTIME_MEMORY_H_ |
0 commit comments