Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (二)
這裡要用到的Binder程式間通訊介面定義在src/shy/luo/ashmem/IMemoryService.java檔案中:
- 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;
- }
- 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;
- }
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介面,它裡面有兩個呼叫介面:
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
- public void setValue(int val) throws RemoteException;
- public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
- public void setValue(int val) throws RemoteException;
public ParcelFileDescriptor getFileDescriptor() throws RemoteException; public void setValue(int val) throws RemoteException;同時,還分別定義了用於Server端實現的IMemoryService.Stub基類和用於Client端使用的代理IMemoryService.Stub.Proxy類。關於Binder程式間通訊機制在應用程式框架層的Java介面定義,請參考前面Android系統程式間通訊Binder機制在應用程式框架層的Java介面原始碼分析一文。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13164110/viewspace-747972/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃Android記憶體
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (五)Android記憶體
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (四)Android記憶體
- Android系統匿名共享記憶體Ashmem(Anonymous Shared Memory)簡要介紹和學習計劃 (三)Android記憶體
- Android匿名共享記憶體(Ashmem)原理Android記憶體
- Android系統中的廣播(Broadcast)機制簡要介紹和學習計劃 .AndroidAST
- Dalvik虛擬機器簡要介紹和學習計劃虛擬機
- Android程式間通訊(IPC)機制Binder簡要介紹和學習計劃Android
- ASMM (Auto Shared Memory Manangement) 自動共享記憶體管理ASMNaN記憶體
- ART執行時垃圾收集機制簡要介紹和學習計劃
- Dalvik虛擬機器垃圾收集機制簡要介紹和學習計劃虛擬機
- javascript記憶體管理簡單介紹JavaScript記憶體
- javascript 記憶體使用管理簡單介紹JavaScript記憶體
- 計算機記憶體管理介紹計算機記憶體
- Linux共享記憶體(二)Linux記憶體
- memory儲存引擎 /MySQL記憶體表的特性與使用介紹儲存引擎MySql記憶體
- Linux系統程式設計—共享記憶體之mmapLinux程式設計記憶體
- CUDA記憶體介紹記憶體
- 記憶體回收介紹記憶體
- 【讀書筆記】Android的Ashmem機制學習筆記Android
- 作業系統——記憶體管理學習筆記作業系統記憶體筆記
- jvm堆記憶體和GC簡介JVM記憶體GC
- JVM中記憶體和GC的介紹JVM記憶體GC
- 記憶體檔案系統的再學習記憶體
- Unix高階程式設計學習筆記--系統呼叫簡介程式設計筆記
- 記憶體管理簡介記憶體
- 學習筆記二--Weex語法介紹筆記
- linux作業系統修改共享記憶體的簡單方法(轉)Linux作業系統記憶體
- Linux系統和核心初始化過程簡要介紹(轉)Linux
- [CareerCup] 8.9 An In-memory File System 記憶體檔案系統記憶體
- ucore作業系統學習筆記(二) ucore lab2實體記憶體管理分析作業系統筆記記憶體
- Linux系統程式設計之命名管道與共享記憶體Linux程式設計記憶體
- javascript匿名函式簡單介紹JavaScript函式
- Bootstrap速學教程之簡要介紹boot
- ros學習檔案系統介紹ROS
- Sieve—Android 記憶體分析系統Android記憶體
- MQTT簡要介紹MQQT
- STM32記憶體結構介紹和FreeRTOS記憶體分配技巧記憶體