1818 */
1919/*
2020 * \file src/ffi/custom_allocator.cc
21- * \brief Process-wide registry for custom Object allocators .
21+ * \brief Process-wide registry for the custom Object allocator .
2222 *
2323 * Every Object allocation goes through the registered allocator. libtvm_ffi
2424 * installs a builtin default allocator (BuiltinDefaultAllocate / DeleteSpace)
2525 * at registry construction so the hot path is never null and every Object
2626 * carries at least the base TVMFFICustomAllocHeader. Frontends (Python via
2727 * the Cython extension, future Rust binding, etc.) override the global
28- * default with TVMFFISetDefaultCustomAllocator when they need richer
28+ * allocator with TVMFFISetCustomAllocator when they need richer
2929 * per-allocation bookkeeping.
3030 */
3131#include < tvm/ffi/base_details.h>
3535
3636#include < atomic>
3737#include < cstdlib>
38- #include < cstring>
39- #include < mutex>
4038#include < new>
41- #include < unordered_map>
4239
4340namespace tvm {
4441namespace ffi {
@@ -114,39 +111,16 @@ class CustomAllocatorRegistry {
114111 // Install the builtin default at construction so the very first
115112 // Object allocation in the process (e.g. inside a static initializer
116113 // that registers a global function) finds a usable allocator.
117- static TVMFFICustomAllocator builtin{&BuiltinDefaultAllocate};
118- default_.store (&builtin, std::memory_order_release);
114+ current_.store (BuiltinDefault (), std::memory_order_release);
119115 }
120116
121- TVMFFICustomAllocator* Get (int32_t idx) const {
122- // Hot path: when no per-type override has been registered, skip the
123- // mutex entirely and just read the global default. The size counter is
124- // incremented under mu_ before the first per-type entry is visible, so
125- // an acquire load here pairs with the release store in Set.
126- if (per_type_size_.load (std::memory_order_acquire) == 0 ) {
127- return default_.load (std::memory_order_acquire);
128- }
129- if (idx >= 0 ) {
130- std::lock_guard<std::mutex> g (mu_);
131- auto it = per_type_.find (idx);
132- if (it != per_type_.end ()) return it->second ;
133- }
134- return default_.load (std::memory_order_acquire);
117+ TVMFFICustomAllocator* Get () const {
118+ return current_.load (std::memory_order_acquire);
135119 }
136120
137- void Set (int32_t idx, TVMFFICustomAllocator* allocator) {
138- if (idx < 0 ) return ;
139- std::lock_guard<std::mutex> g (mu_);
140- if (allocator != nullptr ) {
141- per_type_[idx] = allocator;
142- } else {
143- per_type_.erase (idx);
144- }
145- per_type_size_.store (per_type_.size (), std::memory_order_release);
146- }
147-
148- void SetDefault (TVMFFICustomAllocator* allocator) {
149- default_.store (allocator, std::memory_order_release);
121+ void Set (TVMFFICustomAllocator* allocator) {
122+ current_.store (allocator != nullptr ? allocator : BuiltinDefault (),
123+ std::memory_order_release);
150124 }
151125
152126 static CustomAllocatorRegistry* Global () {
@@ -155,30 +129,26 @@ class CustomAllocatorRegistry {
155129 }
156130
157131 private:
158- std::atomic<TVMFFICustomAllocator*> default_{nullptr };
159- std::atomic<size_t > per_type_size_{0 };
160- mutable std::mutex mu_;
161- std::unordered_map<int32_t , TVMFFICustomAllocator*> per_type_;
132+ static TVMFFICustomAllocator* BuiltinDefault () {
133+ static TVMFFICustomAllocator builtin{&BuiltinDefaultAllocate};
134+ return &builtin;
135+ }
136+
137+ std::atomic<TVMFFICustomAllocator*> current_{nullptr };
162138};
163139
164140} // namespace
165141} // namespace ffi
166142} // namespace tvm
167143
168- TVMFFICustomAllocator* TVMFFIGetCustomAllocator (int32_t type_index ) {
144+ TVMFFICustomAllocator* TVMFFIGetCustomAllocator (void ) {
169145 TVM_FFI_LOG_EXCEPTION_CALL_BEGIN ();
170- return tvm::ffi::CustomAllocatorRegistry::Global ()->Get (type_index );
146+ return tvm::ffi::CustomAllocatorRegistry::Global ()->Get ();
171147 TVM_FFI_LOG_EXCEPTION_CALL_END (TVMFFIGetCustomAllocator);
172148}
173149
174- int TVMFFISetCustomAllocator (int32_t type_index, TVMFFICustomAllocator* allocator) {
175- TVM_FFI_SAFE_CALL_BEGIN ();
176- tvm::ffi::CustomAllocatorRegistry::Global ()->Set (type_index, allocator);
177- TVM_FFI_SAFE_CALL_END ();
178- }
179-
180- int TVMFFISetDefaultCustomAllocator (TVMFFICustomAllocator* allocator) {
150+ int TVMFFISetCustomAllocator (TVMFFICustomAllocator* allocator) {
181151 TVM_FFI_SAFE_CALL_BEGIN ();
182- tvm::ffi::CustomAllocatorRegistry::Global ()->SetDefault (allocator);
152+ tvm::ffi::CustomAllocatorRegistry::Global ()->Set (allocator);
183153 TVM_FFI_SAFE_CALL_END ();
184154}
0 commit comments