blob: 0354f17f481a86839a05aac16cb1beb237856f52 [file] [log] [blame]
Peng Xue1e7e382017-02-24 01:30:16 -08001/*
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 */
25
26#ifndef ANDROID_SHARED_MEMORY_H
27#define ANDROID_SHARED_MEMORY_H
28
29#include <stddef.h>
30
31/******************************************************************
32 *
33 * IMPORTANT NOTICE:
34 *
35 * This file is part of Android's set of stable system headers
36 * exposed by the Android NDK (Native Development Kit).
37 *
38 * Third-party source AND binary code relies on the definitions
39 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
40 *
41 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
42 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
43 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
44 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
45 */
46
47/**
48 * Structures and functions for a shared memory buffer that can be shared across process.
49 */
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
Peng Xue1e7e382017-02-24 01:30:16 -080055/**
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 * \param name an optional name.
65 * \param size size of the shared memory region
66 * \return file descriptor that denotes the shared memory; error code on failure.
67 */
Elliott Hughesc2fcafc2018-06-18 12:28:46 -070068int ASharedMemory_create(const char *name, size_t size) __INTRODUCED_IN(26);
Peng Xue1e7e382017-02-24 01:30:16 -080069
70/**
71 * Get the size of the shared memory region.
72 *
73 * \param fd file descriptor of the shared memory region
74 * \return size in bytes; 0 if fd is not a valid shared memory file descriptor.
75 */
Elliott Hughesc2fcafc2018-06-18 12:28:46 -070076size_t ASharedMemory_getSize(int fd) __INTRODUCED_IN(26);
Peng Xue1e7e382017-02-24 01:30:16 -080077
78/**
79 * Restrict access of shared memory region.
80 *
81 * This function restricts access of a shared memory region. Access can only be removed. The effect
82 * applies globally to all file descriptors in all processes across the system that refer to this
83 * shared memory region. Existing memory mapped regions are not affected.
84 *
85 * It is a common use case to create a shared memory region, map it read/write locally to intialize
86 * content, and then send the shared memory to another process with read only access. Code example
Quddus Chong51633572017-05-09 10:39:23 -070087 * as below (error handling omited).
Peng Xue1e7e382017-02-24 01:30:16 -080088 *
Peng Xue1e7e382017-02-24 01:30:16 -080089 *
Quddus Chong51633572017-05-09 10:39:23 -070090 * int fd = ASharedMemory_create("memory", 128);
Peng Xue1e7e382017-02-24 01:30:16 -080091 *
Quddus Chong51633572017-05-09 10:39:23 -070092 * // By default it has PROT_READ | PROT_WRITE | PROT_EXEC.
93 * char *buffer = (char *) mmap(NULL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Peng Xue1e7e382017-02-24 01:30:16 -080094 *
Quddus Chong51633572017-05-09 10:39:23 -070095 * strcpy(buffer, "This is an example."); // trivially initialize content
Peng Xue1e7e382017-02-24 01:30:16 -080096 *
Quddus Chong51633572017-05-09 10:39:23 -070097 * // limit access to read only
98 * ASharedMemory_setProt(fd, PROT_READ);
99 *
100 * // share fd with another process here and the other process can only map with PROT_READ.
Peng Xue1e7e382017-02-24 01:30:16 -0800101 *
102 * \param fd file descriptor of the shared memory region.
103 * \param prot any bitwise-or'ed combination of PROT_READ, PROT_WRITE, PROT_EXEC denoting
104 * updated access. Note access can only be removed, but not added back.
105 * \return 0 for success, error code on failure.
106 */
Elliott Hughesc2fcafc2018-06-18 12:28:46 -0700107int ASharedMemory_setProt(int fd, int prot) __INTRODUCED_IN(26);
Peng Xue1e7e382017-02-24 01:30:16 -0800108
109#ifdef __cplusplus
110};
111#endif
112
113#endif // ANDROID_SHARED_MEMORY_H
114
115/** @} */