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

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: 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 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..0e7ce7a956ef94c5f33a8b90311dd804bb26d5a4 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,36 @@ MojomValidationResult MojomArray_Validate(
return MOJOM_VALIDATION_ERROR_NONE;
}
+
+struct MojomArrayHeader* MojomArray_DeepCopy(
+ struct MojomBuffer* buffer,
+ const struct MojomTypeDescriptorArray* in_type_desc,
+ const struct MojomArrayHeader* in_array) {
+ assert(in_type_desc);
+ assert(in_array);
+
+ struct MojomArrayHeader* out_array =
+ MojomBuffer_Allocate(buffer, in_array->num_bytes);
+ if (out_array == NULL)
+ return NULL;
+ 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);
+
+ if (!MojomType_DispatchDeepCopy(buffer, in_type_desc->elem_type,
+ in_type_desc->elem_descriptor, in_elem_data,
+ out_elem_data)) {
+ return NULL;
+ }
+ }
+
+ return out_array;
+}

Powered by Google App Engine
This is Rietveld 408576698