Stay organized with collections
Save and categorize content based on your preferences.
Memory
Summary
Functions
ASharedMemory_create
Declared in android/sharedmem.h
int ASharedMemory_create(
const char *name,
size_t size
)
Create a shared memory region.
Create a shared memory region and returns a file descriptor. The resulting file descriptor can be mapped into the process' memory using mmap(2) with PROT_READ | PROT_WRITE | PROT_EXEC
. Access to shared memory regions can be restricted with ASharedMemory_setProt.
Use close(2) to release the shared memory region.
Use android.os.ParcelFileDescriptor to pass the file descriptor to another process. File descriptors may also be sent to other processes over a Unix domain socket with sendmsg(2) and SCM_RIGHTS
. See sendmsg(3) and cmsg(3) man pages for more information.
If you intend to share this file descriptor with a child process after calling exec(3), note that you will need to use fcntl(2) with F_SETFD
to clear the FD_CLOEXEC
flag for this to work on all versions of Android.
Available since API level 26.
Details |
Parameters |
name
|
an optional name.
|
size
|
size of the shared memory region
|
|
Returns
|
file descriptor that denotes the shared memory; -1 and sets errno on failure, or -EINVAL if the error is that size was 0.
|
ASharedMemory_dupFromJava
Declared in android/sharedmem_jni.h
int ASharedMemory_dupFromJava(
JNIEnv *env,
jobject sharedMemory
)
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.
Available since API level 27.
Details |
Parameters |
env
|
The JNIEnv* pointer
|
sharedMemory
|
The 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
Declared in android/sharedmem.h
size_t ASharedMemory_getSize(
int fd
)
Get the size of the shared memory region.
Available since API level 26.
Details |
Parameters |
fd
|
file descriptor of the shared memory region
|
|
Returns
|
size in bytes; 0 if fd is not a valid shared memory file descriptor.
|
ASharedMemory_setProt
Declared in android/sharedmem.h
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.
size_t memSize = ASharedMemory_getSize(fd);
char *buffer = (char *) mmap(NULL, memSize, 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.
Available since API level 26.
Details |
Parameters |
fd
|
file descriptor of the shared memory region.
|
prot
|
any 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, -1 and sets errno on failure.
|
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-11-19 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-19 UTC."],[],[],null,["# Memory\n======\n\nSummary\n-------\n\n| ### Functions ||\n|-----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|\n| [ASharedMemory_create](#group___memory_1ga81461a072ab1df987229c2d40b06272e)`(const char *name, size_t size)` | `int` Create a shared memory region. |\n| [ASharedMemory_dupFromJava](#group___memory_1ga30d57d8e8f6e145a0181278dffcbfece)`(JNIEnv *env, jobject sharedMemory)` | `int` Returns a dup'd FD from the given Java android.os.SharedMemory object. |\n| [ASharedMemory_getSize](#group___memory_1ga7aae7120315fa04a67404c1986e8fa35)`(int fd)` | `size_t` Get the size of the shared memory region. |\n| [ASharedMemory_setProt](#group___memory_1ga5bbfa70e48fcbc488a8cb8fa7657c878)`(int fd, int prot)` | `int` Restrict access of shared memory region. |\n\nFunctions\n---------\n\n### ASharedMemory_create\n\nDeclared in `android/sharedmem.h` \n\n```gdscript\nint ASharedMemory_create(\n const char *name,\n size_t size\n)\n``` \nCreate a shared memory region.\n\nCreate a shared memory region and returns a file descriptor. The resulting file descriptor can be mapped into the process' memory using mmap(2) with `PROT_READ | PROT_WRITE | PROT_EXEC`. Access to shared memory regions can be restricted with [ASharedMemory_setProt](/ndk/reference/group/memory#group___memory_1ga5bbfa70e48fcbc488a8cb8fa7657c878).\n\nUse close(2) to release the shared memory region.\n\nUse [android.os.ParcelFileDescriptor](/reference/android/os/ParcelFileDescriptor) to pass the file descriptor to another process. File descriptors may also be sent to other processes over a Unix domain socket with sendmsg(2) and `SCM_RIGHTS`. See sendmsg(3) and cmsg(3) man pages for more information.\n\nIf you intend to share this file descriptor with a child process after calling exec(3), note that you will need to use fcntl(2) with `F_SETFD` to clear the `FD_CLOEXEC` flag for this to work on all versions of Android.\n\nAvailable since API level 26.\n\n\u003cbr /\u003e\n\n| Details ||\n|-------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| Parameters | |--------|----------------------------------| | `name` | an optional name. | | `size` | size of the shared memory region | |\n| **Returns** | file descriptor that denotes the shared memory; -1 and sets `errno` on failure, or `-EINVAL` if the error is that size was 0. |\n\n### ASharedMemory_dupFromJava\n\nDeclared in `android/sharedmem_jni.h` \n\n```scdoc\nint ASharedMemory_dupFromJava(\n JNIEnv *env,\n jobject sharedMemory\n)\n``` \nReturns a dup'd FD from the given Java android.os.SharedMemory object.\n\nThe returned file descriptor has all the same properties \\& capabilities as the FD returned from [ASharedMemory_create()](/ndk/reference/group/memory#group___memory_1ga81461a072ab1df987229c2d40b06272e), however the protection flags will be the same as those of the android.os.SharedMemory object.\n\nUse close() to release the shared memory region.\n\nAvailable since API level 27.\n\n\u003cbr /\u003e\n\n| Details ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Parameters | |----------------|-----------------------------------------| | `env` | The JNIEnv\\* pointer | | `sharedMemory` | The Java android.os.SharedMemory object | |\n| **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) |\n\n### ASharedMemory_getSize\n\nDeclared in `android/sharedmem.h` \n\n```scdoc\nsize_t ASharedMemory_getSize(\n int fd\n)\n``` \nGet the size of the shared memory region.\n\nAvailable since API level 26.\n\n\u003cbr /\u003e\n\n| Details ||\n|-------------|---------------------------------------------------------------------------------------------------------------|\n| Parameters | |------|---------------------------------------------| | `fd` | file descriptor of the shared memory region | |\n| **Returns** | size in bytes; 0 if `fd` is not a valid shared memory file descriptor. |\n\n### ASharedMemory_setProt\n\nDeclared in `android/sharedmem.h` \n\n```scdoc\nint ASharedMemory_setProt(\n int fd,\n int prot\n)\n``` \nRestrict access of shared memory region.\n\nThis 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.\n\nIt 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).\n\n\n```scdoc\nint fd = ASharedMemory_create(\"memory\", 128);\n\n// By default it has PROT_READ | PROT_WRITE | PROT_EXEC.\nsize_t memSize = ASharedMemory_getSize(fd);\nchar *buffer = (char *) mmap(NULL, memSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);\n\nstrcpy(buffer, \"This is an example.\"); // trivially initialize content\n\n// limit access to read only\nASharedMemory_setProt(fd, PROT_READ);\n\n// share fd with another process here and the other process can only map with PROT_READ.\n```\n\n\u003cbr /\u003e\n\nAvailable since API level 26.\n\n\u003cbr /\u003e\n\n| Details ||\n|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Parameters | |--------|-------------------------------------------------------------------------------------------------------------------------------------------------------| | `fd` | file descriptor of the shared memory region. | | `prot` | any bitwise-or'ed combination of `PROT_READ`, `PROT_WRITE`, `PROT_EXEC` denoting updated access. Note access can only be removed, but not added back. | |\n| **Returns** | 0 for success, -1 and sets `errno` on failure. |"]]