paleolimbot commented on code in PR #387:
URL: https://github.com/apache/arrow-nanoarrow/pull/387#discussion_r1492780215
##########
src/nanoarrow/nanoarrow.hpp:
##########
@@ -273,6 +280,49 @@ using UniqueArrayView = internal::Unique<struct
ArrowArrayView>;
/// @}
+/// \defgroup nanoarrow_hpp-buffer Buffer helpers
+///
+/// Helpers to wrap buffer-like C++ objects as ArrowBuffer objects that can
+/// be used to build ArrowArray objects.
+///
+/// @{
+
+/// \brief Initialize a buffer wrapping an arbitrary C++ object
+///
+/// Initializes a buffer with a release callback that deletes the moved obj
+/// when ArrowBufferReset is called. T must be movable.
+template <typename T>
+static inline void BufferInitWrapped(struct ArrowBuffer* buffer, T obj, const
void* ptr,
+ int64_t size_bytes) {
+ T* obj_moved = new T(std::move(obj));
+ buffer->data = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(ptr));
+ buffer->size_bytes = size_bytes;
+ buffer->capacity_bytes = 0;
+ buffer->allocator =
+ ArrowBufferDeallocator(&internal::DeallocateWrappedBuffer<T>, obj_moved);
+}
Review Comment:
I'll update the documentation here, but the `ptr` (which gets assigned to
`buffer->data`) can be whatever you want. The C++ object `T` manages the
lifecycle, but this constructor doesn't try to guess what the actual pointer
value that it keeps valid is.
In the context of your PR, it would replace your `set_buffer()` with
`BufferInitWrapped(device_buf, device_buf->data(), device_buf.size())`.
--
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]