1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * @addtogroup Memory 19 * @{ 20 */ 21 22 /** 23 * @file sharedmem.h 24 * @brief Shared memory buffers that can be shared across process. 25 */ 26 27 #ifndef ANDROID_SHARED_MEMORY_H 28 #define ANDROID_SHARED_MEMORY_H 29 30 #include <stddef.h> 31 #include <sys/cdefs.h> 32 33 /****************************************************************** 34 * 35 * IMPORTANT NOTICE: 36 * 37 * This file is part of Android's set of stable system headers 38 * exposed by the Android NDK (Native Development Kit). 39 * 40 * Third-party source AND binary code relies on the definitions 41 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. 42 * 43 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) 44 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS 45 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY 46 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES 47 */ 48 49 #ifdef __cplusplus 50 extern "C" { 51 #endif 52 53 #if __ANDROID_API__ >= 26 54 55 /** 56 * Create a shared memory region. 57 * 58 * Create shared memory region and returns an file descriptor. The resulting file descriptor can be 59 * mmap'ed to process memory space with PROT_READ | PROT_WRITE | PROT_EXEC. Access to shared memory 60 * region can be restricted with {@link ASharedMemory_setProt}. 61 * 62 * Use close() to release the shared memory region. 63 * 64 * Available since API level 26. 65 * 66 * \param name an optional name. 67 * \param size size of the shared memory region 68 * \return file descriptor that denotes the shared memory; error code on failure. 69 */ 70 int ASharedMemory_create(const char *name, size_t size) __INTRODUCED_IN(26); 71 72 /** 73 * Get the size of the shared memory region. 74 * 75 * Available since API level 26. 76 * 77 * \param fd file descriptor of the shared memory region 78 * \return size in bytes; 0 if fd is not a valid shared memory file descriptor. 79 */ 80 size_t ASharedMemory_getSize(int fd) __INTRODUCED_IN(26); 81 82 /** 83 * Restrict access of shared memory region. 84 * 85 * This function restricts access of a shared memory region. Access can only be removed. The effect 86 * applies globally to all file descriptors in all processes across the system that refer to this 87 * shared memory region. Existing memory mapped regions are not affected. 88 * 89 * It is a common use case to create a shared memory region, map it read/write locally to intialize 90 * content, and then send the shared memory to another process with read only access. Code example 91 * as below (error handling omited). 92 * 93 * 94 * int fd = ASharedMemory_create("memory", 128); 95 * 96 * // By default it has PROT_READ | PROT_WRITE | PROT_EXEC. 97 * size_t memSize = ASharedMemory_getSize(fd); 98 * char *buffer = (char *) mmap(NULL, memSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 99 * 100 * strcpy(buffer, "This is an example."); // trivially initialize content 101 * 102 * // limit access to read only 103 * ASharedMemory_setProt(fd, PROT_READ); 104 * 105 * // share fd with another process here and the other process can only map with PROT_READ. 106 * 107 * Available since API level 26. 108 * 109 * \param fd file descriptor of the shared memory region. 110 * \param prot any bitwise-or'ed combination of PROT_READ, PROT_WRITE, PROT_EXEC denoting 111 * updated access. Note access can only be removed, but not added back. 112 * \return 0 for success, error code on failure. 113 */ 114 int ASharedMemory_setProt(int fd, int prot) __INTRODUCED_IN(26); 115 116 #endif // __ANDROID_API__ >= 26 117 118 #ifdef __cplusplus 119 }; 120 #endif 121 122 #endif // ANDROID_SHARED_MEMORY_H 123 124 /** @} */ 125