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)原理Android記憶體
- Linux共享記憶體(二)Linux記憶體
- Linux系統程式設計—共享記憶體之mmapLinux程式設計記憶體
- 計算機記憶體管理介紹計算機記憶體
- CUDA記憶體介紹記憶體
- 記憶體回收介紹記憶體
- JVM中記憶體和GC的介紹JVM記憶體GC
- jvm堆記憶體和GC簡介JVM記憶體GC
- Sieve—Android 記憶體分析系統Android記憶體
- Android Studio Profiler Memory (記憶體分析工具)的簡單使用及問題Android記憶體
- 作業系統——記憶體管理學習筆記作業系統記憶體筆記
- Linux系統程式設計之命名管道與共享記憶體Linux程式設計記憶體
- Android記憶體優化(四)解析Memory Monitor、Allocation Tracker和Heap DumpAndroid記憶體優化
- STM32記憶體結構介紹和FreeRTOS記憶體分配技巧記憶體
- 記憶體檔案系統的再學習記憶體
- Unix高階程式設計學習筆記--系統呼叫簡介程式設計筆記
- Memory記憶體傳值記憶體
- VisionPro學習筆記(1)——軟體介紹和基本使用筆記
- ucore作業系統學習筆記(二) ucore lab2實體記憶體管理分析作業系統筆記記憶體
- 【Android系統】Android系統架構簡介Android架構
- Java記憶體模型簡介Java記憶體模型
- Java記憶體模型 - 簡介Java記憶體模型
- Win10系統GPU共享記憶體怎麼關閉?Win10系統GPU共享記憶體的關閉方法Win10GPU記憶體
- ros學習檔案系統介紹ROS
- Android 輸入系統介紹Android
- Android系統介紹與框架Android框架
- Unity Memory Profiler 記憶體分析Unity記憶體
- Allowed memory size 記憶體不足記憶體
- 記憶體管理(Debug Memory Graph)記憶體
- 元學習簡單介紹
- 持久記憶體指令(PMDK)簡介記憶體
- Linux系統的發展歷史和學習前景介紹Linux
- 學習筆記-React的簡單介紹&工作原理筆記React
- Android 是如何管理 App 記憶體的 — Android 記憶體優化第二彈AndroidAPP記憶體優化
- [Linux]共享記憶體Linux記憶體
- POSIX 共享記憶體記憶體
- Golang 共享記憶體Golang記憶體
- 軟體測試學習教程—Jmeter元件介紹(二)JMeter元件
- 12. 記憶體管理(Memory Management)記憶體