| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 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 | 
| Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 24 |  * @brief Shared memory buffers that can be shared across process. | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 25 |  */ | 
 | 26 |  | 
 | 27 | #ifndef ANDROID_SHARED_MEMORY_H | 
 | 28 | #define ANDROID_SHARED_MEMORY_H | 
 | 29 |  | 
 | 30 | #include <stddef.h> | 
 | 31 |  | 
 | 32 | /****************************************************************** | 
 | 33 |  * | 
 | 34 |  * IMPORTANT NOTICE: | 
 | 35 |  * | 
 | 36 |  *   This file is part of Android's set of stable system headers | 
 | 37 |  *   exposed by the Android NDK (Native Development Kit). | 
 | 38 |  * | 
 | 39 |  *   Third-party source AND binary code relies on the definitions | 
 | 40 |  *   here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. | 
 | 41 |  * | 
 | 42 |  *   - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) | 
 | 43 |  *   - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS | 
 | 44 |  *   - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY | 
 | 45 |  *   - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES | 
 | 46 |  */ | 
 | 47 |  | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 48 | #ifdef __cplusplus | 
 | 49 | extern "C" { | 
 | 50 | #endif | 
 | 51 |  | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 52 | /** | 
 | 53 |  * Create a shared memory region. | 
 | 54 |  * | 
 | 55 |  * Create shared memory region and returns an file descriptor.  The resulting file descriptor can be | 
 | 56 |  * mmap'ed to process memory space with PROT_READ | PROT_WRITE | PROT_EXEC. Access to shared memory | 
 | 57 |  * region can be restricted with {@link ASharedMemory_setProt}. | 
 | 58 |  * | 
 | 59 |  * Use close() to release the shared memory region. | 
 | 60 |  * | 
| Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 61 |  * Available since API level 26. | 
 | 62 |  * | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 63 |  * \param name an optional name. | 
 | 64 |  * \param size size of the shared memory region | 
 | 65 |  * \return file descriptor that denotes the shared memory; error code on failure. | 
 | 66 |  */ | 
| Elliott Hughes | c2fcafc | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 67 | int ASharedMemory_create(const char *name, size_t size) __INTRODUCED_IN(26); | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 68 |  | 
 | 69 | /** | 
 | 70 |  * Get the size of the shared memory region. | 
 | 71 |  * | 
| Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 72 |  * Available since API level 26. | 
 | 73 |  * | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 74 |  * \param fd file descriptor of the shared memory region | 
 | 75 |  * \return size in bytes; 0 if fd is not a valid shared memory file descriptor. | 
 | 76 |  */ | 
| Elliott Hughes | c2fcafc | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 77 | size_t ASharedMemory_getSize(int fd) __INTRODUCED_IN(26); | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 78 |  | 
 | 79 | /** | 
 | 80 |  * Restrict access of shared memory region. | 
 | 81 |  * | 
 | 82 |  * This function restricts access of a shared memory region. Access can only be removed. The effect | 
 | 83 |  * applies globally to all file descriptors in all processes across the system that refer to this | 
 | 84 |  * shared memory region. Existing memory mapped regions are not affected. | 
 | 85 |  * | 
 | 86 |  * It is a common use case to create a shared memory region, map it read/write locally to intialize | 
 | 87 |  * content, and then send the shared memory to another process with read only access. Code example | 
| Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 88 |  * as below (error handling omited). | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 89 |  * | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 90 |  * | 
| Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 91 |  *     int fd = ASharedMemory_create("memory", 128); | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 92 |  * | 
| Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 93 |  *     // By default it has PROT_READ | PROT_WRITE | PROT_EXEC. | 
 | 94 |  *     char *buffer = (char *) mmap(NULL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 95 |  * | 
| Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 96 |  *     strcpy(buffer, "This is an example."); // trivially initialize content | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 97 |  * | 
| Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 98 |  *     // limit access to read only | 
 | 99 |  *     ASharedMemory_setProt(fd, PROT_READ); | 
 | 100 |  * | 
 | 101 |  *     // share fd with another process here and the other process can only map with PROT_READ. | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 102 |  * | 
| Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 103 |  * Available since API level 26. | 
 | 104 |  * | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 105 |  * \param fd   file descriptor of the shared memory region. | 
 | 106 |  * \param prot any bitwise-or'ed combination of PROT_READ, PROT_WRITE, PROT_EXEC denoting | 
 | 107 |  *             updated access. Note access can only be removed, but not added back. | 
 | 108 |  * \return 0 for success, error code on failure. | 
 | 109 |  */ | 
| Elliott Hughes | c2fcafc | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 110 | int ASharedMemory_setProt(int fd, int prot) __INTRODUCED_IN(26); | 
| Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 111 |  | 
 | 112 | #ifdef __cplusplus | 
 | 113 | }; | 
 | 114 | #endif | 
 | 115 |  | 
 | 116 | #endif // ANDROID_SHARED_MEMORY_H | 
 | 117 |  | 
 | 118 | /** @} */ |