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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/c/bindings/lib/array.c
diff --git a/mojo/public/c/bindings/lib/array.c b/mojo/public/c/bindings/lib/array.c
index d5a5f651ab2128ef3eaef2c5b209c8c6568f2668..53e50c46f5264aae236444525b940d07ca3b0d90 100644
--- a/mojo/public/c/bindings/lib/array.c
+++ b/mojo/public/c/bindings/lib/array.c
@@ -7,6 +7,7 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
+#include <string.h>
#include "mojo/public/c/bindings/buffer.h"
#include "mojo/public/c/bindings/interface.h"
@@ -216,3 +217,38 @@ MojomValidationResult MojomArray_Validate(
return MOJOM_VALIDATION_ERROR_NONE;
}
+
+bool MojomArray_DeepCopy(
+ struct MojomBuffer* buffer,
+ const struct MojomTypeDescriptorArray* in_type_desc,
+ const struct MojomArrayHeader* in_array,
+ struct MojomArrayHeader** out_array) {
+ assert(in_type_desc);
+ assert(in_array);
+ assert(out_array);
+
+ *out_array = MojomBuffer_Allocate(buffer, in_array->num_bytes);
+ if (*out_array == NULL)
+ return false;
+
+ memcpy(*out_array, in_array, in_array->num_bytes);
+
+ // Nothing else to copy for POD types.
+ if (in_type_desc->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD)
+ return true;
+
+ for (size_t i = 0; i < in_array->num_elements; i++) {
+ void* in_elem_data =
+ array_index_by_type(in_array, in_type_desc->elem_type, i);
+ void* out_elem_data =
+ array_index_by_type(*out_array, in_type_desc->elem_type, i);
+
+ if (!MojomType_DispatchDeepCopy(buffer, in_type_desc->elem_type,
+ in_type_desc->elem_descriptor, in_elem_data,
+ out_elem_data)) {
+ return false;
+ }
+ }
+
+ return true;
+}
« 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