public class Ashmem extends Object
The Ashmem is created by a process. You can map the created Ashmem object to the current process to read and write data. Alternatively, you can transfer and map this Ashmem object to another process for data read and write. You need to ensure the timing sequence for concurrent read and write operations among multiple processes.
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
PROT_EXEC |
Indicates that data in the Ashmem object can be executed.
|
static int |
PROT_NONE |
Indicates that data in the Ashmem object cannot be read, written, or executed.
|
static int |
PROT_READ |
Indicates that data in the Ashmem object can be read.
|
static int |
PROT_WRITE |
Indicates that data in the Ashmem object can be written.
|
| 构造器 | 说明 |
|---|---|
Ashmem(long nativeObject) |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
closeAshmem() |
Closes this Ashmem.
|
static Ashmem |
createAshmem(String name,
int size) |
Creates an Ashmem object based on the specified debugging name and memory size.
|
static Ashmem |
createAshmemFromExisting(long unwrappedNativeObject) |
Creates an Ashmem object by copying an existing Ashmem object.
|
protected void |
finalize() |
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
long |
getAshmemIdentity() |
Acquires the identity of Ashmem object.
|
int |
getAShmemSize() |
Obtains the memory size of the Ashmem object.
|
boolean |
mapAshmem(int mapType) |
Maps the Ashmem object onto this process using a specified mapping type.
|
boolean |
mapReadAndWriteAShmem() |
Creates a read/write mapping between the Ashmem object and this process.
|
boolean |
mapReadOnlyAShmem() |
Creates a read-only mapping between the Ashmem object and this process.
|
byte[] |
readFromAShmem(int size,
int offset) |
Reads data from the Ashmem object.
|
boolean |
setProtection(int protectionType) |
Sets the protection type for the Ashmem object.
|
void |
unmapAShmem() |
Unmaps the Ashmem object from this process.
|
boolean |
writeToAShmem(byte[] data,
int size,
int offset) |
Writes data into the Ashmem object.
|
public static final int PROT_EXEC
It can be used to set the protection or mapping type for the Ashmem object.
public static final int PROT_NONE
It can be used to set the protection or mapping type for the Ashmem object.
public static final int PROT_READ
It can be used to set the protection or mapping type for the Ashmem object.
public static final int PROT_WRITE
It can be used to set the protection or mapping type for the Ashmem object.
public static Ashmem createAshmem(String name, int size)
The debugging name is used only for querying information about this Ashmem when you check the memory mappings of the processes that have mapped onto this Ashmem.
name - Indicates the debugging name, which helps query the Ashmem information.size - Indicates the size of the Ashmem.null if the creation fails.public static Ashmem createAshmemFromExisting(long unwrappedNativeObject)
The two Ashmem objects point to the same shared memory area.
unwrappedNativeObject - Indicates the ID of the existing Ashmem object.
You can obtain the ID using the getAshmemIdentity() method.protected void finalize()
throws Throwable
Objectfinalize method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked
if and when the Java™ virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize method may take any action, including
making this object available again to other threads; the usual purpose
of finalize, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize method of class Object performs no
special action; it simply returns normally. Subclasses of
Object may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize method causes
the finalization of this object to be halted, but is otherwise
ignored.
finalize 在类中 ObjectThrowable - the Exception raised by this methodWeakReference,
PhantomReferencepublic void closeAshmem()
You must call this method to close the Ashmem object created via createAshmem(java.lang.String,int)
in a timely manner if you no longer need it, to prevent memory leaks.
public boolean mapAshmem(int mapType)
Before reading and writing data using the Ashmem, you must call this method to map the Ashmem object onto this process. The mapping size is the total memory size of the Ashmem object, and the mapping starts from the start position of the Ashmem. The mapping type must be included in the protection types set for this Ashmem object.
mapType - Indicates the mapping type to use. The value is a bitwise ORed combination of
PROT_READ (readable/unreadable), PROT_WRITE (writable/unwritable),
PROT_EXEC (executable/unexecutable), or PROT_NONE.true if the mapping is successful; returns false otherwise.public boolean mapReadAndWriteAShmem()
The mapping size is the total memory size of the Ashmem object, and the mapping starts from
the start position of the Ashmem. When calling this method, ensure that the protection types of
the Ashmem object include PROT_READ | PROT_WRITE.
true if the mapping is successful; returns false otherwise.public boolean mapReadOnlyAShmem()
The mapping size is the total memory size of the Ashmem object, and the mapping starts from
the start position of the Ashmem. When calling this method, ensure that the protection types of
the Ashmem object include PROT_READ.
true if the mapping is successful; returns false otherwise.public void unmapAShmem()
After you finish using the Ashmem that has been mapped onto your process via mapAshmem(int),
mapReadAndWriteAshmem, or mapReadOnlyAshmem, you must call this method to cancel the mapping to
prevent memory leaks. You can create a mapping again if required.
public boolean setProtection(int protectionType)
The protection type can only be added but not removed.
Once the protection type is set, it will take effect for all mappings to the Ashmem object, regardless of whether the mappings are created before or after the protection type is set.
protectionType - Indicates the protection type to set. The value is a bitwise ORed combination of
PROT_READ, PROT_WRITE, PROT_EXEC, or PROT_NONE.true if the setting is successful; returns false otherwise.public int getAShmemSize()
public boolean writeToAShmem(byte[] data,
int size,
int offset)
You need to control the timing sequence for multi-process operations.
Before calling this method, ensure that you have obtained the write permission for the Ashmem object and a write mapping to this Ashmem object has been created for your process.
data - Indicates the data to be written into the Ashmem object.size - Indicates the size of the data to write.offset - Indicates the start position in the Ashmem object to write data.true if data is successfully written; returns false in other scenarios,
for example, data is written out of bounds or the write permission is not obtained.public byte[] readFromAShmem(int size,
int offset)
You need to control the timing sequence for multi-process operations.
Before calling this method, ensure that you have obtained the read permission for the Ashmem object and a read mapping to this Ashmem object has been created for your process.
size - Indicates the size of the data to read.offset - Indicates the start position in the Ashmem object to read data.public long getAshmemIdentity()