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_ARRAY_H_ | |
6 #define MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_ARRAY_H_ | |
7 | |
8 #include <mojo/bindings/buffer.h> | |
9 #include <mojo/bindings/internal/type_descriptor.h> | |
10 #include <mojo/macros.h> | |
11 #include <stdint.h> | |
12 | |
13 MOJO_BEGIN_EXTERN_C | |
14 | |
15 // The fields below are just the header of a mojom array. The bytes that | |
16 // immediately follow this struct consist of |num_bytes - | |
17 // sizeof(MojomArrayHeader)| bytes describing |num_elements| elements of the | |
18 // array. | |
19 struct MojomArrayHeader { | |
20 // num_bytes includes the size of this struct along with the accompanying | |
21 // array bytes that follow these fields. | |
22 uint32_t num_bytes; | |
23 uint32_t num_elements; | |
24 }; | |
25 MOJO_STATIC_ASSERT(sizeof(struct MojomArrayHeader) == 8, | |
26 "struct MojomArrayHeader must be 8 bytes."); | |
27 | |
28 // This union is used to represent references to a mojom array. | |
29 union MojomArrayHeaderPtr { | |
30 // |ptr| is used to access the array when it hasn't been encoded yet. | |
31 struct MojomArrayHeader* ptr; | |
32 // |offset| is used to access the array after it has been encoded. | |
33 uint64_t offset; | |
34 }; | |
35 MOJO_STATIC_ASSERT(sizeof(union MojomArrayHeaderPtr) == 8, | |
36 "union MojomArrayHeaderPtr must be 8 bytes."); | |
37 | |
38 // Allocates enough space using the given |buffer| for |num_elements|, each of | |
39 // which is |element_byte_size| bytes in size. Returns NULL on failure. | |
40 struct MojomArrayHeader* MojomArray_New(struct MojomBuffer* buffer, | |
41 uint32_t num_elements, | |
42 uint32_t element_byte_size); | |
43 | |
44 // This is a macro for accessing a particular element in a mojom array. Given | |
45 // |base|, which pointers to a |struct MojomArrayHeader|, extracts the |index|th | |
46 // element, where each element is |sizeof(type)| bytes. | |
47 #define MOJOM_ARRAY_INDEX(base, type, index) \ | |
48 ((type*)((char*)(base) + sizeof(struct MojomArrayHeader) + \ | |
49 sizeof(type) * (index))) | |
50 | |
51 // Returns the number of bytes required to serialize this mojom array. | |
52 // |in_type_desc| is the generated descriptor entry that describes |in_array|. | |
53 // The user isn't expected to call this function directly, but this will | |
54 // probably be called when |ComputeSerializedSize()|ing a user-defined mojom | |
55 // struct. | |
56 size_t MojomArray_ComputeSerializedSize( | |
57 const struct MojomTypeDescriptorArray* in_type_desc, | |
58 const struct MojomArrayHeader* in_array_data); | |
59 | |
60 // Encodes the mojom array described by the |inout_array| buffer; note that any | |
61 // references from the array are also in the buffer backed by |inout_array|, and | |
62 // they are recursively encoded. Encodes all pointers to relative offsets, and | |
63 // encodes all handles by moving them into |inout_handles_buffer| and encoding | |
64 // the index into the handle. | |
65 // |in_type_desc|: Describes the pointer and handle fields of the mojom array. | |
66 // |inout_array|: Contains the array, and any other references outside the | |
67 // array. | |
68 // |in_array_size|: Size of the buffer backed by |inout_array| in bytes. | |
69 // |inout_handles_buffer|: | |
70 // A buffer used to record handles during encoding. The |num_handles_used| | |
71 // field can be used to determine how many handles were moved into this | |
72 // buffer after this function returns. | |
73 void MojomArray_EncodePointersAndHandles( | |
74 const struct MojomTypeDescriptorArray* in_type_desc, | |
75 struct MojomArrayHeader* inout_array, | |
76 uint32_t in_array_size, | |
77 struct MojomHandleBuffer* inout_handles_buffer); | |
78 | |
79 // Decodes the mojom array described by the |inout_array| buffer; note that any | |
80 // references from the array are also in the buffer backed by |inout_array|, and | |
81 // they are recursively decoded. Decodes all offset to pointers, and decodes all | |
82 // handles by moving them out of |inout_handles| array using the encoded index. | |
83 // |in_type_desc|: Describes the pointer and handle fields of the mojom array. | |
84 // |inout_array|: Contains the array, and any other references outside the | |
85 // array. | |
86 // |in_array_size|: Size of the buffer backed by |inout_array|. | |
87 // |inout_handles|: Mojo handles are moved out of this array, and are references | |
88 // by index in |inout_buf|. | |
89 // |in_num_handles|: Size in # of number elements available in |inout_handles|. | |
90 void MojomArray_DecodePointersAndHandles( | |
91 const struct MojomTypeDescriptorArray* in_type_desc, | |
92 struct MojomArrayHeader* inout_array, | |
93 uint32_t in_array_size, | |
94 MojoHandle* inout_handles, | |
95 uint32_t in_num_handles); | |
96 | |
97 // Validates the mojom array described by the |in_struct| buffer. Any | |
98 // references from the array are also recursively validated, and are expected | |
99 // to be in the same buffer backing |in_array|. | |
100 // |in_type_desc|: Describes the pointer and handle fields of the mojom array. | |
101 // |in_array|: Buffer containing the array, and any other references outside of | |
102 // the array | |
103 // |in_array_size|: Size of the buffer backed by |in_array| in bytes. | |
104 // |in_num_handles|: Number of valid handles expected to be referenced from | |
105 // |in_array|. | |
106 // |inout_context|: An initialized context that contains the expected location | |
107 // of the next pointer and next offset. This is used to | |
108 // validate that no two pointers or handles are shared. | |
109 MojomValidationResult MojomArray_Validate( | |
110 const struct MojomTypeDescriptorArray* in_type_desc, | |
111 const struct MojomArrayHeader* in_array, | |
112 uint32_t in_array_size, | |
113 uint32_t in_num_handles, | |
114 struct MojomValidationContext* inout_context); | |
115 | |
116 // Creates a new copy of |in_array| using |buffer| to allocate space. | |
117 // Recursively creates new copies of any references from |in_array|, and updates | |
118 // the references to point to the new copies. This operation is useful if you | |
119 // want to linearize |in_array| using the buffer backed by |buffer|. If there is | |
120 // insufficient space in the buffer or has unknown-typed data, this function | |
121 // returns false and the supplied buffer may be partially used. Otherwise, | |
122 // |out_array| is set to the new copy of the struct. | |
123 // |buffer|: A mojom buffer used to allocate space for the new array. | |
124 // |in_type_desc|: Describes the pointer and handle fields of the mojom array. | |
125 // |in_array|: The unencoded mojom array to be copied. | |
126 // |out_array|: Will be set to the new unencoded mojom array. | |
127 bool MojomArray_DeepCopy(struct MojomBuffer* buffer, | |
128 const struct MojomTypeDescriptorArray* in_type_desc, | |
129 const struct MojomArrayHeader* in_array, | |
130 struct MojomArrayHeader** out_array); | |
131 | |
132 MOJO_END_EXTERN_C | |
133 | |
134 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_ARRAY_H_ | |
OLD | NEW |