Android系统匿名共享内存Ashmem(Anonymous Shared Memory)简要介绍和学习计划

本文详细介绍了Android系统中的匿名共享内存Ashmem子系统,包括其工作原理、使用方法以及如何在两个进程中共享内存数据。通过实例演示了如何创建、获取和操作匿名共享内存文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android系统中,提供了独特的匿名共享内存子系统Ashmem(Anonymous Shared Memory),它以驱动程序的形式实现在内核空间中。它有两个特点,一是能够辅助内存管理系统来有效地管理不再使用的内存块,二是它通过Binder进程间通信机制来实现进程间的内存共享。本文中,我们将通过实例来简要介绍Android系统的匿名共享内存的使用方法,使得我们对Android系统的匿名共享内存机制有一个感性的认识,为进一步学习它的源代码实现打下基础。

Android系统的匿名共享内存子系统的主体是以驱动程序的形式实现在内核空间的,同时,在系统运行时库层和应用程序框架层提供了访问接口,其中,在系统运行时库层提供了C/C++调用接口,而在应用程序框架层提供了Java调用接口。这里,我们将直接通过应用程序框架层提供的Java调用接口来说明匿名共享内存子系统Ashmem的使用方法,毕竟我们在Android开发应用程序时,是基于Java语言的,而实际上,应用程序框架层的Java调用接口是通过JNI方法来调用系统运行时库层的C/C++调用接口,最后进入到内核空间的Ashmem驱动程序去的。

我们在这里举的例子是一个名为Ashmem的应用程序,它包含了一个Server端和一个Client端实现,其中,Server端是以Service的形式实现的,在这里Service里面,创建一个匿名共享内存文件,而Client是一个Activity,这个Activity通过Binder进程间通信机制获得前面这个Service创建的匿名共享内存文件的句柄,从而实现共享。在Android应用程序框架层,提供了一个MemoryFile接口来封装了匿名共享内存文件的创建和使用,它实现在frameworks/base/core/java/android/os/MemoryFile.java文件中。下面,我们就来看看Server端是如何通过MemoryFile类来创建匿名共享内存文件的以及Client是如何获得这个匿名共享内存文件的句柄的。

在MemoryFile类中,提供了两种创建匿名共享内存的方法,我们通过MemoryFile类的构造函数来看看这两种使用方法:

  1. publicclassMemoryFile
  2. {
  3. ......
  4. /**
  5. *Allocatesanewashmemregion.Theregionisinitiallynotpurgable.
  6. *
  7. *@paramnameoptionalnameforthefile(canbenull).
  8. *@paramlengthofthememoryfileinbytes.
  9. *@throwsIOExceptionifthememoryfilecouldnotbecreated.
  10. */
  11. publicMemoryFile(Stringname,intlength)throwsIOException{
  12. mLength=length;
  13. mFD=native_open(name,length);
  14. mAddress=native_mmap(mFD,length,PROT_READ|PROT_WRITE);
  15. mOwnsRegion=true;
  16. }
  17. /**
  18. *Createsareferencetoanexistingmemoryfile.Changestotheoriginalfile
  19. *willbeavailablethroughthisreference.
  20. *Callsto{@link#allowPurging(boolean)}onthereturnedMemoryFilewillfail.
  21. *
  22. *@paramfdFiledescriptorforanexistingmemoryfile,asreturnedby
  23. *{@link#getFileDescriptor()}.Thisfiledescriptorwillbeclosed
  24. *by{@link#close()}.
  25. *@paramlengthLengthofthememoryfileinbytes.
  26. *@parammodeFilemode.Currentlyonly"r"forread-onlyaccessissupported.
  27. *@throwsNullPointerExceptionif<code>fd</code>isnull.
  28. *@throwsIOExceptionIf<code>fd</code>doesnotrefertoanexistingmemoryfile,
  29. *orifthefilemodeoftheexistingmemoryfileismorerestrictive
  30. *than<code>mode</code>.
  31. *
  32. *@hide
  33. */
  34. publicMemoryFile(FileDescriptorfd,intlength,Stringmode)throwsIOException{
  35. if(fd==null){
  36. thrownewNullPointerException("Filedescriptorisnull.");
  37. }
  38. if(!isMemoryFile(fd)){
  39. thrownewIllegalArgumentException("Notamemoryfile.");
  40. }
  41. mLength=length;
  42. mFD=fd;
  43. mAddress=native_mmap(mFD,length,modeToProt(mode));
  44. mOwnsRegion=false;
  45. }
  46. ......
  47. }
public class MemoryFile
{
	......

	/**
	* Allocates a new ashmem region. The region is initially not purgable.
	*
	* @param name optional name for the file (can be null).
	* @param length of the memory file in bytes.
	* @throws IOException if the memory file could not be created.
	*/
	public MemoryFile(String name, int length) throws IOException {
		mLength = length;
		mFD = native_open(name, length);
		mAddress = native_mmap(mFD, length, PROT_READ | PROT_WRITE);
		mOwnsRegion = true;
	}

	/**
	* Creates a reference to an existing memory file. Changes to the original file
	* will be available through this reference.
	* Calls to {@link #allowPurging(boolean)} on the returned MemoryFile will fail.
	*
	* @param fd File descriptor for an existing memory file, as returned by
	*        {@link #getFileDescriptor()}. This file descriptor will be closed
	*        by {@link #close()}.
	* @param length Length of the memory file in bytes.
	* @param mode File mode. Currently only "r" for read-only access is supported.
	* @throws NullPointerException if <code>fd</code> is null.
	* @throws IOException If <code>fd</code> does not refer to an existing memory file,
	*         or if the file mode of the existing memory file is more restrictive
	*         than <code>mode</code>.
	*
	* @hide
	*/
	public MemoryFile(FileDescriptor fd, int length, String mode) throws IOException {
		if (fd == null) {
			throw new NullPointerException("File descriptor is null.");
		}
		if (!isMemoryFile(fd)) {
			throw new IllegalArgumentException("Not a memory file.");
		}
		mLength = length;
		mFD = fd;
		mAddress = native_mmap(mFD, length, modeToProt(mode));
		mOwnsRegion = false;
	}

	......
}
从注释中,我们可以看出这两个构造函数的使用方法,这里就不再详述了。两个构造函数的主要区别是第一个参数,第一种构造方法是以指定的字符串调用JNI方法native_open来创建一个匿名共享内存文件,从而得到一个文件描述符,接着就以这个文件描述符为参数调用JNI方法natvie_mmap把这个匿名共享内存文件映射在进程空间中,然后就可以通过这个映射后得到的地址空间来直接访问内存数据了;第二种构造方法是以指定的文件描述符来直接调用JNI方法natvie_mmap把这个匿名共享内存文件映射在进程空间中,然后进行访问,而这个文件描述符就必须要是一个匿名共享内存文件的文件描述符,这是通过一个内部函数isMemoryFile来验证的,而这个内部函数isMemoryFile也是通过JNI方法调用来进一步验证的。前面所提到的这些JNI方法调用,最终都是通过系统运行时库层进入到内核空间的Ashmem驱动程序中去,不过这里我们不关心这些JNI方法、系统运行库层调用以及Ashmem驱动程序的具体实现,在接下来的两篇文章中,我们将会着重介绍,这里我们只关注MemoryFile这个类的使用方法。

前面我们说到,我们在这里举的例子包含了一个Server端和一个Client端实现,其中, Server端就是通过前面一个构造函数来创建一个匿名共享内存文件,接着,Client端过Binder进程间通信机制来向Server请求这个匿名共享内存的文件描述符,有了这个文件描述符之后,就可以通过后面一个构造函数来共享这个内存文件了。

因为涉及到Binder进程间通信,我们首先定义好Binder进程间通信接口。Binder进程间通信机制的相关介绍,请参考前面一篇文章Android进程间通信(IPC)机制Binder简要介绍和学习计划,这里就不详细介绍了,直接进入主题。
首先在源代码工程的packages/experimental目录下创建一个应用程序工程目录Ashmem。关于如何获得Android源代码工程,请参考在Ubuntu上下载、编译和安装Android最新源代码一文;关于如何在Android源代码工程中创建应用程序工程,请参考在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务一文。这里,工程名称就是Ashmem了,它定义了一个路径为shy.luo.ashmem的package,这个例子的源代码主要就是实现在这里了。下面,将会逐一介绍这个package里面的文件。

这里要用到的Binder进程间通信接口定义在src/shy/luo/ashmem/IMemoryService.java文件中:

  1. packageshy.luo.ashmem;
  2. importandroid.util.Log;
  3. importandroid.os.IInterface;
  4. importandroid.os.Binder;
  5. importandroid.os.IBinder;
  6. importandroid.os.Parcel;
  7. importandroid.os.ParcelFileDescriptor;
  8. importandroid.os.RemoteException;
  9. publicinterfaceIMemoryServiceextendsIInterface{
  10. publicstaticabstractclassStubextendsBinderimplementsIMemoryService{
  11. privatestaticfinalStringDESCRIPTOR="shy.luo.ashmem.IMemoryService";
  12. publicStub(){
  13. attachInterface(this,DESCRIPTOR);
  14. }
  15. publicstaticIMemoryServiceasInterface(IBinderobj){
  16. if(obj==null){
  17. returnnull;
  18. }
  19. IInterfaceiin=(IInterface)obj.queryLocalInterface(DESCRIPTOR);
  20. if(iin!=null&&iininstanceofIMemoryService){
  21. return(IMemoryService)iin;
  22. }
  23. returnnewIMemoryService.Stub.Proxy(obj);
  24. }
  25. publicIBinderasBinder(){
  26. returnthis;
  27. }
  28. @Override
  29. publicbooleanonTransact(intcode,Parceldata,Parcelreply,intflags)throwsandroid.os.RemoteException{
  30. switch(code){
  31. caseINTERFACE_TRANSACTION:{
  32. reply.writeString(DESCRIPTOR);
  33. returntrue;
  34. }
  35. caseTRANSACTION_getFileDescriptor:{
  36. data.enforceInterface(DESCRIPTOR);
  37. ParcelFileDescriptorresult=this.getFileDescriptor();
  38. reply.writeNoException();
  39. if(result!=null){
  40. reply.writeInt(1);
  41. result.writeToParcel(reply,0);
  42. }else{
  43. reply.writeInt(0);
  44. }
  45. returntrue;
  46. }
  47. caseTRANSACTION_setValue:{
  48. data.enforceInterface(DESCRIPTOR);
  49. intval=data.readInt();
  50. setValue(val);
  51. reply.writeNoException();
  52. returntrue;
  53. }
  54. }
  55. returnsuper.onTransact(code,data,reply,flags);
  56. }
  57. privatestaticclassProxyimplementsIMemoryService{
  58. privateIBindermRemote;
  59. Proxy(IBinderremote){
  60. mRemote=remote;
  61. }
  62. publicIBinderasBinder(){
  63. returnmRemote;
  64. }
  65. publicStringgetInterfaceDescriptor(){
  66. returnDESCRIPTOR;
  67. }
  68. publicParcelFileDescriptorgetFileDescriptor()throwsRemoteException{
  69. Parceldata=Parcel.obtain();
  70. Parcelreply=Parcel.obtain();
  71. ParcelFileDescriptorresult;
  72. try{
  73. data.writeInterfaceToken(DESCRIPTOR);
  74. mRemote.transact(Stub.TRANSACTION_getFileDescriptor,data,reply,0);
  75. reply.readException();
  76. if(0!=reply.readInt()){
  77. result=ParcelFileDescriptor.CREATOR.createFromParcel(reply);
  78. }else{
  79. result=null;
  80. }
  81. }finally{
  82. reply.recycle();
  83. data.recycle();
  84. }
  85. returnresult;
  86. }
  87. publicvoidsetValue(intval)throwsRemoteException{
  88. Parceldata=Parcel.obtain();
  89. Parcelreply=Parcel.obtain();
  90. try{
  91. data.writeInterfaceToken(DESCRIPTOR);
  92. data.writeInt(val);
  93. mRemote.transact(Stub.TRANSACTION_setValue,data,reply,0);
  94. reply.readException();
  95. }finally{
  96. reply.recycle();
  97. data.recycle();
  98. }
  99. }
  100. }
  101. staticfinalintTRANSACTION_getFileDescriptor=IBinder.FIRST_CALL_TRANSACTION+0;
  102. staticfinalintTRANSACTION_setValue=IBinder.FIRST_CALL_TRANSACTION+1;
  103. }
  104. publicParcelFileDescriptorgetFileDescriptor()throwsRemoteException;
  105. publicvoidsetValue(intval)throwsRemoteException;
  106. }
package shy.luo.ashmem;

import android.util.Log;
import android.os.IInterface;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;

public interface IMemoryService extends IInterface {
	public static abstract class Stub extends Binder implements IMemoryService {
		private static final String DESCRIPTOR = "shy.luo.ashmem.IMemoryService";

		public Stub() {
			attachInterface(this, DESCRIPTOR);
		}

		public static IMemoryService asInterface(IBinder obj) {
			if (obj == null) {
				return null;
			}

			IInterface iin = (IInterface)obj.queryLocalInterface(DESCRIPTOR);
			if (iin != null && iin instanceof IMemoryService) {
				return (IMemoryService)iin;
			}

			return new IMemoryService.Stub.Proxy(obj);
		}

		public IBinder asBinder() {
			return this;
		}

		@Override 
		public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws android.os.RemoteException {
			switch (code) {
			case INTERFACE_TRANSACTION: {
				reply.writeString(DESCRIPTOR);
				return true;
			}
			case TRANSACTION_getFileDescriptor: {
				data.enforceInterface(DESCRIPTOR);
				
				ParcelFileDescriptor result = this.getFileDescriptor();
				
				reply.writeNoException();
				
				if (result != null) {
					reply.writeInt(1);
					result.writeToParcel(reply, 0);
				} else {
					reply.writeInt(0);
				}

				return true;
			}
			case TRANSACTION_setValue: {
				data.enforceInterface(DESCRIPTOR);
				
				int val = data.readInt();
				setValue(val);
				
				reply.writeNoException();
				
				return true;
			}
			}

			return super.onTransact(code, data, reply, flags);
		}

		private static class Proxy implements IMemoryService {
			private IBinder mRemote;

			Proxy(IBinder remote) {
				mRemote = remote;
			}

			public IBinder asBinder() {
				return mRemote;
			}

			public String getInterfaceDescriptor() {
				return DESCRIPTOR;
			}

			public ParcelFileDescriptor getFileDescriptor() throws RemoteException {
				Parcel data = Parcel.obtain();
				Parcel reply = Parcel.obtain();

				ParcelFileDescriptor result;
	
				try {
					data.writeInterfaceToken(DESCRIPTOR);

					mRemote.transact(Stub.TRANSACTION_getFileDescriptor, data, reply, 0);
		
					reply.readException();
					if (0 != reply.readInt()) {
						result = ParcelFileDescriptor.CREATOR.createFromParcel(reply);
					} else {
						result = null;
					}
				} finally {
					reply.recycle();
					data.recycle();
				}
	
				return result;
			}

			public void setValue(int val) throws RemoteException {
				Parcel data = Parcel.obtain();
				Parcel reply = Parcel.obtain();

				try {
					data.writeInterfaceToken(DESCRIPTOR);
					data.writeInt(val);

					mRemote.transact(Stub.TRANSACTION_setValue, data, reply, 0);
					
					reply.readException();
				} finally {
					reply.recycle();
					data.recycle();
				}
			}
		}

		static final int TRANSACTION_getFileDescriptor = IBinder.FIRST_CALL_TRANSACTION + 0;
		static final int TRANSACTION_setValue = IBinder.FIRST_CALL_TRANSACTION + 1;

	}

	public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
	public void setValue(int val) throws RemoteException;
}
这里主要是定义了IMemoryService接口,它里面有两个调用接口:

  1. publicParcelFileDescriptorgetFileDescriptor()throwsRemoteException;
  2. publicvoidsetValue(intval)throwsRemoteException;
	public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
	public void setValue(int val) throws RemoteException;
同时,还分别定义了用于Server端实现的IMemoryService.Stub基类和用于Client端使用的代理IMemoryService.Stub.Proxy类。关于Binder进程间通信机制在应用程序框架层的Java接口定义,请参考前面 Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析一文。

有了Binder进程间通信接口之后,接下来就是要在Server端实现一个本地服务了。这里,Server端实现的本地服务名为MemoryService,实现在src/shy/luo/ashmem/MemoryService.java文件中:

  1. packageshy.luo.ashmem;
  2. importjava.io.FileDescriptor;
  3. importjava.io.IOException;
  4. importandroid.os.Parcel;
  5. importandroid.os.MemoryFile;
  6. importandroid.os.ParcelFileDescriptor;
  7. importandroid.util.Log;
  8. publicclassMemoryServiceextendsIMemoryService.Stub{
  9. privatefinalstaticStringLOG_TAG="shy.luo.ashmem.MemoryService";
  10. privateMemoryFilefile=null;
  11. publicMemoryService(){
  12. try{
  13. file=newMemoryFile("Ashmem",4);
  14. setValue(0);
  15. }
  16. catch(IOExceptionex){
  17. Log.i(LOG_TAG,"Failedtocreatememoryfile.");
  18. ex.printStackTrace();
  19. }
  20. }
  21. publicParcelFileDescriptorgetFileDescriptor(){
  22. Log.i(LOG_TAG,"GetFileDescriptor.");
  23. ParcelFileDescriptorpfd=null;
  24. try{
  25. pfd=file.getParcelFileDescriptor();
  26. }catch(IOExceptionex){
  27. Log.i(LOG_TAG,"Failedtogetfiledescriptor.");
  28. ex.printStackTrace();
  29. }
  30. returnpfd;
  31. }
  32. publicvoidsetValue(intval){
  33. if(file==null){
  34. return;
  35. }
  36. byte[]buffer=newbyte[4];
  37. buffer[0]=(byte)((val>>>24)&0xFF);
  38. buffer[1]=(byte)((val>>>16)&0xFF);
  39. buffer[2]=(byte)((val>>>8)&0xFF);
  40. buffer[3]=(byte)(val&0xFF);
  41. try{
  42. file.writeBytes(buffer,0,0,4);
  43. Log.i(LOG_TAG,"Setvalue"+val+"tomemoryfile.");
  44. }
  45. catch(IOExceptionex){
  46. Log.i(LOG_TAG,"Failedtowritebytestomemoryfile.");
  47. ex.printStackTrace();
  48. }
  49. }
  50. }
package shy.luo.ashmem;

import java.io.FileDescriptor;
import java.io.IOException;

import android.os.Parcel;
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
import android.util.Log;

public class MemoryService extends IMemoryService.Stub {
	private final static String LOG_TAG = "shy.luo.ashmem.MemoryService";
	private MemoryFile file = null;
	
	public MemoryService() {
		try {
                        file = new MemoryFile("Ashmem", 4);
                        setValue(0);
                }
                catch(IOException ex) {
                        Log.i(LOG_TAG, "Failed to create memory file.");
                        ex.printStackTrace();
                }
	}

	public ParcelFileDescriptor getFileDescriptor() {
		Log.i(LOG_TAG, "Get File Descriptor.");

		ParcelFileDescriptor pfd = null;

		try {
			pfd = file.getParcelFileDescriptor();
		} catch(IOException ex) {
			Log.i(LOG_TAG, "Failed to get file descriptor.");
			ex.printStackTrace();
		}

		return pfd;
	}
	
	public void setValue(int val) {
		if(file == null) {
			return;
		}

		byte[] buffer = new byte[4];   
		buffer[0] = (byte)((val >>> 24) & 0xFF);
		buffer[1] = (byte)((val >>> 16) & 0xFF);
		buffer[2] = (byte)((val >>> 8) & 0xFF); 
		buffer[3] = (byte)(val & 0xFF);
		
		try {
			file.writeBytes(buffer, 0, 0, 4);
			Log.i(LOG_TAG, "Set value " + val + " to memory file. ");
		}
		catch(IOException ex) {
			Log.i(LOG_TAG, "Failed to write bytes to memory file.");
			ex.printStackTrace();
		}
	}
}
注意,这里的MemoryService类实现了IMemoryService.Stub类,表示这是一个Binder服务的本地实现。在构造函数中,通过指定文件名和文件大小来创建了一个匿名共享内存文件,即创建MemoryFile的一个实例,并保存在类成员变量file中。这个匿名共享内存文件名为"Ashmem",大小为4个节字,刚好容纳一个整数,我们这里举的例子就是要说明如果创建一个匿名共享内存来在两个进程间实现共享一个整数了。当然,在实际应用中,可以根据需要创建合适大小的共享内存来共享有意义的数据。

这里还实现了IMemoryService.Stub的两个接口getFileDescriptor和setVal,一个用来获取匿名共享内存文件的文件描述符,一个来往匿名共享内存文件中写入一个整数,其中,接口getFileDescriptor的返回值是一个ParcelFileDescriptor。在Java中,是用FileDescriptor类来表示一个文件描述符的,而ParcelFileDescriptor是用来序列化FileDescriptor的,以便在进程间调用时传输。
定义好本地服务好,就要定义一个Server来启动这个服务了。这里定义的Server实现在src/shy/luo/ashmem/Server.java文件中:

  1. packageshy.luo.ashmem;
  2. importandroid.app.Service;
  3. importandroid.content.Intent;
  4. importandroid.os.IBinder;
  5. importandroid.util.Log;
  6. importandroid.os.ServiceManager;
  7. publicclassServerextendsService{
  8. privatefinalstaticStringLOG_TAG="shy.luo.ashmem.Server";
  9. privateMemoryServicememoryService=null;
  10. @Override
  11. publicIBinderonBind(Intentintent){
  12. returnnull;
  13. }
  14. @Override
  15. publicvoidonCreate(){
  16. Log.i(LOG_TAG,"CreateMemoryService...");
  17. memoryService=newMemoryService();
  18. try{
  19. ServiceManager.addService("AnonymousSharedMemory",memoryService);
  20. Log.i(LOG_TAG,"Succeedtoaddmemoryservice.");
  21. }catch(RuntimeExceptionex){
  22. Log.i(LOG_TAG,"FailedtoaddMemoryService.");
  23. ex.printStackTrace();
  24. }
  25. }
  26. @Override
  27. publicvoidonStart(Intentintent,intstartId){
  28. Log.i(LOG_TAG,"StartMemoryService.");
  29. }
  30. @Override
  31. publicvoidonDestroy(){
  32. Log.i(LOG_TAG,"DestroyMemoryService.");
  33. }
  34. }
package shy.luo.ashmem;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.os.ServiceManager;

public class Server extends Service {
    private final static String LOG_TAG = "shy.luo.ashmem.Server";

    private MemoryService memoryService = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
	Log.i(LOG_TAG, "Create Memory Service...");

	memoryService = new MemoryService();

        try {
            ServiceManager.addService("AnonymousSharedMemory", memoryService);
            Log.i(LOG_TAG, "Succeed to add memory service.");
        } catch (RuntimeException ex) {
            Log.i(LOG_TAG, "Failed to add Memory Service.");
            ex.printStackTrace();
        }

    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.i(LOG_TAG, "Start Memory Service.");
    }

    @Override
    public void onDestroy() {
	Log.i(LOG_TAG, "Destroy Memory Service.");
    }
}
这个Server继承了Android系统应用程序框架层提供的Service类,当它被启动时,运行在一个独立的进程中。当这个Server被启动时,它的onCreate函数就会被调用,然后它就通过ServiceManager的addService接口来添加MemoryService了:

  1. memoryService=newMemoryService();
  2. try{
  3. ServiceManager.addService("AnonymousSharedMemory",memoryService);
  4. Log.i(LOG_TAG,"Succeedtoaddmemoryservice.");
  5. }catch(RuntimeExceptionex){
  6. Log.i(LOG_TAG,"FailedtoaddMemoryService.");
  7. ex.printStackTrace();
  8. }
    memoryService = new MemoryService();

    try {
        ServiceManager.addService("AnonymousSharedMemory", memoryService);
        Log.i(LOG_TAG, "Succeed to add memory service.");
    } catch (RuntimeException ex) {
        Log.i(LOG_TAG, "Failed to add Memory Service.");
        ex.printStackTrace();
    }
这样,当这个Server成功启动了,Client就可以通过ServiceManager的getService接口来获取这个MemoryService了。

接着,我们就来看Client端的实现。Client端是一个Activity,实现在src/shy/luo/ashmem/Client.java文件中:

  1. packageshy.luo.ashmem;
  2. importjava.io.FileDescriptor;
  3. importjava.io.IOException;
  4. importshy.luo.ashmem.R;
  5. importandroid.app.Activity;
  6. importandroid.content.Intent;
  7. importandroid.os.Bundle;
  8. importandroid.os.MemoryFile;
  9. importandroid.os.ParcelFileDescriptor;
  10. importandroid.os.ServiceManager;
  11. importandroid.os.RemoteException;
  12. importandroid.util.Log;
  13. importandroid.view.View;
  14. importandroid.view.View.OnClickListener;
  15. importandroid.widget.Button;
  16. importandroid.widget.EditText;
  17. publicclassClientextendsActivityimplementsOnClickListener{
  18. privatefinalstaticStringLOG_TAG="shy.luo.ashmem.Client";
  19. IMemoryServicememoryService=null;
  20. MemoryFilememoryFile=null;
  21. privateEditTextvalueText=null;
  22. privateButtonreadButton=null;
  23. privateButtonwriteButton=null;
  24. privateButtonclearButton=null;
  25. @Override
  26. publicvoidonCreate(BundlesavedInstanceState){
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. IMemoryServicems=getMemoryService();
  30. if(ms==null){
  31. startService(newIntent("shy.luo.ashmem.server"));
  32. }else{
  33. Log.i(LOG_TAG,"MemoryServicehasstarted.");
  34. }
  35. valueText=(EditText)findViewById(R.id.edit_value);
  36. readButton=(Button)findViewById(R.id.button_read);
  37. writeButton=(Button)findViewById(R.id.button_write);
  38. clearButton=(Button)findViewById(R.id.button_clear);
  39. readButton.setOnClickListener(this);
  40. writeButton.setOnClickListener(this);
  41. clearButton.setOnClickListener(this);
  42. Log.i(LOG_TAG,"ClientActivityCreated.");
  43. }
  44. @Override
  45. publicvoidonResume(){
  46. super.onResume();
  47. Log.i(LOG_TAG,"ClientActivityResumed.");
  48. }
  49. @Override
  50. publicvoidonPause(){
  51. super.onPause();
  52. Log.i(LOG_TAG,"ClientActivityPaused.");
  53. }
  54. @Override
  55. publicvoidonClick(Viewv){
  56. if(v.equals(readButton)){
  57. intval=0;
  58. MemoryFilemf=getMemoryFile();
  59. if(mf!=null){
  60. try{
  61. byte[]buffer=newbyte[4];
  62. mf.readBytes(buffer,0,0,4);
  63. val=(buffer[0]<<24)|((buffer[1]&0xFF)<<16)|((buffer[2]&0xFF)<<8)|(buffer[3]&0xFF);
  64. }catch(IOExceptionex){
  65. Log.i(LOG_TAG,"Failedtoreadbytesfrommemoryfile.");
  66. ex.printStackTrace();
  67. }
  68. }
  69. Stringtext=String.valueOf(val);
  70. valueText.setText(text);
  71. }elseif(v.equals(writeButton)){
  72. Stringtext=valueText.getText().toString();
  73. intval=Integer.parseInt(text);
  74. IMemoryServicems=getMemoryService();
  75. if(ms!=null){
  76. try{
  77. ms.setValue(val);
  78. }catch(RemoteExceptionex){
  79. Log.i(LOG_TAG,"Failedtosetvaluetomemoryservice.");
  80. ex.printStackTrace();
  81. }
  82. }
  83. }elseif(v.equals(clearButton)){
  84. Stringtext="";
  85. valueText.setText(text);
  86. }
  87. }
  88. privateIMemoryServicegetMemoryService(){
  89. if(memoryService!=null){
  90. returnmemoryService;
  91. }
  92. memoryService=IMemoryService.Stub.asInterface(
  93. ServiceManager.getService("AnonymousSharedMemory"));
  94. Log.i(LOG_TAG,memoryService!=null?"Succeedtogetmemeoryservice.":"Failedtogetmemoryservice.");
  95. returnmemoryService;
  96. }
  97. privateMemoryFilegetMemoryFile(){
  98. if(memoryFile!=null){
  99. returnmemoryFile;
  100. }
  101. IMemoryServicems=getMemoryService();
  102. if(ms!=null){
  103. try{
  104. ParcelFileDescriptorpfd=ms.getFileDescriptor();
  105. if(pfd==null){
  106. Log.i(LOG_TAG,"Failedtogetmemoryfiledescriptor.");
  107. returnnull;
  108. }
  109. try{
  110. FileDescriptorfd=pfd.getFileDescriptor();
  111. if(fd==null){
  112. Log.i(LOG_TAG,"Failedtogetmemeoryfiledescriptor.");
  113. returnnull;
  114. }
  115. memoryFile=newMemoryFile(fd,4,"r");
  116. }catch(IOExceptionex){
  117. Log.i(LOG_TAG,"Failedtocreatememoryfile.");
  118. ex.printStackTrace();
  119. }
  120. }catch(RemoteExceptionex){
  121. Log.i(LOG_TAG,"Failedtogetfiledescriptorfrommemoryservice.");
  122. ex.printStackTrace();
  123. }
  124. }
  125. returnmemoryFile;
  126. }
  127. }
package shy.luo.ashmem;

import java.io.FileDescriptor;
import java.io.IOException;

import shy.luo.ashmem.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
import android.os.ServiceManager;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Client extends Activity implements OnClickListener {
	private final static String LOG_TAG = "shy.luo.ashmem.Client";
	
	IMemoryService memoryService = null;
	MemoryFile memoryFile = null;
	
	private EditText valueText = null;
	private Button readButton = null;
	private Button writeButton = null;
	private Button clearButton = null;
	
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
        	super.onCreate(savedInstanceState);
        	setContentView(R.layout.main);

		IMemoryService ms = getMemoryService();
		if(ms == null) {        
        		startService(new Intent("shy.luo.ashmem.server"));
		} else {
			Log.i(LOG_TAG, "Memory Service has started.");
		}

        	valueText = (EditText)findViewById(R.id.edit_value);
        	readButton = (Button)findViewById(R.id.button_read);
        	writeButton = (Button)findViewById(R.id.button_write);
        	clearButton = (Button)findViewById(R.id.button_clear);

		readButton.setOnClickListener(this);
        	writeButton.setOnClickListener(this);
        	clearButton.setOnClickListener(this);
        
        	Log.i(LOG_TAG, "Client Activity Created.");
    	}

    	@Override
    	public void onResume() {
		super.onResume();

		Log.i(LOG_TAG, "Client Activity Resumed.");
    	}

    	@Override
    	public void onPause() {
		super.onPause();

		Log.i(LOG_TAG, "Client Activity Paused.");
    	}
    
    	@Override
    	public void onClick(View v) {
    		if(v.equals(readButton)) {
    			int val = 0;
    		
    			MemoryFile mf = getMemoryFile();
    			if(mf != null) {
				try {
    					byte[] buffer = new byte[4];
    					mf.readBytes(buffer, 0, 0, 4);
    			
    					val = (buffer[0] << 24) | ((buffer[1] & 0xFF) << 16) | ((buffer[2] & 0xFF) << 8) | (buffer[3] & 0xFF);
				} catch(IOException ex) {
					Log.i(LOG_TAG, "Failed to read bytes from memory file.");
					ex.printStackTrace();
				}
    			}	
    		
    			String text = String.valueOf(val);
    			valueText.setText(text);
    		} else if(v.equals(writeButton)) {
    			String text = valueText.getText().toString();
    			int val = Integer.parseInt(text);
    		
    			IMemoryService ms = getMemoryService();
    			if(ms != null) {
				try {
    					ms.setValue(val);
				} catch(RemoteException ex) {
					Log.i(LOG_TAG, "Failed to set value to memory service.");
					ex.printStackTrace();
				}
    			}
    		} else if(v.equals(clearButton)) {
    			String text = "";
    			valueText.setText(text);
    		}
    	}
    
    	private IMemoryService getMemoryService() {
    		if(memoryService != null) {
    			return memoryService;
    		}
    	
    		memoryService = IMemoryService.Stub.asInterface(
                			ServiceManager.getService("AnonymousSharedMemory"));

		Log.i(LOG_TAG, memoryService != null ? "Succeed to get memeory service." : "Failed to get memory service.");
    	
    		return memoryService;
    	}
    
    	private MemoryFile getMemoryFile() {
    		if(memoryFile != null) {
    			return memoryFile;
    		}
    		
    		IMemoryService ms = getMemoryService();
    		if(ms != null) {
			try {
    				ParcelFileDescriptor pfd = ms.getFileDescriptor();
				if(pfd == null) {
					Log.i(LOG_TAG, "Failed to get memory file descriptor.");
					return null;
				}

				try {
					FileDescriptor fd = pfd.getFileDescriptor();
					if(fd == null) {
						Log.i(LOG_TAG, "Failed to get memeory file descriptor.");
						return null;                      
					}	

    					memoryFile = new MemoryFile(fd, 4, "r");
				} catch(IOException ex) {
					Log.i(LOG_TAG, "Failed to create memory file.");
					ex.printStackTrace();
				}
    			} catch(RemoteException ex) {
				Log.i(LOG_TAG, "Failed to get file descriptor from memory service.");
				ex.printStackTrace();
			}
		}
    	
    		return memoryFile;
    	}
}
Client端的界面主要包含了三个按钮Read、Write和Clear,以及一个用于显示内容的文本框。

这个Activity在onCreate时,会通过startService接口来启动我们前面定义的Server进程。调用startService时,需要指定要启动的服务的名称,这里就是"shy.luo.ashmem.server"了,后面我们会在程序的描述文件AndroidManifest.xml看到前面的Server类是如何和名称"shy.luo.ashmem.server"关联起来的。关于调用startService函数来启动自定义服务的过程,可以参考Android系统在新进程中启动自定义服务过程(startService)的原理分析一文。

内部函数getMemoryService用来获取IMemoryService。如果是第一次调用该函数,则会通过ServiceManager的getService接口来获得这个IMemoryService接口,然后保存在类成员变量memoryService中,以后再调用这个函数时,就可以直接返回memoryService了。

内部函数getMemoryFile用来从MemoryService中获得匿名共享内存文件的描述符。同样,如果是第一次调用该函数,则会通过IMemoryService的getFileDescriptor接口来获得MemoryService中的匿名共享内存文件的描述符,然后用这个文件描述符来创建一个MemoryFile实例,并保存在类成员变量memoryFile中,以后再调用这个函数时,就可以直接返回memoryFile了。

有了memoryService和memoryFile后,我们就可以在Client端访问Server端创建的匿名共享内存了。点击Read按钮时,就通过memoryFile的readBytes接口把共享内存中的整数读出来,并显示在文本框中;点击Write按钮时,就通过memoryService这个代理类的setVal接口来调用MemoryService的本地实现类的setVal服务,从而把文本框中的数值写到Server端创建的匿名共享内存中去;点击Clear按钮时,就会清空文本框的内容。这样,我们就可以通过Read和Write按钮来验证我们是否在Client和Server两个进程中实现内存共享了。

现在,我们再来看看Client界面的配置文件,它定义在res/layout/main.xml文件中:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. android:gravity="center">
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="@string/value">
  16. </TextView>
  17. <EditText
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:id="@+id/edit_value"
  21. android:hint="@string/hint">
  22. </EditText>
  23. </LinearLayout>
  24. <LinearLayout
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:orientation="horizontal"
  28. android:gravity="center">
  29. <Button
  30. android:id="@+id/button_read"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="@string/read">
  34. </Button>
  35. <Button
  36. android:id="@+id/button_write"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="@string/write">
  40. </Button>
  41. <Button
  42. android:id="@+id/button_clear"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="@string/clear">
  46. </Button>
  47. </LinearLayout>
  48. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:orientation="vertical" 
    	android:gravity="center">
    	<TextView 
			android:layout_width="wrap_content"
       		android:layout_height="wrap_content" 
        	android:text="@string/value">
    	</TextView>
    	<EditText 
     		android:layout_width="fill_parent"
        	android:layout_height="wrap_content" 
        	android:id="@+id/edit_value"
        	android:hint="@string/hint">
    	</EditText>
    </LinearLayout>
     <LinearLayout
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal" 
    	android:gravity="center">
    	<Button 
    		android:id="@+id/button_read"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/read">
    	</Button>
    	<Button 
    		android:id="@+id/button_write"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/write">
    	</Button>
    	<Button 
    		android:id="@+id/button_clear"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/clear">
    	</Button>
    </LinearLayout>
</LinearLayout>
相关的字符串定义在res/values/strings.xml文件中:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">Ashmem</string>
  4. <stringname="value">Value</string>
  5. <stringname="hint">Pleaseinputavalue...</string>
  6. <stringname="read">Read</string>
  7. <stringname="write">Write</string>
  8. <stringname="clear">Clear</string>
  9. </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">Ashmem</string>
    <string name="value">Value</string>
    <string name="hint">Please input a value...</string>
    <string name="read">Read</string>
    <string name="write">Write</string>
    <string name="clear">Clear</string>
</resources>
这样,界面的相关配置文件就介绍完了。

我们还要再来看程序描述文件AndroidManifest.xml的相关配置,它位于Ashmem目录下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="shy.luo.ashmem"
  4. android:sharedUserId="android.uid.system"
  5. android:versionCode="1"
  6. android:versionName="1.0">
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".Client"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. <service
  16. android:enabled="true"
  17. android:name=".Server"
  18. android:process=".Server">
  19. <intent-filter>
  20. <actionandroid:name="shy.luo.ashmem.server"/>
  21. <categoryandroid:name="android.intent.category.DEFAULT"/>
  22. </intent-filter>
  23. </service>
  24. </application>
  25. </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="shy.luo.ashmem"
      android:sharedUserId="android.uid.system"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Client"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<service 
			android:enabled="true" 
			android:name=".Server"
			android:process=".Server" >
			<intent-filter>
                <action android:name="shy.luo.ashmem.server"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
		</service>
    </application>
</manifest>
这里我们可以看到,下面的配置项把服务名称"shy.luo.ashmem.server"和本地服务类Server关联了起来:

  1. <service
  2. android:enabled="true"
  3. android:name=".Server"
  4. android:process=".Server">
  5. <intent-filter>
  6. <actionandroid:name="shy.luo.ashmem.server"/>
  7. <categoryandroid:name="android.intent.category.DEFAULT"/>
  8. </intent-filter>
  9. </service>
    <service 
	android:enabled="true" 
	android:name=".Server"
	android:process=".Server" >
	<intent-filter>
             <action android:name="shy.luo.ashmem.server"/>
             <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </service>
这样,我们就可以通过startService(new Intent("shy.luo.ashmem.server"))来启动这个Server了。不过,在Android中,启动服务是需要权限的,所以,下面这一行配置获取了启动服务需要的相应权限:

  1. android:sharedUserId="android.uid.system"
    android:sharedUserId="android.uid.system"
最后,我们来看工程的编译脚本文件Android.mk,它位于Ashmem目录下:
  1. LOCAL_PATH:=$(callmy-dir)
  2. include$(CLEAR_VARS)
  3. LOCAL_MODULE_TAGS:=optional
  4. LOCAL_SRC_FILES+=$(callall-subdir-java-files)
  5. LOCAL_PACKAGE_NAME:=Ashmem
  6. LOCAL_CERTIFICATE:=platform
  7. include$(BUILD_PACKAGE)
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES += $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := Ashmem

LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)
这里又有一个关键的地方:

  1. LOCAL_CERTIFICATE:=platform
LOCAL_CERTIFICATE := platform
因为我们需要在程序中启动Service,所以要配置这一行,并且要把源代码工程放在Android源代码平台中进行编译。

这样,整个例子的源代码实现就介绍完了,接下来就要编译了。有关如何单独编译Android源代码工程的模块,以及如何打包system.img,请参考如何单独编译Android源代码中的模块一文。

执行以下命令进行编译和打包:

  1. USER-NAME@MACHINE-NAME:~/Android$mmmpackages/experimental/Ashmem
  2. USER-NAME@MACHINE-NAME:~/Android$makesnod
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Ashmem
USER-NAME@MACHINE-NAME:~/Android$ make snod
这样,打包好的Android系统镜像文件system.img就包含我们前面创建的Ashmem应用程序了。

再接下来,就是运行模拟器来运行我们的例子了。关于如何在Android源代码工程中运行模拟器,请参考在Ubuntu上下载、编译和安装Android最新源代码一文。

执行以下命令启动模拟器:

  1. USER-NAME@MACHINE-NAME:~/Android$emulator
USER-NAME@MACHINE-NAME:~/Android$ emulator
模拟器启动起,就可以在Home Screen上看到Ashmem应用程序图标了:


点击Ashmem图标,启动Ashmem应用程序,界面如下:


这样,我们就可以验证程序的功能了,看看是否实现了在两个进程中通过使用Android系统的匿名共享内存机制来共享内存数据的功能。

通过这个例子的学习,相信读者对Android系统匿名共享内存子系统Ashmem有了一个大概的认识,但是,这种认识还是停留在表面上。我们在文章开始时就提到,Android系统匿名共享内存子系统Ashmem两个特点,一是能够辅助内存管理系统来有效地管理不再使用的内存块,二是它通过Binder进程间通信机制来实现进程间的内存共享。第二个特点我们在上面这个例子中看到了,但是似乎还不够深入,我们知道,在Linux系统中,文件描述符其实就是一个整数,它是用来索引进程保存在内核空间的打开文件数据结构的,而且,这个文件描述符只是在进程内有效,也就是说,在不同的进程中,相同的文件描述符的值,代表的可能是不同的打开文件,既然是这样,把Server进程中的文件描述符传给Client进程,似乎就没有用了,但是不用担心,在传输过程中,Binder驱动程序会帮我们处理好一切,保证Client进程拿到的文件描述符是在本进程中有效的,并且它指向就是Server进程创建的匿名共享内存文件。至于第一个特点,我们也准备在后续学习Android系统匿名共享内存子系统Ashmem时,再详细介绍。

因此,为了深入了解Android系统匿名共享内存子系统Ashmem,在接下来的两篇文章中,围绕上面提到的两个特点,分别学习:

1. Android系统匿名共享内存子系统Ashmem是如何够辅助内存管理系统来有效地管理不再使用的内存块的?

2.Android系统匿名共享内存子系统Ashmem是如何通过Binder进程间通信机制来实现进程间的内存共享的?

学习完这两篇文章后,相信大家对Android系统匿名共享内存子系统Ashmem就会有一个更深刻的认识了,敬请关注。

原博客链接:http://blog.youkuaiyun.com/luoshengyang/article/details/6651971

本文节选自《Android系统源代码情景分析》

电子工业出版社出版

罗升阳 著

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值