Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: mojo/public/c/bindings/lib/union.c

Issue 2200843002: C bindings: Implement _DeepCopy() & some unittests. (Closed) Base URL: git@github.com:domokit/mojo.git@cgen_validate
Patch Set: Address comments: no longer move, but copy instead. Return NULL if insufficient space. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/c/bindings/lib/type_descriptor.c ('k') | mojo/public/c/bindings/struct.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/c/bindings/union.h" 5 #include "mojo/public/c/bindings/union.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <string.h>
8 9
9 #include "mojo/public/c/bindings/lib/type_descriptor.h" 10 #include "mojo/public/c/bindings/lib/type_descriptor.h"
10 11
11 size_t MojomUnion_ComputeSerializedSize( 12 size_t MojomUnion_ComputeSerializedSize(
12 const struct MojomTypeDescriptorUnion* in_type_desc, 13 const struct MojomTypeDescriptorUnion* in_type_desc,
13 const struct MojomUnionLayout* in_union_data) { 14 const struct MojomUnionLayout* in_union_data) {
14 assert(in_type_desc); 15 assert(in_type_desc);
15 assert(in_union_data); 16 assert(in_union_data);
16 17
17 for (size_t i = 0; i < in_type_desc->num_entries; i++) { 18 for (size_t i = 0; i < in_type_desc->num_entries; i++) {
(...skipping 21 matching lines...) Expand all
39 assert(in_buf_size >= sizeof(struct MojomUnionLayout)); 40 assert(in_buf_size >= sizeof(struct MojomUnionLayout));
40 41
41 for (size_t i = 0; i < in_type_desc->num_entries; i++) { 42 for (size_t i = 0; i < in_type_desc->num_entries; i++) {
42 const struct MojomTypeDescriptorUnionEntry* entry = 43 const struct MojomTypeDescriptorUnionEntry* entry =
43 &(in_type_desc->entries[i]); 44 &(in_type_desc->entries[i]);
44 45
45 if (inout_union->tag != entry->tag) 46 if (inout_union->tag != entry->tag)
46 continue; 47 continue;
47 48
48 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) 49 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD)
49 continue; 50 continue;
viettrungluu 2016/08/03 16:43:24 Should this really be |continue|? Or should it be
vardhan 2016/08/03 23:11:01 It should be break, thanks!
50 51
51 MojomType_DispatchEncodePointersAndHandles( 52 MojomType_DispatchEncodePointersAndHandles(
52 entry->elem_type, 53 entry->elem_type,
53 entry->elem_descriptor, 54 entry->elem_descriptor,
54 entry->nullable, 55 entry->nullable,
55 &inout_union->data, 56 &inout_union->data,
56 in_buf_size - ((char*)&inout_union->data - (char*)inout_union), 57 in_buf_size - ((char*)&inout_union->data - (char*)inout_union),
57 inout_handles_buffer); 58 inout_handles_buffer);
59
60 break;
58 } 61 }
59 } 62 }
60 63
61 void MojomUnion_DecodePointersAndHandles( 64 void MojomUnion_DecodePointersAndHandles(
62 const struct MojomTypeDescriptorUnion* in_type_desc, 65 const struct MojomTypeDescriptorUnion* in_type_desc,
63 struct MojomUnionLayout* inout_union, 66 struct MojomUnionLayout* inout_union,
64 uint32_t in_union_size, 67 uint32_t in_union_size,
65 MojoHandle* inout_handles, 68 MojoHandle* inout_handles,
66 uint32_t in_num_handles) { 69 uint32_t in_num_handles) {
67 assert(in_union_size >= sizeof(struct MojomUnionLayout)); 70 assert(in_union_size >= sizeof(struct MojomUnionLayout));
68 assert(inout_handles != NULL || in_num_handles == 0); 71 assert(inout_handles != NULL || in_num_handles == 0);
69 72
70 for (size_t i = 0; i < in_type_desc->num_entries; i++) { 73 for (size_t i = 0; i < in_type_desc->num_entries; i++) {
71 const struct MojomTypeDescriptorUnionEntry* entry = 74 const struct MojomTypeDescriptorUnionEntry* entry =
72 &(in_type_desc->entries[i]); 75 &(in_type_desc->entries[i]);
73 76
74 if (inout_union->tag != entry->tag) 77 if (inout_union->tag != entry->tag)
75 continue; 78 continue;
76 79
77 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) 80 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD)
78 continue; 81 continue;
viettrungluu 2016/08/03 16:43:24 "
79 82
80 MojomType_DispatchDecodePointersAndHandles( 83 MojomType_DispatchDecodePointersAndHandles(
81 entry->elem_type, 84 entry->elem_type,
82 entry->elem_descriptor, 85 entry->elem_descriptor,
83 entry->nullable, 86 entry->nullable,
84 &inout_union->data, 87 &inout_union->data,
85 in_union_size - ((char*)&inout_union->data - (char*)inout_union), 88 in_union_size - ((char*)&inout_union->data - (char*)inout_union),
86 inout_handles, 89 inout_handles,
87 in_num_handles); 90 in_num_handles);
91
92 break;
88 } 93 }
89 } 94 }
90 95
91 MojomValidationResult MojomUnion_Validate( 96 MojomValidationResult MojomUnion_Validate(
92 const struct MojomTypeDescriptorUnion* in_type_desc, 97 const struct MojomTypeDescriptorUnion* in_type_desc,
93 bool in_nullable, 98 bool in_nullable,
94 const struct MojomUnionLayout* in_union, 99 const struct MojomUnionLayout* in_union,
95 uint32_t in_union_size, 100 uint32_t in_union_size,
96 uint32_t in_num_handles, 101 uint32_t in_num_handles,
97 struct MojomValidationContext* inout_context) { 102 struct MojomValidationContext* inout_context) {
98 for (size_t i = 0; i < in_type_desc->num_entries; i++) { 103 for (size_t i = 0; i < in_type_desc->num_entries; i++) {
99 const struct MojomTypeDescriptorUnionEntry* entry = 104 const struct MojomTypeDescriptorUnionEntry* entry =
100 &(in_type_desc->entries[i]); 105 &(in_type_desc->entries[i]);
101 106
102 if (in_union->tag != entry->tag) 107 if (in_union->tag != entry->tag)
103 continue; 108 continue;
104 109
105 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) 110 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD)
106 continue; 111 continue;
viettrungluu 2016/08/03 16:43:24 "
107 112
108 if (!in_nullable && in_union->size != sizeof(struct MojomUnionLayout)) 113 if (!in_nullable && in_union->size != sizeof(struct MojomUnionLayout))
109 return MOJOM_VALIDATION_UNEXPECTED_NULL_UNION; 114 return MOJOM_VALIDATION_UNEXPECTED_NULL_UNION;
110 115
111 MojomValidationResult result = MojomType_DispatchValidate( 116 return MojomType_DispatchValidate(
112 entry->elem_type, 117 entry->elem_type,
113 entry->elem_descriptor, 118 entry->elem_descriptor,
114 entry->nullable, 119 entry->nullable,
115 &(in_union->data), 120 &(in_union->data),
116 in_union_size - ((char*)&(in_union->data) - (char*)in_union), 121 in_union_size - ((char*)&(in_union->data) - (char*)in_union),
117 in_num_handles, 122 in_num_handles,
118 inout_context); 123 inout_context);
119 if (result != MOJOM_VALIDATION_ERROR_NONE)
120 return result;
121 } 124 }
122 return MOJOM_VALIDATION_ERROR_NONE; 125 return MOJOM_VALIDATION_ERROR_NONE;
123 } 126 }
127
128 bool MojomUnion_DeepCopy(struct MojomBuffer* buffer,
129 const struct MojomTypeDescriptorUnion* in_type_desc,
130 const struct MojomUnionLayout* in_union_data,
131 struct MojomUnionLayout* out_union_data) {
132 memcpy(out_union_data, in_union_data, sizeof(struct MojomUnionLayout));
133
134 // Unions with size 0 are null.
135 if (in_union_data->size == 0)
136 return true;
137
138 for (size_t i = 0; i < in_type_desc->num_entries; i++) {
139 const struct MojomTypeDescriptorUnionEntry* entry =
140 &(in_type_desc->entries[i]);
141 if (in_union_data->tag != entry->tag)
142 continue;
143
144 // We should skip non-pointer types.
145 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD)
146 continue;
viettrungluu 2016/08/03 16:43:24 " (Or maybe |return true;|?)
147
148 return MojomType_DispatchDeepCopy(
149 buffer, entry->elem_type, entry->elem_descriptor,
150 &(in_union_data->data), &(out_union_data->data));
151 }
152
153 return true;
viettrungluu 2016/08/03 16:43:24 Hmmm, returning true is a little odd: It means tha
vardhan 2016/08/03 23:11:01 My assumption was that it shouldn't fail, but disc
154 }
OLDNEW
« no previous file with comments | « mojo/public/c/bindings/lib/type_descriptor.c ('k') | mojo/public/c/bindings/struct.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698