xref: /aosp_15_r20/external/libchrome/mojo/public/c/system/buffer.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker // This file contains types/constants and functions specific to shared buffers.
6*635a8641SAndroid Build Coastguard Worker //
7*635a8641SAndroid Build Coastguard Worker // Note: This header should be compilable as C.
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #ifndef MOJO_PUBLIC_C_SYSTEM_BUFFER_H_
10*635a8641SAndroid Build Coastguard Worker #define MOJO_PUBLIC_C_SYSTEM_BUFFER_H_
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include "mojo/public/c/system/macros.h"
15*635a8641SAndroid Build Coastguard Worker #include "mojo/public/c/system/system_export.h"
16*635a8641SAndroid Build Coastguard Worker #include "mojo/public/c/system/types.h"
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker // Flags passed to |MojoCreateSharedBuffer()| via
19*635a8641SAndroid Build Coastguard Worker // |MojoCreateSharedBufferOptions|. See values defined below.
20*635a8641SAndroid Build Coastguard Worker typedef uint32_t MojoCreateSharedBufferFlags;
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker // No flags. Default behavior.
23*635a8641SAndroid Build Coastguard Worker #define MOJO_CREATE_SHARED_BUFFER_FLAG_NONE ((uint32_t)0)
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker // Options passed to |MojoCreateSharedBuffer()|.
26*635a8641SAndroid Build Coastguard Worker struct MOJO_ALIGNAS(8) MojoCreateSharedBufferOptions {
27*635a8641SAndroid Build Coastguard Worker   // The size of this structure, used for versioning.
28*635a8641SAndroid Build Coastguard Worker   uint32_t struct_size;
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker   // See |MojoCreateSharedBufferFlags|.
31*635a8641SAndroid Build Coastguard Worker   MojoCreateSharedBufferFlags flags;
32*635a8641SAndroid Build Coastguard Worker };
33*635a8641SAndroid Build Coastguard Worker MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) <= 8, "int64_t has weird alignment");
34*635a8641SAndroid Build Coastguard Worker MOJO_STATIC_ASSERT(sizeof(MojoCreateSharedBufferOptions) == 8,
35*635a8641SAndroid Build Coastguard Worker                    "MojoCreateSharedBufferOptions has wrong size");
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker // Flags passed to |MojoGetBufferInfo()| via |MojoGetBufferInfoOptions|. See
38*635a8641SAndroid Build Coastguard Worker // values defined below.
39*635a8641SAndroid Build Coastguard Worker typedef uint32_t MojoGetBufferInfoFlags;
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker // No flags. Default behavior.
42*635a8641SAndroid Build Coastguard Worker #define MOJO_GET_BUFFER_INFO_FLAG_NONE ((uint32_t)0)
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker // Options passed to |MojoGetBufferInfo()|.
45*635a8641SAndroid Build Coastguard Worker struct MOJO_ALIGNAS(8) MojoGetBufferInfoOptions {
46*635a8641SAndroid Build Coastguard Worker   // The size of this structure, used for versioning.
47*635a8641SAndroid Build Coastguard Worker   uint32_t struct_size;
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker   // See |MojoGetBufferInfoFlags|.
50*635a8641SAndroid Build Coastguard Worker   MojoGetBufferInfoFlags flags;
51*635a8641SAndroid Build Coastguard Worker };
52*635a8641SAndroid Build Coastguard Worker MOJO_STATIC_ASSERT(sizeof(MojoGetBufferInfoOptions) == 8,
53*635a8641SAndroid Build Coastguard Worker                    "MojoSharedBufferOptions has wrong size");
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker // Structure used to receive information about a shared buffer via
56*635a8641SAndroid Build Coastguard Worker // |MojoGetBufferInfo()|.
57*635a8641SAndroid Build Coastguard Worker struct MOJO_ALIGNAS(8) MojoSharedBufferInfo {
58*635a8641SAndroid Build Coastguard Worker   // The size of this structure, used for versioning.
59*635a8641SAndroid Build Coastguard Worker   uint32_t struct_size;
60*635a8641SAndroid Build Coastguard Worker 
61*635a8641SAndroid Build Coastguard Worker   // The size of the shared buffer.
62*635a8641SAndroid Build Coastguard Worker   uint64_t size;
63*635a8641SAndroid Build Coastguard Worker };
64*635a8641SAndroid Build Coastguard Worker MOJO_STATIC_ASSERT(sizeof(MojoSharedBufferInfo) == 16,
65*635a8641SAndroid Build Coastguard Worker                    "MojoSharedBufferInfo has wrong size");
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker // Flags passed to |MojoDuplicateBufferHandle()| via
68*635a8641SAndroid Build Coastguard Worker // |MojoDuplicateBufferHandleOptions|. See values defined below.
69*635a8641SAndroid Build Coastguard Worker typedef uint32_t MojoDuplicateBufferHandleFlags;
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker // No options. Default behavior. Note that if a shared buffer handle is ever
72*635a8641SAndroid Build Coastguard Worker // duplicated without |MOJO_DUPLICATE_BUFFER_HANDLE_READ_ONLY| (see below),
73*635a8641SAndroid Build Coastguard Worker // neither it nor any of its duplicates can ever be duplicated *with*
74*635a8641SAndroid Build Coastguard Worker // |MOJO_DUPLICATE_BUFFER_HANDLE_READ_ONLY| in the future. That is, once a
75*635a8641SAndroid Build Coastguard Worker // writable handle has been duplicated as another writable handle, it is no
76*635a8641SAndroid Build Coastguard Worker // longer possible to create read-only handles to the underlying buffer object.
77*635a8641SAndroid Build Coastguard Worker #define MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_NONE ((uint32_t)0)
78*635a8641SAndroid Build Coastguard Worker 
79*635a8641SAndroid Build Coastguard Worker // Duplicates the handle as read-only. If successful, the resulting new handle
80*635a8641SAndroid Build Coastguard Worker // will always map to a read-only memory region. Successful use of this flag
81*635a8641SAndroid Build Coastguard Worker // also imposes the limitation that the handle or any of its subsequent
82*635a8641SAndroid Build Coastguard Worker // duplicates may never be duplicated *without* this flag in the future. That
83*635a8641SAndroid Build Coastguard Worker // is, once a read-only handle is produced for a buffer object, all future
84*635a8641SAndroid Build Coastguard Worker // handles to that object must also be read-only.
85*635a8641SAndroid Build Coastguard Worker #define MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_READ_ONLY ((uint32_t)1 << 0)
86*635a8641SAndroid Build Coastguard Worker 
87*635a8641SAndroid Build Coastguard Worker // Options passed to |MojoDuplicateBufferHandle()|.
88*635a8641SAndroid Build Coastguard Worker struct MojoDuplicateBufferHandleOptions {
89*635a8641SAndroid Build Coastguard Worker   // The size of this structure, used for versioning.
90*635a8641SAndroid Build Coastguard Worker   uint32_t struct_size;
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker   // See |MojoDuplicateBufferHandleFlags|.
93*635a8641SAndroid Build Coastguard Worker   MojoDuplicateBufferHandleFlags flags;
94*635a8641SAndroid Build Coastguard Worker };
95*635a8641SAndroid Build Coastguard Worker MOJO_STATIC_ASSERT(sizeof(MojoDuplicateBufferHandleOptions) == 8,
96*635a8641SAndroid Build Coastguard Worker                    "MojoDuplicateBufferHandleOptions has wrong size");
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker // Flags passed to |MojoMapBuffer()| via |MojoMapBufferOptions|. See values
99*635a8641SAndroid Build Coastguard Worker // defined below.
100*635a8641SAndroid Build Coastguard Worker typedef uint32_t MojoMapBufferFlags;
101*635a8641SAndroid Build Coastguard Worker 
102*635a8641SAndroid Build Coastguard Worker // No flags. Default behavior.
103*635a8641SAndroid Build Coastguard Worker #define MOJO_MAP_BUFFER_FLAG_NONE ((uint32_t)0)
104*635a8641SAndroid Build Coastguard Worker 
105*635a8641SAndroid Build Coastguard Worker // Options passed to |MojoMapBuffer()|.
106*635a8641SAndroid Build Coastguard Worker struct MojoMapBufferOptions {
107*635a8641SAndroid Build Coastguard Worker   // The size of this structure, used for versioning.
108*635a8641SAndroid Build Coastguard Worker   uint32_t struct_size;
109*635a8641SAndroid Build Coastguard Worker 
110*635a8641SAndroid Build Coastguard Worker   // See |MojoMapBufferFlags|.
111*635a8641SAndroid Build Coastguard Worker   MojoMapBufferFlags flags;
112*635a8641SAndroid Build Coastguard Worker };
113*635a8641SAndroid Build Coastguard Worker MOJO_STATIC_ASSERT(sizeof(MojoMapBufferOptions) == 8,
114*635a8641SAndroid Build Coastguard Worker                    "MojoMapBufferOptions has wrong size");
115*635a8641SAndroid Build Coastguard Worker 
116*635a8641SAndroid Build Coastguard Worker #ifdef __cplusplus
117*635a8641SAndroid Build Coastguard Worker extern "C" {
118*635a8641SAndroid Build Coastguard Worker #endif
119*635a8641SAndroid Build Coastguard Worker 
120*635a8641SAndroid Build Coastguard Worker // Creates a buffer of size |num_bytes| bytes that can be shared between
121*635a8641SAndroid Build Coastguard Worker // processes. The returned handle may be duplicated any number of times by
122*635a8641SAndroid Build Coastguard Worker // |MojoDuplicateBufferHandle()|.
123*635a8641SAndroid Build Coastguard Worker //
124*635a8641SAndroid Build Coastguard Worker // To access the buffer's storage, one must call |MojoMapBuffer()|.
125*635a8641SAndroid Build Coastguard Worker //
126*635a8641SAndroid Build Coastguard Worker // |options| may be set to null for a shared buffer with the default options.
127*635a8641SAndroid Build Coastguard Worker //
128*635a8641SAndroid Build Coastguard Worker // On success, |*shared_buffer_handle| will be set to the handle for the shared
129*635a8641SAndroid Build Coastguard Worker // buffer. On failure it is not modified.
130*635a8641SAndroid Build Coastguard Worker //
131*635a8641SAndroid Build Coastguard Worker // Returns:
132*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_OK| on success.
133*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
134*635a8641SAndroid Build Coastguard Worker //       |*options| is invalid).
135*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has
136*635a8641SAndroid Build Coastguard Worker //       been reached (e.g., if the requested size was too large, or if the
137*635a8641SAndroid Build Coastguard Worker //       maximum number of handles was exceeded).
138*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|.
139*635a8641SAndroid Build Coastguard Worker MOJO_SYSTEM_EXPORT MojoResult
140*635a8641SAndroid Build Coastguard Worker MojoCreateSharedBuffer(uint64_t num_bytes,
141*635a8641SAndroid Build Coastguard Worker                        const struct MojoCreateSharedBufferOptions* options,
142*635a8641SAndroid Build Coastguard Worker                        MojoHandle* shared_buffer_handle);
143*635a8641SAndroid Build Coastguard Worker 
144*635a8641SAndroid Build Coastguard Worker // Duplicates the handle |buffer_handle| as a new shared buffer handle. On
145*635a8641SAndroid Build Coastguard Worker // success this returns the new handle in |*new_buffer_handle|. A shared buffer
146*635a8641SAndroid Build Coastguard Worker // remains allocated as long as there is at least one shared buffer handle
147*635a8641SAndroid Build Coastguard Worker // referencing it in at least one process in the system.
148*635a8641SAndroid Build Coastguard Worker //
149*635a8641SAndroid Build Coastguard Worker // |options| may be set to null to duplicate the buffer handle with the default
150*635a8641SAndroid Build Coastguard Worker // options.
151*635a8641SAndroid Build Coastguard Worker //
152*635a8641SAndroid Build Coastguard Worker // Access rights to mapped memory from the duplicated handle may be controlled
153*635a8641SAndroid Build Coastguard Worker // by flags in |*options|, with some limitations. See notes on
154*635a8641SAndroid Build Coastguard Worker // |MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_NONE| and
155*635a8641SAndroid Build Coastguard Worker // |MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_READ_ONLY| regarding restrictions on
156*635a8641SAndroid Build Coastguard Worker // duplication with respect to these flags.
157*635a8641SAndroid Build Coastguard Worker //
158*635a8641SAndroid Build Coastguard Worker // Returns:
159*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_OK| on success.
160*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
161*635a8641SAndroid Build Coastguard Worker //       |buffer_handle| is not a valid buffer handle or |*options| is invalid).
162*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|.
163*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_FAILED_PRECONDITION| if
164*635a8641SAndroid Build Coastguard Worker //       |MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_READ_ONLY| was set but the handle
165*635a8641SAndroid Build Coastguard Worker //       was already previously duplicated without that flag; or if
166*635a8641SAndroid Build Coastguard Worker //       |MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_READ_ONLY| was not set but the
167*635a8641SAndroid Build Coastguard Worker //       handle was already previously duplicated with that flag.
168*635a8641SAndroid Build Coastguard Worker MOJO_SYSTEM_EXPORT MojoResult MojoDuplicateBufferHandle(
169*635a8641SAndroid Build Coastguard Worker     MojoHandle buffer_handle,
170*635a8641SAndroid Build Coastguard Worker     const struct MojoDuplicateBufferHandleOptions* options,
171*635a8641SAndroid Build Coastguard Worker     MojoHandle* new_buffer_handle);
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker // Maps the part (at offset |offset| of length |num_bytes|) of the buffer given
174*635a8641SAndroid Build Coastguard Worker // by |buffer_handle| into memory, with options specified by |options|.
175*635a8641SAndroid Build Coastguard Worker // |offset+num_bytes| must be less than or equal to the size of the buffer. On
176*635a8641SAndroid Build Coastguard Worker // success, |*buffer| points to memory with the requested part of the buffer. On
177*635a8641SAndroid Build Coastguard Worker // failure |*buffer| it is not modified.
178*635a8641SAndroid Build Coastguard Worker //
179*635a8641SAndroid Build Coastguard Worker // A single buffer handle may have multiple active mappings. The permissions
180*635a8641SAndroid Build Coastguard Worker // (e.g., writable or executable) of the returned memory depend on the
181*635a8641SAndroid Build Coastguard Worker // properties of the buffer and properties attached to the buffer handle, as
182*635a8641SAndroid Build Coastguard Worker // well as |flags|.
183*635a8641SAndroid Build Coastguard Worker //
184*635a8641SAndroid Build Coastguard Worker // A mapped buffer must eventually be unmapped by calling |MojoUnmapBuffer()|
185*635a8641SAndroid Build Coastguard Worker // with the value of |*buffer| returned by this function.
186*635a8641SAndroid Build Coastguard Worker //
187*635a8641SAndroid Build Coastguard Worker // |options| may be null to map the buffer with default behavior.
188*635a8641SAndroid Build Coastguard Worker //
189*635a8641SAndroid Build Coastguard Worker // Returns:
190*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_OK| on success.
191*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
192*635a8641SAndroid Build Coastguard Worker //       |buffer_handle| is not a valid buffer handle, the range specified by
193*635a8641SAndroid Build Coastguard Worker //       |offset| and |num_bytes| is not valid, or |*options| is invalid).
194*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_RESOURCE_EXHAUSTED| if the mapping operation itself failed
195*635a8641SAndroid Build Coastguard Worker //       (e.g., due to not having appropriate address space available).
196*635a8641SAndroid Build Coastguard Worker MOJO_SYSTEM_EXPORT MojoResult
197*635a8641SAndroid Build Coastguard Worker MojoMapBuffer(MojoHandle buffer_handle,
198*635a8641SAndroid Build Coastguard Worker               uint64_t offset,
199*635a8641SAndroid Build Coastguard Worker               uint64_t num_bytes,
200*635a8641SAndroid Build Coastguard Worker               const struct MojoMapBufferOptions* options,
201*635a8641SAndroid Build Coastguard Worker               void** buffer);
202*635a8641SAndroid Build Coastguard Worker 
203*635a8641SAndroid Build Coastguard Worker // Unmaps a buffer pointer that was mapped by |MojoMapBuffer()|. |buffer| must
204*635a8641SAndroid Build Coastguard Worker // have been the result of |MojoMapBuffer()| (not some other pointer inside
205*635a8641SAndroid Build Coastguard Worker // the mapped memory), and the entire mapping will be removed.
206*635a8641SAndroid Build Coastguard Worker //
207*635a8641SAndroid Build Coastguard Worker // A mapping may only be unmapped once.
208*635a8641SAndroid Build Coastguard Worker //
209*635a8641SAndroid Build Coastguard Worker // Returns:
210*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_OK| on success.
211*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_INVALID_ARGUMENT| if |buffer| is invalid (e.g., is not the
212*635a8641SAndroid Build Coastguard Worker //       result of |MojoMapBuffer()| or has already been unmapped).
213*635a8641SAndroid Build Coastguard Worker MOJO_SYSTEM_EXPORT MojoResult MojoUnmapBuffer(void* buffer);
214*635a8641SAndroid Build Coastguard Worker 
215*635a8641SAndroid Build Coastguard Worker // Retrieve information about |buffer_handle| into |info|.
216*635a8641SAndroid Build Coastguard Worker //
217*635a8641SAndroid Build Coastguard Worker // Callers must initialize |info->struct_size| to |sizeof(MojoSharedBufferInfo)|
218*635a8641SAndroid Build Coastguard Worker // before calling this function.
219*635a8641SAndroid Build Coastguard Worker //
220*635a8641SAndroid Build Coastguard Worker // |options| may be null for default options.
221*635a8641SAndroid Build Coastguard Worker //
222*635a8641SAndroid Build Coastguard Worker // Returns:
223*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_OK| on success.
224*635a8641SAndroid Build Coastguard Worker //   |MOJO_RESULT_INVALID_ARGUMENT| if |buffer_handle| is invalid, |info| is
225*635a8641SAndroid Build Coastguard Worker //       null, or |*options| is invalid.
226*635a8641SAndroid Build Coastguard Worker //
227*635a8641SAndroid Build Coastguard Worker // On success, |info->size| will be set to the size of the buffer. On failure it
228*635a8641SAndroid Build Coastguard Worker // is not modified.
229*635a8641SAndroid Build Coastguard Worker MOJO_SYSTEM_EXPORT MojoResult
230*635a8641SAndroid Build Coastguard Worker MojoGetBufferInfo(MojoHandle buffer_handle,
231*635a8641SAndroid Build Coastguard Worker                   const struct MojoGetBufferInfoOptions* options,
232*635a8641SAndroid Build Coastguard Worker                   struct MojoSharedBufferInfo* info);
233*635a8641SAndroid Build Coastguard Worker 
234*635a8641SAndroid Build Coastguard Worker #ifdef __cplusplus
235*635a8641SAndroid Build Coastguard Worker }  // extern "C"
236*635a8641SAndroid Build Coastguard Worker #endif
237*635a8641SAndroid Build Coastguard Worker 
238*635a8641SAndroid Build Coastguard Worker #endif  // MOJO_PUBLIC_C_SYSTEM_BUFFER_H_
239