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

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

Issue 2200843002: C bindings: Implement _DeepCopy() & some unittests. (Closed) Base URL: git@github.com:domokit/mojo.git@cgen_validate
Patch Set: oops, include c.sha1. also properly merge 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
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/struct.h" 5 #include "mojo/public/c/bindings/struct.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 #include "mojo/public/c/bindings/union.h" 11 #include "mojo/public/c/bindings/union.h"
11 12
12 size_t MojomStruct_ComputeSerializedSize( 13 size_t MojomStruct_ComputeSerializedSize(
13 const struct MojomTypeDescriptorStruct* in_type_desc, 14 const struct MojomTypeDescriptorStruct* in_type_desc,
14 const struct MojomStructHeader* in_struct) { 15 const struct MojomStructHeader* in_struct) {
15 assert(in_struct); 16 assert(in_struct);
16 assert(in_type_desc); 17 assert(in_type_desc);
17 18
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 elem_data, 157 elem_data,
157 in_struct_size - ((char*)elem_data - (char*)in_struct), 158 in_struct_size - ((char*)elem_data - (char*)in_struct),
158 in_num_handles, 159 in_num_handles,
159 inout_context); 160 inout_context);
160 if (result != MOJOM_VALIDATION_ERROR_NONE) 161 if (result != MOJOM_VALIDATION_ERROR_NONE)
161 return result; 162 return result;
162 } 163 }
163 164
164 return MOJOM_VALIDATION_ERROR_NONE; 165 return MOJOM_VALIDATION_ERROR_NONE;
165 } 166 }
167
168 struct MojomStructHeader* MojomStruct_DeepCopy(
169 struct MojomBuffer* buffer,
170 const struct MojomTypeDescriptorStruct* in_type_desc,
171 struct MojomStructHeader* in_struct) {
172 assert(in_type_desc);
173 assert(in_struct);
174 assert(buffer->buf_size - buffer->num_bytes_used >= in_struct->num_bytes);
175
176 struct MojomStructHeader* out_struct =
177 MojomBuffer_Allocate(buffer, in_struct->num_bytes);
178 memcpy(out_struct, in_struct, in_struct->num_bytes);
179
180 for (size_t i = 0; i < in_type_desc->num_entries; i++) {
181 const struct MojomTypeDescriptorStructEntry* entry =
182 &(in_type_desc->entries[i]);
183
184 if (in_struct->version < entry->min_version)
185 continue;
186
187 void* in_elem_data =
188 ((char*)in_struct + sizeof(struct MojomStructHeader) + entry->offset);
189 void* out_elem_data = ((char*)out_struct +
190 sizeof(struct MojomStructHeader) + entry->offset);
191 MojomType_DispatchDeepCopy(
192 buffer,
193 entry->elem_type,
194 entry->elem_descriptor,
195 in_elem_data,
196 out_elem_data);
197 }
198
199 return out_struct;
200 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698