在 2 个本机模块之间传递数据 (Android NDK)

发布于 2024-12-11 13:21:53 字数 339 浏览 0 评论 0原文

这是我的程序的结构: - 2个独立模块libA和libB,每个模块都是一个共享库libA.so和libB.so - 一项 Java 活动创建 2 个线程 thA 和 thB,每个线程都从一个库调用本机 JNI 函数(thA 从 libA.so 调用函数,thB 从 libB.so 调用函数)。

我想在两个库之间传递本机类型的数据(Java 对此一无所知,包含 Java 无法处理的指针等类型),但我找不到任何方法让它们进行通信。

假设两个库都知道本机类型“typeA”的定义,是否有一种方法可以将 typeA 的对象从 libA 传递到 libB (最好不必复制 VM 内存中的数据)。也许传递一个内存指针?

谢谢

Here is the structure of my program:
- 2 independent modules libA and libB, each one is a single shared library libA.so and libB.so
- A java activity creating 2 threads thA and thB, each one of them calling native JNI functions from one library (thA calling functions from libA.so and thB calling function from libB.so).

I want to pass data of native types (that Java does not know anything about, containing types like pointers that Java can't handle) between both libraries, but I couldn't find any way to make them communicate.

Supossing both libraries know the definition of a native type "typeA", is there a way to pass an object of typeA from libA to libB (preferably without having to copy the data in the VM memory). Pass a memory pointer maybe??

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情勿念 2024-12-18 13:21:53

我正在我正在开发的应用程序中执行此操作。我在 libA 中分配内存,并通过 JNI 将指针作为 long 返回到 Java。然后我调用 libB 中的一个函数来向下传递指针,以便可以访问它。

然而,这种幼稚的方法有问题。尽管有时可能会起作用,但如果 libA 和 libB 位于 RAM 的不同页面上,则触摸 libB 中的指针可能会导致程序崩溃。报告的崩溃原因可能因构建和运行而异,这使得调试变得困难。更糟糕的是,如果分配的内存部分驻留在 RAM 的一页上,部分驻留在另一页上,则只有当您达到边界时,超出边界的内容才会崩溃,因此可能无法立即检测到问题。我已经多次设法杀死操作系统并产生许多不同的错误。如果您的库很小并且加载在相邻的内存中,那么您很有可能会没事。 Android 上的本机应用程序可以摆脱普通 Java 应用程序无法做到的事情。

解决方案:

  1. Ashmem 将允许您在之间创建共享内存空间应用程序。如果您是 Android 开发或 Linux 操作系统的新手,这可能会经历一个学习曲线。

  2. 您可以在两个应用程序之间使用 Socket 并传输数据.

  3. 您可以通过 JNI 函数 直接作用于 Java 创建的缓冲区,该缓冲区的大小必须能够容纳来自 libA 的数据。然后您可以再次通过 JNI 将其传递给 libB。此解决方案会比较慢,并且成本可能会使您一开始使用本机代码的原因无效,但这是调试和发现问题是否与内存相关或程序逻辑固有的好方法。

I am doing exactly this in an application I'm working on. I allocate memory in libA and return the pointer as a long up to Java via JNI. I then call a function in libB that passes the pointer down so it can be accessed.

However, this naive approach has problems. Although it may work sometimes, if libA and libB live on different pages of RAM, touching the pointer in libB may crash the program. The reason reported for the crash may vary on the build and run, making it a difficult situation to debug. Even worse, if the memory allocated partially resides on one page of RAM and partially on another, only when you hit the boundary you will crash whatever lies beyond, and therefore may not detect the problem immediately. I have managed to kill the OS numerous times and produce any number of various errors. If your libraries are small and loaded in adjacent memory, odds are good you will be okay. Native applications on Android can get away with things that vanilla Java apps cannot.

Solutions:

  1. Ashmem will allow you to create a shared memory space between the applications. This may come at a learning curve if you are new to Android development or the Linux OS.

  2. You could use a Socket between two applications and stream the data.

  3. You could send the data up from libA via JNI functions directly to a Java created buffer that must be sized to accommodate the data from libA. You could then once again pass it down via JNI to libB. This solution will be slower and the cost may invalidate the reasons you are using native code to begin with, but it's a good way to debug and discover if your problems are memory-related or inherent to your program's logic.

一瞬间的火花 2024-12-18 13:21:53

将一个指向 typeA 的指针从 A 传递到 B 可能是一种选择。但这取决于库中 typeA 的生命周期。确保 typeA 对象在一个库中未被删除,同时仍在另一个库中使用。

传递指针的一种方法是遍历 JNI 并使用 jlong​​/long 来表示指针。

Passing a pointer to a typeA from A to B may be an option. It depends on the live cycle of typeA in the libraries though. Make sure that the typeA object is not deleted in one library while it's still in use in the other.

One way to pass the pointer is to go over JNI and use a jlong/long to represent the pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文