23 #ifndef TVM_RUNTIME_MEMORY_H_
24 #define TVM_RUNTIME_MEMORY_H_
29 #include <type_traits>
40 template <
typename T,
typename... Args>
59 template <
typename Derived>
68 template <
typename T,
typename... Args>
70 using Handler =
typename Derived::template Handler<T>;
71 static_assert(std::is_base_of<Object, T>::value,
"make can only be used to create Object");
72 T* ptr = Handler::New(
static_cast<Derived*
>(
this), std::forward<Args>(args)...);
73 ptr->type_index_ = T::RuntimeTypeIndex();
74 ptr->deleter_ = Handler::Deleter();
85 template <
typename ArrayType,
typename ElemType,
typename... Args>
87 using Handler =
typename Derived::template ArrayHandler<ArrayType, ElemType>;
88 static_assert(std::is_base_of<Object, ArrayType>::value,
89 "make_inplace_array can only be used to create Object");
91 Handler::New(
static_cast<Derived*
>(
this), num_elems, std::forward<Args>(args)...);
92 ptr->type_index_ = ArrayType::RuntimeTypeIndex();
93 ptr->deleter_ = Handler::Deleter();
101 template <
typename T>
104 using StorageType =
typename std::aligned_storage<
sizeof(T),
alignof(T)>::type;
106 template <
typename... Args>
122 new (data) T(std::forward<Args>(args)...);
123 return reinterpret_cast<T*
>(data);
129 static void Deleter_(
Object* objptr) {
133 T* tptr =
static_cast<T*
>(objptr);
144 template <
typename ArrayType,
typename ElemType>
147 using StorageType =
typename std::aligned_storage<
sizeof(ArrayType),
alignof(ArrayType)>::type;
149 static_assert(
alignof(ArrayType) %
alignof(ElemType) == 0 &&
150 sizeof(ArrayType) %
alignof(ElemType) == 0,
151 "element alignment constraint");
153 template <
typename... Args>
168 size_t requested_size = num_elems *
sizeof(ElemType) +
sizeof(ArrayType);
169 size_t num_storage_slots = (requested_size + unit - 1) / unit;
171 new (data) ArrayType(std::forward<Args>(args)...);
172 return reinterpret_cast<ArrayType*
>(data);
178 static void Deleter_(
Object* objptr) {
182 ArrayType* tptr =
static_cast<ArrayType*
>(objptr);
187 tptr->ArrayType::~ArrayType();
194 template <
typename T,
typename... Args>
199 template <
typename ArrayType,
typename ElemType,
typename... Args>
202 std::forward<Args>(args)...);
Base class of object allocators that implements make. Use curiously recurring template pattern.
Definition: memory.h:60
ObjectPtr< ArrayType > make_inplace_array(size_t num_elems, Args &&... args)
Definition: memory.h:86
ObjectPtr< T > make_object(Args &&... args)
Make a new object using the allocator.
Definition: memory.h:69
A custom smart pointer for Object.
Definition: object.h:362
base class of all object containers.
Definition: object.h:171
void(* FDeleter)(Object *self)
Object deleter.
Definition: object.h:177
static ArrayType * New(SimpleObjAllocator *, size_t num_elems, Args &&... args)
Definition: memory.h:154
typename std::aligned_storage< sizeof(ArrayType), alignof(ArrayType)>::type StorageType
Definition: memory.h:147
static Object::FDeleter Deleter()
Definition: memory.h:175
static Object::FDeleter Deleter()
Definition: memory.h:126
typename std::aligned_storage< sizeof(T), alignof(T)>::type StorageType
Definition: memory.h:104
static T * New(SimpleObjAllocator *, Args &&... args)
Definition: memory.h:107
ObjectPtr< ArrayType > make_inplace_array_object(size_t num_elems, Args &&... args)
Definition: memory.h:200
ObjectPtr< ArrayNode > make_object()
Definition: array.h:908
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A managed object in the TVM runtime.