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

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

Issue 2200843002: C bindings: Implement _DeepCopy() & some unittests. (Closed) Base URL: git@github.com:domokit/mojo.git@cgen_validate
Patch Set: fix tab indent, update sha1 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/array.h ('k') | mojo/public/c/bindings/lib/struct.c » ('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/array.h" 5 #include "mojo/public/c/bindings/array.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <string.h>
10 11
11 #include "mojo/public/c/bindings/buffer.h" 12 #include "mojo/public/c/bindings/buffer.h"
12 #include "mojo/public/c/bindings/interface.h" 13 #include "mojo/public/c/bindings/interface.h"
13 #include "mojo/public/c/bindings/lib/type_descriptor.h" 14 #include "mojo/public/c/bindings/lib/type_descriptor.h"
14 #include "mojo/public/c/bindings/lib/util.h" 15 #include "mojo/public/c/bindings/lib/util.h"
15 #include "mojo/public/c/bindings/union.h" 16 #include "mojo/public/c/bindings/union.h"
16 17
17 struct MojomArrayHeader* MojomArray_New(struct MojomBuffer* buf, 18 struct MojomArrayHeader* MojomArray_New(struct MojomBuffer* buf,
18 uint32_t num_elements, 19 uint32_t num_elements,
19 uint32_t element_byte_size) { 20 uint32_t element_byte_size) {
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 elem_data, 210 elem_data,
210 in_array_size - (uint32_t)(elem_data - (char*)in_array), 211 in_array_size - (uint32_t)(elem_data - (char*)in_array),
211 in_num_handles, 212 in_num_handles,
212 inout_context); 213 inout_context);
213 if (result != MOJOM_VALIDATION_ERROR_NONE) 214 if (result != MOJOM_VALIDATION_ERROR_NONE)
214 return result; 215 return result;
215 } 216 }
216 217
217 return MOJOM_VALIDATION_ERROR_NONE; 218 return MOJOM_VALIDATION_ERROR_NONE;
218 } 219 }
220
221 bool MojomArray_DeepCopy(
222 struct MojomBuffer* buffer,
223 const struct MojomTypeDescriptorArray* in_type_desc,
224 const struct MojomArrayHeader* in_array,
225 struct MojomArrayHeader** out_array) {
226 assert(in_type_desc);
227 assert(in_array);
228 assert(out_array);
229
230 *out_array = MojomBuffer_Allocate(buffer, in_array->num_bytes);
231 if (*out_array == NULL)
232 return false;
233
234 memcpy(*out_array, in_array, in_array->num_bytes);
235
236 // Nothing else to copy for POD types.
237 if (in_type_desc->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD)
238 return true;
239
240 for (size_t i = 0; i < in_array->num_elements; i++) {
241 void* in_elem_data =
242 array_index_by_type(in_array, in_type_desc->elem_type, i);
243 void* out_elem_data =
244 array_index_by_type(*out_array, in_type_desc->elem_type, i);
245
246 if (!MojomType_DispatchDeepCopy(buffer, in_type_desc->elem_type,
247 in_type_desc->elem_descriptor, in_elem_data,
248 out_elem_data)) {
249 return false;
250 }
251 }
252
253 return true;
254 }
OLDNEW
« no previous file with comments | « mojo/public/c/bindings/array.h ('k') | mojo/public/c/bindings/lib/struct.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698