android ndk中的共享内存 [英] shared memory in android ndk

查看:34
本文介绍了android ndk中的共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将大数据从一个进程/应用程序传输到 Android 中的另一个进程/应用程序,但有些如何使用 Java 层中的内存文件创建共享内存'不工作.

I wanted to transfer large data from one process/app to another process/app in Android, but some how creating shared memory using Memory File in Java layer doesn't work.

然后尝试在我阅读的某个地方在 android native 中创建共享内存
可以使用 ashmem_create_region API.但看起来这个 API 无法访问或打开.它在 libcutils.so 中.

So then tried to create shared Memory in android native, somewhere I read
ashmem_create_region API can be used. But looks like this API is not accessible or open. It is inside libcutils.so.

android-ndk-r9c/platforms/android-19/arch-arm/usr/lib/rs/libcutils.so

第一季度.如何在android原生代码中访问这个库.

Q1. How to access this library in android native code.

第二季度.如果无法访问,那么在 ndk 工具中提供此功能的目的是什么.

Q2. If it is not possible to access then what is purpose of giving this in ndk tool.

第三季度.如果我以某种方式链接了 libcutils.so 并且能够使用 libcutils.so 在本机中创建共享内存那么我可以使用这个 fd 与 Java 层中的 MemoryFile 进行映射,这样它将避免为每个数据事务写入应用程序本机副本.

Q3. If somehow I linked libcutils.so and able to create shared Memory in native using libcutils.so Then can I use this fd to map with MemoryFile in Java layer so it will avoid application native copy to write for each data transaction.

推荐答案

libcutils.so 在 ndk 中提供以启用 renderscript 集成.它仍然是一个未记录的系统库,因为 Android 不承诺在所有平台上支持其所有 API,尤其是在未来的平台上,请参阅 关于 android-ndk 用户组.

libcutils.so is shipped in ndk to enable renderscript integration. It remains an undocumented system library, in the sense that Android does not promise to support all its APIs on all platforms, and especially on future platforms, see more on on android-ndk user group.

这就是说,我应该承认使用 ashmem 是相当安全的,因为它是明确的 自 v.1 起在 Java API 中记录.我为链接 libcutils.so 其他地方写了一个简短的解决方案.

This said, I should acknowledge that using ashmem is quite safe, as it is explicitly documented in Java API since v.1. I wrote a short solution for linking against libcutils.so elsewhere on SO.

这里是关于 ashmem_create_region() API 和 IPC 警告的讨论:https://groups.google.com/forum/m/#!topic/android-platform/L6a6Xvn4HSI.tl;nr:你仍然需要 Binder 进行握手.

Here is a discussion about ashmem_create_region() API and caveats with IPC through this: https://groups.google.com/forum/m/#!topic/android-platform/L6a6Xvn4HSI. tl;nr: you still need Binder for handshake.

您的 Java 代码可以使用一些解决方法fd>.

Your Java code can access fd using some workarounds.

更新: 新的 NDK r9d 出,libcutils.so 不再在 platforms/android-19/arch-arm/usr/lib 目录下,正如 @Tim Murray 在下面的评论中所承诺的那样.

Update: new NDK r9d is out, and libcutils.so is no longer in the platforms/android-19/arch-arm/usr/lib directory, as @Tim Murray promised in comments below.

这篇关于android ndk中的共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆