OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #ifndef MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_STRUCT_H_ | |
6 #define MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_STRUCT_H_ | |
7 | |
8 #include <mojo/bindings/buffer.h> | |
9 #include <mojo/bindings/internal/type_descriptor.h> | |
10 #include <mojo/bindings/internal/util.h> | |
11 #include <mojo/macros.h> | |
12 #include <stddef.h> | |
13 #include <stdint.h> | |
14 | |
15 MOJO_BEGIN_EXTERN_C | |
16 | |
17 struct MojomStructHeader { | |
18 // |num_bytes| includes the size of this struct header along with the | |
19 // accompanying struct data. |num_bytes| must be rounded up to 8 bytes. | |
20 uint32_t num_bytes; | |
21 uint32_t version; | |
22 }; | |
23 MOJO_STATIC_ASSERT(sizeof(struct MojomStructHeader) == 8, | |
24 "struct MojomStructHeader must be 8 bytes."); | |
25 | |
26 // Returns the number of bytes required to serialize |in_struct|. | |
27 // |in_type_desc| is the generated type descriptor that describes the locations | |
28 // of the pointers and handles in |in_struct|. | |
29 size_t MojomStruct_ComputeSerializedSize( | |
30 const struct MojomTypeDescriptorStruct* in_type_desc, | |
31 const struct MojomStructHeader* in_struct); | |
32 | |
33 // Encodes the mojom struct described by the |inout_struct| buffer; note that | |
34 // any references from the struct are also in the buffer backed by | |
35 // |inout_struct|, and they are recursively encoded. Encodes all pointers to | |
36 // relative offsets, and encodes all handles by moving them into | |
37 // |inout_handles_buffer| encoding the index into the handle. | |
38 // |in_type_desc|: Describes the pointer and handle fields of the mojom struct. | |
39 // |inout_struct|: Contains the struct, and any other references outside of the | |
40 // struct. | |
41 // |in_struct_size|: Size of the buffer backed by |inout_struct| in bytes. | |
42 // |inout_handles_buffer|: | |
43 // A buffer used to record handles during encoding. The |num_handles_used| | |
44 // field can be used to determine how many handles were moved into this | |
45 // buffer. | |
46 void MojomStruct_EncodePointersAndHandles( | |
47 const struct MojomTypeDescriptorStruct* in_type_desc, | |
48 struct MojomStructHeader* inout_struct, | |
49 uint32_t in_struct_size, | |
50 struct MojomHandleBuffer* inout_handles_buffer); | |
51 | |
52 // Decodes the mojom struct described by the |inout_struct| buffer; note that | |
53 // any references from the struct are also in |inout_struct|, and they are | |
54 // recursively decoded. Decodes all offsets to pointers, and decodes all handles | |
55 // by moving them out of |inout_handles| array using the encoded index into the | |
56 // array. | |
57 // |in_type_desc|: Describes the pointer and handle fields of the mojom struct. | |
58 // |inout_struct|: Contains the struct, and any other references outside of the | |
59 // struct. | |
60 // |in_struct_size|: Size of the buffer backed by |inout_struct| in bytes. | |
61 // |inout_handles|: Mojo handles are in this array, and are referenced by index | |
62 // in |inout_buf|. | |
63 // |in_num_handles|: Size in # of number elements available in |inout_handles|. | |
64 void MojomStruct_DecodePointersAndHandles( | |
65 const struct MojomTypeDescriptorStruct* in_type_desc, | |
66 struct MojomStructHeader* inout_struct, | |
67 uint32_t in_struct_size, | |
68 MojoHandle* inout_handles, | |
69 uint32_t in_num_handles); | |
70 | |
71 // Validates the mojom struct described by the |in_struct| buffer. Any | |
72 // references from the struct are also recursively validated, and are expected | |
73 // to be in the same buffer backing |in_struct|. | |
74 // |in_type_desc|: Describes the pointer and handle fields of the mojom struct. | |
75 // |inout_struct|: Buffer containing the struct, and any other references | |
76 // outside of the struct. | |
77 // |in_struct_size|: Size of the buffer backed by |inout_struct| in bytes. | |
78 // |in_num_handles|: Number of valid handles expected to be referenced from | |
79 // |in_struct|. | |
80 // |inout_context|: An initialized context that contains the expected location | |
81 // of the next pointer and next offset. This is used to | |
82 // validate that no two pointers or handles are shared. | |
83 MojomValidationResult MojomStruct_Validate( | |
84 const struct MojomTypeDescriptorStruct* in_type_desc, | |
85 const struct MojomStructHeader* in_struct, | |
86 uint32_t in_struct_size, | |
87 uint32_t in_num_handles, | |
88 struct MojomValidationContext* inout_context); | |
89 | |
90 // Creates a new copy of |in_struct| using |buffer| to allocate space. | |
91 // Recursively creates new copies of any references from |in_struct|, and | |
92 // updates the references to point to the new copies. This operation is useful | |
93 // if you want to linearize |in_struct| using the buffer backed by |buffer|. If | |
94 // there is insufficient space in the buffer or has unknown-typed data, this | |
95 // function returns false and the supplied buffer may be partially used. | |
96 // Otherwise, |out_struct| is set to the new copy of the struct. | |
97 // |buffer|: A mojom buffer used to allocate space for the new struct. | |
98 // |in_type_desc|: Describes the pointer and handle fields of the mojom struct. | |
99 // |in_struct|: The unencoded mojom struct to be copied. | |
100 // |out_struct|: Will be set to the new unencoded mojom struct. | |
101 bool MojomStruct_DeepCopy(struct MojomBuffer* buffer, | |
102 const struct MojomTypeDescriptorStruct* in_type_desc, | |
103 const struct MojomStructHeader* in_struct, | |
104 struct MojomStructHeader** out_struct); | |
105 | |
106 MOJO_END_EXTERN_C | |
107 | |
108 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_STRUCT_H_ | |
OLD | NEW |