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_BINDINGS_UNION_H_ | |
6 #define MOJO_PUBLIC_C_BINDINGS_UNION_H_ | |
7 | |
8 #include <mojo/macros.h> | |
9 #include <stddef.h> | |
10 #include <stdint.h> | |
11 | |
12 #include "mojo/public/c/bindings/buffer.h" | |
13 #include "mojo/public/c/bindings/lib/type_descriptor.h" | |
14 #include "mojo/public/c/bindings/lib/util.h" | |
15 | |
16 MOJO_BEGIN_EXTERN_C | |
17 | |
18 struct MojomUnionLayout { | |
19 // |num_bytes|, the number of bytes used in this union, includes the size of | |
20 // the |num_bytes| and |tag| fields, along with data. | |
21 uint32_t size; | |
22 uint32_t tag; | |
23 union { | |
24 // To be used when the data is a pointer. | |
25 union MojomPointer pointer; | |
26 | |
27 // This is here to force this union data to be at least 8 bytes. | |
28 uint64_t force_size_; | |
29 } data; | |
30 }; | |
31 MOJO_STATIC_ASSERT(sizeof(struct MojomUnionLayout) == 16, | |
32 "struct MojomUnionLayout must be 16 bytes."); | |
33 | |
34 // Returns the number of bytes required to serialize |in_union_data|'s active | |
35 // field, not including the union layout (as described by |struct | |
36 // MojomUnionLayout|) itself. | |
37 // |in_type_desc| is the generated type that describes the tags of the pointer | |
38 // and handle types in the given union. | |
39 size_t MojomUnion_ComputeSerializedSize( | |
40 const struct MojomTypeDescriptorUnion* in_type_desc, | |
41 const struct MojomUnionLayout* in_union_data); | |
42 | |
43 // Encodes the mojom union described by the |inout_union| buffer; note that any | |
44 // references from the union are also in the buffer backed by |inout_union|, and | |
45 // they are recursively encoded. Encodes all pointers to relative offsets, and | |
46 // encodes all handles by moving them into |inout_handles_buffer| and encoding | |
47 // the index into the handle. | |
48 // |in_type_desc|: Describes the pointer and handle fields of the mojom union. | |
49 // |inout_union|: Contains the union, and any other references outside the | |
50 // union. | |
51 // |in_union_size|: Size of the buffer backed by |inout_union| in bytes. | |
52 // |inout_handles_buffer|: | |
53 // A buffer used to record handles during encoding. The |num_handles_used| | |
54 // field can be used to determine how many handles were moved into this | |
55 // buffer. | |
56 void MojomUnion_EncodePointersAndHandles( | |
57 const struct MojomTypeDescriptorUnion* in_type_desc, | |
58 struct MojomUnionLayout* inout_union, | |
59 uint32_t in_union_size, | |
60 struct MojomHandleBuffer* inout_handles_buffer); | |
61 | |
62 // Decodes the mojom union described by the |inout_union| buffer; note that any | |
63 // references from the union are also in the buffer backed by |inout_union|, and | |
64 // they are recursively decoded. Decodes all offsets to pointers, and decodes | |
65 // all handles by moving them out of |inout_handles| using the encoded index. | |
66 // |in_type_desc|: Describes the pointer and handle fields of the mojom union. | |
67 // |inout_union|: Contains the union, and any other references outside the | |
68 // union. | |
69 // |in_union_size|: Size of the buffer backed by |inout_union| in bytes. | |
70 // |inout_handles|: Mojo handles are moved out of this array and into | |
71 // |inout_union|, using the encoded index into this array. | |
72 // |in_num_handles|: Size in # of number elements available in |inout_handles|. | |
73 void MojomUnion_DecodePointersAndHandles( | |
74 const struct MojomTypeDescriptorUnion* in_type_desc, | |
75 struct MojomUnionLayout* inout_union, | |
76 uint32_t in_union_size, | |
77 MojoHandle* inout_handles, | |
78 uint32_t in_num_handles); | |
79 | |
80 // Validates the mojom union described by the |in_union| buffer. Any | |
81 // references from the union are also recursively validated, and are expected to | |
82 // be in the same buffer backing |in_union|. | |
83 // |in_type_desc|: Describes the pointer and handle fields of the mojom union. | |
84 // |in_union|: Buffer containing the union, and any other references | |
85 // outside of the union. | |
86 // |in_union_size|: Size of the buffer backed by |in_union| in bytes. | |
87 // |in_num_handles|: Number of valid handles expected to be referenced from | |
88 // |in_union|. | |
89 // |inout_context|: An initialized context that contains the expected location | |
90 // of the next pointer and next offset. This is used to | |
91 // validate that no two pointers or handles are shared. | |
92 MojomValidationResult MojomUnion_Validate( | |
93 const struct MojomTypeDescriptorUnion* in_type_desc, | |
94 bool in_nullable, | |
95 const struct MojomUnionLayout* in_union, | |
96 uint32_t in_union_size, | |
97 uint32_t in_num_handles, | |
98 struct MojomValidationContext* inout_context); | |
99 | |
100 // Copies over |in_union_data| into |out_union_data|, recursively copying any | |
101 // references from |in_union_data| using |buffer| to allocate space, and | |
102 // updating the references to point to the new copies. Note that a space is | |
103 // never allocated for the new union data in this function -- |out_union_data| | |
104 // should already be allocated for this function ahead of time. Returns false if | |
105 // the copy couldn't be made (insufficient space, or unrecognized tag), in which | |
106 // case the buffer may be partially used. | |
107 // |buffer|: A mojom buffer used to allocate space for any references from the | |
108 // union. | |
109 // |in_type_desc|: Describes the pointer and handle fields of the mojom union. | |
110 // |in_union_data|: The unencoded mojom union to be copied. | |
111 // |out_union_data|: Where |in_union_data| should be copied to. | |
112 bool MojomUnion_DeepCopy(struct MojomBuffer* buffer, | |
113 const struct MojomTypeDescriptorUnion* in_type_desc, | |
114 const struct MojomUnionLayout* in_union_data, | |
115 struct MojomUnionLayout* out_union_data); | |
116 | |
117 MOJO_END_EXTERN_C | |
118 | |
119 #endif // MOJO_PUBLIC_C_BINDINGS_UNION_H_ | |
OLD | NEW |