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

Unified 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: 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/lib/array.c ('k') | mojo/public/c/bindings/lib/type_descriptor.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/c/bindings/lib/struct.c
diff --git a/mojo/public/c/bindings/lib/struct.c b/mojo/public/c/bindings/lib/struct.c
index 93a552c5c7f75de34f677275f37cc544b31e105b..611bfa73bafad09aa96e9b17273361d5732912c2 100644
--- a/mojo/public/c/bindings/lib/struct.c
+++ b/mojo/public/c/bindings/lib/struct.c
@@ -5,6 +5,7 @@
#include "mojo/public/c/bindings/struct.h"
#include <assert.h>
+#include <string.h>
#include "mojo/public/c/bindings/lib/type_descriptor.h"
#include "mojo/public/c/bindings/union.h"
@@ -163,3 +164,42 @@ MojomValidationResult MojomStruct_Validate(
return MOJOM_VALIDATION_ERROR_NONE;
}
+
+bool MojomStruct_DeepCopy(
+ struct MojomBuffer* buffer,
+ const struct MojomTypeDescriptorStruct* in_type_desc,
+ const struct MojomStructHeader* in_struct,
+ struct MojomStructHeader** out_struct) {
+ assert(in_type_desc);
+ assert(in_struct);
+ assert(out_struct);
+
+ *out_struct = MojomBuffer_Allocate(buffer, in_struct->num_bytes);
+ if (*out_struct == NULL)
+ return false;
+
+ memcpy(*out_struct, in_struct, in_struct->num_bytes);
+
+ for (size_t i = 0; i < in_type_desc->num_entries; i++) {
+ const struct MojomTypeDescriptorStructEntry* entry =
+ &(in_type_desc->entries[i]);
+
+ if (in_struct->version < entry->min_version)
+ continue;
+
+ void* in_elem_data =
+ ((char*)in_struct + sizeof(struct MojomStructHeader) + entry->offset);
+ void* out_elem_data = ((char*)*out_struct +
+ sizeof(struct MojomStructHeader) + entry->offset);
+ if (!MojomType_DispatchDeepCopy(
+ buffer,
+ entry->elem_type,
+ entry->elem_descriptor,
+ in_elem_data,
+ out_elem_data)) {
+ return false;
+ }
+ }
+
+ return true;
+}
« no previous file with comments | « mojo/public/c/bindings/lib/array.c ('k') | mojo/public/c/bindings/lib/type_descriptor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698