Skip to content

Most visited

Recently visited

navigation

Memory

Memory

Files

file  sharedmem.h
 
file  sharedmem_jni.h
 

Functions

int ASharedMemory_create (const char *name, size_t size)
 
size_t ASharedMemory_getSize (int fd)
 
int ASharedMemory_setProt (int fd, int prot)
 
int ASharedMemory_dupFromJava (JNIEnv *env, jobject sharedMemory)
 

Detailed Description

Function Documentation

◆ ASharedMemory_create()

int ASharedMemory_create ( const char *  name,
size_t  size 
)

Structures and functions for a shared memory buffer that can be shared across process. Create a shared memory region.

Create shared memory region and returns an file descriptor. The resulting file descriptor can be mmap'ed to process memory space with PROT_READ | PROT_WRITE | PROT_EXEC. Access to shared memory region can be restricted with ASharedMemory_setProt.

Use close() to release the shared memory region.

Parameters
namean optional name.
sizesize of the shared memory region
Returns
file descriptor that denotes the shared memory; error code on failure.

◆ ASharedMemory_dupFromJava()

int ASharedMemory_dupFromJava ( JNIEnv *  env,
jobject  sharedMemory 
)

Structures and functions for a shared memory buffer that can be shared across process. Returns a dup'd FD from the given Java android.os.SharedMemory object. The returned file descriptor has all the same properties & capabilities as the FD returned from ASharedMemory_create(), however the protection flags will be the same as those of the android.os.SharedMemory object.

Use close() to release the shared memory region.

Parameters
envThe JNIEnv* pointer
sharedMemoryThe Java android.os.SharedMemory object
Returns
file descriptor that denotes the shared memory; -1 if the shared memory object is already closed, if the JNIEnv or jobject is NULL, or if there are too many open file descriptors (errno=EMFILE)

◆ ASharedMemory_getSize()

size_t ASharedMemory_getSize ( int  fd)

Get the size of the shared memory region.

Parameters
fdfile descriptor of the shared memory region
Returns
size in bytes; 0 if fd is not a valid shared memory file descriptor.

◆ ASharedMemory_setProt()

int ASharedMemory_setProt ( int  fd,
int  prot 
)

Restrict access of shared memory region.

This function restricts access of a shared memory region. Access can only be removed. The effect applies globally to all file descriptors in all processes across the system that refer to this shared memory region. Existing memory mapped regions are not affected.

It is a common use case to create a shared memory region, map it read/write locally to intialize content, and then send the shared memory to another process with read only access. Code example as below (error handling omited).

int fd = ASharedMemory_create("memory", 128);

// By default it has PROT_READ | PROT_WRITE | PROT_EXEC.
char *buffer = (char *) mmap(NULL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

strcpy(buffer, "This is an example."); // trivially initialize content

// limit access to read only
ASharedMemory_setProt(fd, PROT_READ);

// share fd with another process here and the other process can only map with PROT_READ.
Parameters
fdfile descriptor of the shared memory region.
protany bitwise-or'ed combination of PROT_READ, PROT_WRITE, PROT_EXEC denoting updated access. Note access can only be removed, but not added back.
Returns
0 for success, error code on failure.
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)