tqchen commented on code in PR #593:
URL: https://github.com/apache/tvm-ffi/pull/593#discussion_r3295117594


##########
include/tvm/ffi/c_api.h:
##########
@@ -580,6 +580,83 @@ TVM_FFI_DLL int TVMFFIObjectDecRef(TVMFFIObjectHandle obj);
 TVM_FFI_DLL int TVMFFIObjectCreateOpaque(void* handle, int32_t type_index,
                                          void (*deleter)(void* handle), 
TVMFFIObjectHandle* out);
 
+//-----------------------------------------------------------------------
+// Section: Custom allocator hook for Object allocations
+//
+// Frontends (Python, Rust, etc.) can register a custom allocator that the
+// core ObjAllocator consults inside make_object<T> / 
make_inplace_array_object.
+// When a custom allocator is registered, every allocation of a ref-counted
+// Object is given the allocator's chance to prepend bookkeeping bytes ahead
+// of the object. The allocator returns the address of the embedded T
+// (= Object = TVMFFIObject); immediately before that address it must place a
+// TVMFFICustomAllocHeader whose ``delete_space`` field is invoked from the
+// C++ Weak deleter to release the entire block.
+//
+// The core libtvm_ffi.so does not depend on Python (or any other frontend).
+// libtvm_ffi installs a builtin default allocator at registry init time, so
+// every Object allocation always carries at least the base
+// TVMFFICustomAllocHeader; frontends that need richer per-allocation
+// bookkeeping (e.g. Python's ``py_object`` back-pointer) override the global
+// default with their own allocator.
+//-----------------------------------------------------------------------
+/*!
+ * \brief Header that sits immediately before the embedded TVMFFIObject. The
+ *        C++ deleter recovers this header by walking back a fixed offset
+ *        (``sizeof(TVMFFICustomAllocHeader)``) from the TVMFFIObject pointer
+ *        and invokes ``delete_space``.
+ *
+ * Frontends are free to define a derived layout that places extra fields
+ * before this base; the derived ``delete_space`` callback is responsible for
+ * recovering its enclosing struct from the ``tptr`` argument. Frontends can
+ * detect their own allocations by comparing this header's ``delete_space``
+ * pointer against their own callback (no flag bit needed).
+ */
+typedef struct {
+  /*!
+   * \brief Free the allocation that contains ``tptr``.
+   * \param tptr The pointer originally returned by
+   *             ``TVMFFICustomAllocator::allocate`` (i.e. the address of T,
+   *             not the start of the underlying malloc block).
+   */
+  void (*delete_space)(void* tptr);
+} TVMFFICustomAllocHeader;
+
+/*!
+ * \brief Custom allocator entry registered with TVMFFISetCustomAllocator.
+ */
+typedef struct {
+  /*!
+   * \brief Allocate ``size`` bytes for an Object of ``type_index`` with the
+   *        requested ``alignment``. Implementations must place a
+   *        TVMFFICustomAllocHeader immediately before the returned pointer
+   *        (at the fixed base offset documented on TVMFFICustomAllocHeader)
+   *        with a non-NULL ``delete_space``.
+   * \return Pointer to T's location (treated as TVMFFIObject* by the caller).
+   */
+  void* (*allocate)(size_t size, size_t alignment, int32_t type_index);

Review Comment:
   it is helpful to include 
   
   ```c++
     void* (*allocate)(size_t size, size_t alignment, int32_t type_index, void* 
context);
     void* context;
   ```
   This helps to future proof case that needs a caller
   
   Need to document the error behavior, when error, return NULL, and error is 
set through TVMFFIErrorSetRaised



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to