Android MemoryFile 源码分析以及使用


 Android对于I/O操作效率比较低,提高效率的方法可以通过NDK直接进行读写,或者调用MemoryFile.java类对I/O进行处理。


一、MemoryFile.java 源码分析

 MemoryFile.java 源码位置:frameworks/base/core/java/android/os/MemoryFile.java

MemoryFile类直接对native方法进行封装,通过JNI调用底层C++方法,下面对具体函数进行分析。

1. 构造函数

    public MemoryFile(String name, int length) throws IOException {
        mLength = length;
        mFD = native_open(name, length);
        if (length > 0) {
            mAddress = native_mmap(mFD, length, PROT_READ | PROT_WRITE);
        } else {
            mAddress = 0;
        }
    }


这里直接调用native方法native_mmap对name文件进行内存映射,这里的native_mmap方法调用JNI方法android_os_MemoryFile_open,这个函数又通过运行库提供的ashmem_create_region来创建内存共享映射,这个函数的实现在system/core/libcutils/ashmem-dev.c中,具体代码如下:

/*
  * 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 fd, ret;
  fd = open(ASHMEM_DEVICE, O_RDWR);   // open函数打开ASHMEM_DEVICE设备文件
  if (fd < 0)
  return fd;
  if (name) {
  char buf[ASHMEM_NAME_LEN];
  strlcpy(buf, name, sizeof(buf));
  ret = ioctl(fd, ASHMEM_SET_NAME, buf);
  if (ret < 0)
  goto error;
  }
  ret = ioctl(fd, ASHMEM_SET_SIZE, size);  // 设置匿名共享内存的名称和大小
  if (ret < 0)
  goto error;
  return fd;
  error:
  close(fd);
  return ret;
  }


调用native_mmap函数对打开的设备文件进行映射,调用JNI函数android_os_MemoryFile_mmap,这个函数利用内存映射mmap实现。


2. readBytes 函数

具体源码如下:

    public int readBytes(byte[] buffer, int srcOffset, int destOffset, int count)
            throws IOException {
        if (isDeactivated()) {
            throw new IOException("Can't read from deactivated memory file.");
        }
        if (destOffset < 0 || destOffset > buffer.length || count < 0
                || count > buffer.length - destOffset
                || srcOffset < 0 || srcOffset > mLength
                || count > mLength - srcOffset) {
            throw new IndexOutOfBoundsException();
        }
        return native_read(mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);
    }


readBytes函数同样调用JNI接口native_read即android_os_MemoryFile_read,通过SetByteArrayRegion进行内存的读取。


3. writeBytes函数

    public void writeBytes(byte[] buffer, int srcOffset, int destOffset, int count)
            throws IOException {
        if (isDeactivated()) {
            throw new IOException("Can't write to deactivated memory file.");
        }
        if (srcOffset < 0 || srcOffset > buffer.length || count < 0
                || count > buffer.length - srcOffset
                || destOffset < 0 || destOffset > mLength
                || count > mLength - destOffset) {
            throw new IndexOutOfBoundsException();
        }
        native_write(mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);
    }


这里调用native_write接口,通过android_os_MemoryFile_write 调用GetByteArrayRegion对内存进行写操作。

这两个函数用到了ashmem_pin_region和ashmem_unpin_region是系统运行时库提供的接口,用来执行我们前面说的匿名共享内存的锁定和解锁操作,它们的作用是告诉Ashmem驱动程序,它的哪些内存块是正在使用的,需要锁定,哪些内存是不需要使用了,可以它解锁,这样,Ashmem驱动程序就可以辅助内存管理系统来有效地管理内存了。



二、MemoryFile.java 使用实例


public class MemoryFileTest {


private Context content;
public MemoryFileTest(Context content) {
this.content = content;


}
public void testFileDescriptor() throws Exception {  
        MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);


        byte[] buffer = new byte[testString.length];


        // check low level accessors
        file.writeBytes(testString, 0, 2000, testString.length);
        file.readBytes(buffer, 2000, 0, testString.length);
        compareBuffers(testString, buffer, testString.length);


        // check streams
        buffer = new byte[testString.length];


        OutputStream os = file.getOutputStream();
        os.write(testString);


        InputStream is = file.getInputStream();
        is.mark(testString.length);
        is.read(buffer);
        compareBuffers(testString, buffer, testString.length);


        // test mark/reset
        buffer = new byte[testString.length];
        is.reset();
        is.read(buffer);
        compareBuffers(testString, buffer, testString.length);


        file.close();
    }  


private void compareBuffers(byte[] buffer1, byte[] buffer2, int length) throws Exception {  
        for (int i = 0; i < length; i++) {  
            if (buffer1[i] != buffer2[i]) { 
            Toast.makeText(content, "readBytes did not read back what writeBytes wrote", Toast.LENGTH_SHORT).show();
            }  
        }  
}

private static final byte[] testString = new byte[] {
       3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4,
       0, 6, 2, 8, 6, 2, 0, 8, 9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7, 9, 8, 2, 1, 4, 8, 0, 8, 6, 5, 1, 3, 2, 8, 2, 3, 0, 6, 6, 4, 7, 0, 9, 3, 8, 4, 4, 6, 0, 9, 5, 5, 0, 5, 8, 2, 2, 3, 1, 7, 2,
       5, 3, 5, 9, 4, 0, 8, 1, 2, 8, 4, 8, 1, 1, 1, 7, 4, 5, 0, 2, 8, 4, 1, 0, 2, 7, 0, 1, 9, 3, 8, 5, 2, 1, 1, 0, 5, 5, 5, 9, 6, 4, 4, 6, 2, 2, 9, 4, 8, 9, 5, 4, 9, 3, 0, 3, 8, 1, 9, 6, 4, 4, 2, 8, 8, 1, 0, 9, 7, 5,
       6, 6, 5, 9, 3, 3, 4, 4, 6, 1, 2, 8, 4, 7, 5, 6, 4, 8, 2, 3, 3, 7, 8, 6, 7, 8, 3, 1, 6, 5, 2, 7, 1, 2, 0, 1, 9, 0, 9, 1, 4, 5, 6, 4, 8, 5, 6, 6, 9, 2, 3, 4, 6, 0, 3, 4, 8, 6, 1, 0, 4, 5, 4, 3, 2, 6, 6, 4, 8, 2,
       1, 3, 3, 9, 3, 6, 0, 7, 2, 6, 0, 2, 4, 9, 1, 4, 1, 2, 7, 3, 7, 2,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6, 1, 1, 1, 9, 5, 9, 0, 9, 2, 1, 6, 4, 2, 0, 1, 9, 8, 9, 3, 8, 0, 9, 5, 2, 5, 7, 2, 0, 1, 0, 6, 5, 4, 8, 5, 8, 6, 3, 2, 7, 8, 8, 6, 5, 9, 3, 6, 1, 5, 3, 3, 8, 1, 8, 2, 7, 9, 6, 8, 2, 3, 0, 3, 0, 1, 9, 5, 2,
       0, 3, 5, 3, 0, 1, 8, 5, 2, 9, 6, 8, 9, 9, 5, 7, 7, 3, 6, 2, 2, 5, 9, 9, 4, 1, 3, 8, 9, 1, 2, 4, 9, 7, 2, 1, 7, 7, 5, 2, 8, 3, 4, 7, 9, 1, 3, 1, 5, 1, 5, 5, 7, 4, 8, 5, 7, 2, 4, 2, 4, 5, 4, 1, 5, 0, 6, 9, 5, 9,

       5, 0, 8, 2, 9, 5, 3, 3, 1}


}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android系统源码是按照功能进行分类的,主要分为系统代码、工具、文档、开发环境、虚拟机、配置脚本和编译脚本等类别。其中,系统代码是Android系统的核心部分,包含了各个功能模块的实现代码。工具包括了用于Android系统开发和调试的工具,例如adb、emulator等。文档部分包含了Android系统的开发文档和相关说明。开发环境是Android系统的开发所需的各种环境、库和工具。虚拟机是用于运行Android应用程序的Dalvik虚拟机。配置脚本和编译脚本是用于配置和编译Android系统的脚本文件。 Android系统采用的是一个从BSD继承而来的标准的系统函数库bionic。它是一个轻量级的C库,专门为Android系统进行了优化和定制。在源码根目录下有bionic文件夹,它包含了bionic库的源代码和相关文件。 Android4.3程序库的类型非常多,功能也非常强大。其中一些常用且重要的系统程序库包括: - libcore:Android系统的核心库,提供了Java核心类库的实现,包括集合、IO、网络等功能。 - libandroid_runtime:Android运行时库,提供了Android应用程序运行所需的功能,例如应用程序的启动和管理、进程间通信等。 - libui:Android系统的用户界面库,提供了绘制窗口、图形渲染等功能。 - libsqlite:SQLite数据库库,提供了数据库的管理和操作功能。 - libmedia:媒体库,提供了音频和视频的播放和录制功能。 以上是Android系统源码分析的一些基本信息。如果你有更具体的问题,可以告诉我,我会尽力帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值