1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains types/constants and functions specific to buffers (and in
6 // particular shared buffers).
7 // TODO(vtl): Reorganize this file (etc.) to separate general buffer functions
8 // from (shared) buffer creation.
10 // Note: This header should be compilable as C.
12 #ifndef MOJO_PUBLIC_C_SYSTEM_BUFFER_H_
13 #define MOJO_PUBLIC_C_SYSTEM_BUFFER_H_
15 #include "mojo/public/c/system/macros.h"
16 #include "mojo/public/c/system/system_export.h"
17 #include "mojo/public/c/system/types.h"
19 // |MojoCreateSharedBufferOptions|: Used to specify creation parameters for a
20 // shared buffer to |MojoCreateSharedBuffer()|.
21 // |uint32_t struct_size|: Set to the size of the
22 // |MojoCreateSharedBufferOptions| struct. (Used to allow for future
24 // |MojoCreateSharedBufferOptionsFlags flags|: Reserved for future use.
25 // |MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE|: No flags; default mode.
27 // TODO(vtl): Maybe add a flag to indicate whether the memory should be
29 // TODO(vtl): Also a flag for discardable (ashmem-style) buffers.
31 typedef uint32_t MojoCreateSharedBufferOptionsFlags
;
34 const MojoCreateSharedBufferOptionsFlags
35 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE
= 0;
37 #define MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE \
38 ((MojoCreateSharedBufferOptionsFlags)0)
41 MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) == 8, "int64_t has weird alignment");
42 struct MOJO_ALIGNAS(8) MojoCreateSharedBufferOptions
{
44 MojoCreateSharedBufferOptionsFlags flags
;
46 MOJO_STATIC_ASSERT(sizeof(MojoCreateSharedBufferOptions
) == 8,
47 "MojoCreateSharedBufferOptions has wrong size");
49 // |MojoDuplicateBufferHandleOptions|: Used to specify parameters in duplicating
50 // access to a shared buffer to |MojoDuplicateBufferHandle()|.
51 // |uint32_t struct_size|: Set to the size of the
52 // |MojoDuplicateBufferHandleOptions| struct. (Used to allow for future
54 // |MojoDuplicateBufferHandleOptionsFlags flags|: Reserved for future use.
55 // |MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE|: No flags; default
58 // TODO(vtl): Add flags to remove writability (and executability)? Also, COW?
60 typedef uint32_t MojoDuplicateBufferHandleOptionsFlags
;
63 const MojoDuplicateBufferHandleOptionsFlags
64 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE
= 0;
66 #define MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE \
67 ((MojoDuplicateBufferHandleOptionsFlags)0)
70 struct MojoDuplicateBufferHandleOptions
{
72 MojoDuplicateBufferHandleOptionsFlags flags
;
74 MOJO_STATIC_ASSERT(sizeof(MojoDuplicateBufferHandleOptions
) == 8,
75 "MojoDuplicateBufferHandleOptions has wrong size");
77 // |MojoMapBufferFlags|: Used to specify different modes to |MojoMapBuffer()|.
78 // |MOJO_MAP_BUFFER_FLAG_NONE| - No flags; default mode.
80 typedef uint32_t MojoMapBufferFlags
;
83 const MojoMapBufferFlags MOJO_MAP_BUFFER_FLAG_NONE
= 0;
85 #define MOJO_MAP_BUFFER_FLAG_NONE ((MojoMapBufferFlags)0)
92 // Note: See the comment in functions.h about the meaning of the "optional"
93 // label for pointer parameters.
95 // Creates a buffer of size |num_bytes| bytes that can be shared between
96 // applications (by duplicating the handle -- see |MojoDuplicateBufferHandle()|
97 // -- and passing it over a message pipe). To access the buffer, one must call
100 // |options| may be set to null for a shared buffer with the default options.
102 // On success, |*shared_buffer_handle| will be set to the handle for the shared
103 // buffer. (On failure, it is not modified.)
105 // Note: While more than |num_bytes| bytes may apparently be
106 // available/visible/readable/writable, trying to use those extra bytes is
107 // undefined behavior.
110 // |MOJO_RESULT_OK| on success.
111 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
112 // |*options| is invalid).
113 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has
114 // been reached (e.g., if the requested size was too large, or if the
115 // maximum number of handles was exceeded).
116 // |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|.
117 MOJO_SYSTEM_EXPORT MojoResult
MojoCreateSharedBuffer(
118 const struct MojoCreateSharedBufferOptions
* options
, // Optional.
119 uint64_t num_bytes
, // In.
120 MojoHandle
* shared_buffer_handle
); // Out.
122 // Duplicates the handle |buffer_handle| to a buffer. This creates another
123 // handle (returned in |*new_buffer_handle| on success), which can then be sent
124 // to another application over a message pipe, while retaining access to the
125 // |buffer_handle| (and any mappings that it may have).
127 // |options| may be set to null to duplicate the buffer handle with the default
130 // On success, |*shared_buffer_handle| will be set to the handle for the new
131 // buffer handle. (On failure, it is not modified.)
134 // |MOJO_RESULT_OK| on success.
135 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
136 // |buffer_handle| is not a valid buffer handle or |*options| is invalid).
137 // |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|.
138 MOJO_SYSTEM_EXPORT MojoResult
MojoDuplicateBufferHandle(
139 MojoHandle buffer_handle
,
140 const struct MojoDuplicateBufferHandleOptions
* options
, // Optional.
141 MojoHandle
* new_buffer_handle
); // Out.
143 // Maps the part (at offset |offset| of length |num_bytes|) of the buffer given
144 // by |buffer_handle| into memory, with options specified by |flags|. |offset +
145 // num_bytes| must be less than or equal to the size of the buffer. On success,
146 // |*buffer| points to memory with the requested part of the buffer. (On
147 // failure, it is not modified.)
149 // A single buffer handle may have multiple active mappings (possibly depending
150 // on the buffer type). The permissions (e.g., writable or executable) of the
151 // returned memory may depend on the properties of the buffer and properties
152 // attached to the buffer handle as well as |flags|.
154 // Note: Though data outside the specified range may apparently be
155 // available/visible/readable/writable, trying to use those extra bytes is
156 // undefined behavior.
159 // |MOJO_RESULT_OK| on success.
160 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
161 // |buffer_handle| is not a valid buffer handle or the range specified by
162 // |offset| and |num_bytes| is not valid).
163 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if the mapping operation itself failed
164 // (e.g., due to not having appropriate address space available).
165 MOJO_SYSTEM_EXPORT MojoResult
MojoMapBuffer(MojoHandle buffer_handle
,
168 void** buffer
, // Out.
169 MojoMapBufferFlags flags
);
171 // Unmaps a buffer pointer that was mapped by |MojoMapBuffer()|. |buffer| must
172 // have been the result of |MojoMapBuffer()| (not some pointer strictly inside
173 // the mapped memory), and the entire mapping will be removed (partial unmapping
174 // is not supported). A mapping may only be unmapped exactly once.
177 // |MOJO_RESULT_OK| on success.
178 // |MOJO_RESULT_INVALID_ARGUMENT| if |buffer| is invalid (e.g., is not the
179 // result of |MojoMapBuffer()| or has already been unmapped).
180 MOJO_SYSTEM_EXPORT MojoResult
MojoUnmapBuffer(void* buffer
); // In.
186 #endif // MOJO_PUBLIC_C_SYSTEM_BUFFER_H_