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