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

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: oops, include c.sha1. also properly merge Created 4 years, 5 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
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..ec6a8bdf1e67e1bedb20f1ce88a8f1e43d1bea87 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,32 @@ MojomValidationResult MojomArray_Validate(
return MOJOM_VALIDATION_ERROR_NONE;
}
+
+struct MojomArrayHeader* MojomArray_DeepCopy(
+ struct MojomBuffer* buffer,
+ const struct MojomTypeDescriptorArray* in_type_desc,
+ struct MojomArrayHeader* in_array) {
+ assert(in_type_desc);
+ assert(in_array);
+
+ struct MojomArrayHeader* out_array =
+ MojomBuffer_Allocate(buffer, in_array->num_bytes);
viettrungluu 2016/08/02 20:15:06 E.g., this could fail. Though at least here you ca
+ 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 out_array;
+
+ 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);
+
+ MojomType_DispatchDeepCopy(buffer, in_type_desc->elem_type,
viettrungluu 2016/08/02 20:15:06 Presumably, this could fail too. But that would po
+ in_type_desc->elem_descriptor, in_elem_data,
+ out_elem_data);
+ }
+
+ return out_array;
+}

Powered by Google App Engine
This is Rietveld 408576698