e.printStackTrace();
}
_data.writeFileDescriptor(fd);
b.transact(XXX, _data, _reply, 0);
_reply.readException();
_reply.readInt();
} catch (RemoteException e) {
e.printStackTrace();
} finally {
_reply.recycle();
_data.recycle();
}
//… …
}
源码路径:./frameworks/base/core/java/android/os/MemoryFile.java
主要功能
-
readBytes()
-
writeBytes()
代码实现
/**
-
Reads bytes from the memory file.
-
Will throw an IOException if the file has been purged.
-
@param buffer byte array to read bytes into.
-
@param srcOffset offset into the memory file to read from.
-
@param destOffset offset into the byte array buffer to read into.
-
@param count number of bytes to read.
-
@return number of bytes read.
-
@throws IOException if the memory file has been purged or deactivated.
*/
public int readBytes(byte[] buffer, int srcOffset, int destOffset, int count)
throws IOException {
beginAccess();
try {
mMapping.position(srcOffset);
mMapping.get(buffer, destOffset, count);
} finally {
endAccess();
}
return count;
}
/**
-
Write bytes to the memory file.
-
Will throw an IOException if the file has been purged.
-
@param buffer byte array to write bytes from.
-
@param srcOffset offset into the byte array buffer to write from.
-
@param destOffset offset into the memory file to write to.
-
@param count number of bytes to write.
-
@throws IOException if the memory file has been purged or deactivated.
*/
public void writeBytes(byte[] buffer, int srcOffset, int destOffset, int count)
throws IOException {
beginAccess();
try {
mMapping.position(destOffset);
mMapping.put(buffer, srcOffset, count);
} finally {
endAccess();
}
}
JNI:./frameworks/base/core/jni/android_os_MemoryFile.cpp
namespace android {
static jboolean android_os_MemoryFile_pin(JNIEnv* env, jobject clazz, jobject fileDescriptor,
jboolean pin) {
int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
int result = (pin ? ashmem_pin_region(fd, 0, 0) : ashmem_unpin_region(fd, 0, 0));
if (result < 0) {
jniThrowException(env, “java/io/IOException”, NULL);
}
return result == ASHMEM_WAS_PURGED;
}
static jint android_os_MemoryFile_get_size(JNIEnv* env, jobject clazz,
jobject fileDescriptor) {
int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
// Use ASHMEM_GET_SIZE to find out if the fd refers to an ashmem region.
// ASHMEM_GET_SIZE should succeed for all ashmem regions, and the kernel
// should return ENOTTY for all other valid file descriptors
int result = ashmem_get_size_region(fd);
if (result < 0) {
if (errno == ENOTTY) {
// ENOTTY means that the ioctl does not apply to this object,
// i.e., it is not an ashmem region.
return (jint) -1;
}
// Some other error, throw exception
jniThrowIOException(env, errno);
return (jint) -1;
}
return (jint) result;
}
static const JNINativeMethod methods[] = {
{“native_pin”, “(Ljava/io/FileDescriptor;Z)Z”, (void*)android_os_MemoryFile_pin},
{“native_get_size”, “(Ljava/io/FileDescriptor;)I”,
(void*)android_os_MemoryFile_get_size}
};
int register_android_os_MemoryFile(JNIEnv* env) {
return RegisterMethodsOrDie(env, “android/os/MemoryFile”, methods, NELEM(methods));
}
}
可以看到使用了ashmem, ashmem又是什么?
Ashmem (Anonymous Shared Memroy,匿名共享内存)是一种共享内存的机制,它利用了Linux的mmap系统调用,将不同进程中的同一段物理内存映射到进程各自的虚拟地址空间,从而实现高效的进程间共享。
优势
-
在dev目录下对应的设备是/dev/ashmem,相比于传统的内存分配机制,如malloc、anonymous/named mmap,其好处是提供了辅助内核内存回收算法的pin/unpin机制。
-
我们知道Binder 每个进程 mmap接收数据的大小有限制,超过 1M 就会报错。所以我们可以用Ashmem在 IPC 中传递大型数据。
源码位置:./system/core/libcutils/ashmem-dev.cpp
主要方法
-
ashmem_create_region :向ashmem驱动程序请求为应用程序创建一块匿名共享内存,并且放回它的文件描述符。
-
ashmem_set_prot_region:用来设置匿名共享内存的访问保护位。
-
ashmem_pin_region:向fd描述的文件发送io控制命令ASHMEM_PIN,用来锁定一小块内存区域。offset表示在匿名内存中的偏移位置,len表示长度。
-
ashmem_unpin_region:和 ashmem_pin_region 基本一样,只是发送了ASHMEM_UNPIN命令用来解锁一块内存区域。
-
ashmem_get_size_region:用来获取匿名共享内存大小。
其中 MemoryFile用到了两个:pin_region和get_size_region.即锁定一块内存区域和获取匿名共享内存的大小。
/*
-
ashmem_create_region - creates a new ashmem region and returns the file
-
descriptor, or <0 on error
-
`name’ is an optional label to give the region (visible in /proc/pid/maps)
-
`size’ is the size of the region, in page-aligned bytes
*/
int ashmem_create_region(const char *name, size_t size)
{
int ret, save_errno;
int fd = __ashmem_open();
if (fd < 0) {
return fd;
}
if (name) {
char buf[ASHMEM_NAME_LEN] = {0};
strlcpy(buf, name, sizeof(buf));
ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));
if (ret < 0) {
goto error;
}
}
ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
if (ret < 0) {
goto error;
}
return fd;
error:
save_errno = errno;
close(fd);
errno = save_errno;
return ret;
}
int ashmem_set_prot_region(int fd, int prot)
{
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
}
int ashmem_pin_region(int fd, size_t offset, size_t len)
{
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
}
int ashmem_unpin_region(int fd, size_t offset, size_t len)
{
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
}
int ashmem_get_size_region(int fd)
{
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {