将 JAVA 中分配的内存所有权转移给 C++

发布于 2025-01-14 20:29:55 字数 707 浏览 5 评论 0原文

我有一个用 C++ 编写的库,它具有可通过复制和移动语义传输批量数据的 API。对于前。其中一个函数签名如下所示:

template <
    typename ITERATOR,
    typename TYPE = typename std::iterator_traits<ITERATOR>::value_type>
    void appendData(std::size_t  columnIndex,
                    ITERATOR      begin,
                    ITERATOR      end);

调用者可以将迭代器传递给像 std::vector 这样的数据结构,并且此方法将附加从“begin”开始直到但不包括的数据“结束”迭代器。调用者还可以传入 std::move_iterator 而不是常规迭代器,并且数据将被移动到内部结构中而不是被复制。出于效率原因,建议该库的用户传入 std::move_iterator

我想从 Java 代码(可能通过 JNI)访问这个库并使用这个接口来传输数据。问题是如何将 Java 内部分配的数据缓冲区的所有权转移到 C++ 端,以便稍后在 C++ 中释放 Java 分配的数据缓冲区?

我考虑过使用 apache arrow 作为数据传输机制,但如果我没记错的话,箭头结构的所有权仍然取决于分配这些结构的(Java)代码。

I have a library written in C++ that has APIs to transfer bulk data, both via copy and move semantics. For ex. one of the function signature looks like this:

template <
    typename ITERATOR,
    typename TYPE = typename std::iterator_traits<ITERATOR>::value_type>
    void appendData(std::size_t  columnIndex,
                    ITERATOR      begin,
                    ITERATOR      end);

A caller can pass in an iterator to a data structure like a std::vector and this method will append the data starting from 'begin' up to but not including the 'end' iterator. The caller can also pass in a std::move_iterator instead of a regular iterator, and the data would be moved into the internal structures instead of being copied. The recommendation to the users of this library is to pass in a std::move_iterator for efficiency reasons.

I want to access this library from Java code (possibly via JNI) and use this interface to transfer data. The question is how do I transfer ownership of a buffer of data that was allocated inside Java to the C++ side so that the Java allocated buffer of data is later freed in C++?

I thought about using apache arrow as the data transfer mechanism, but again the ownership of the arrow structures still rests with the (Java) code that allocated these structures if I am not wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文