Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

java - shared memory in android ndk

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.

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.

which is at

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

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.

Your Java code can access fd using some workarounds.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...