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

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: 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/struct.c
diff --git a/mojo/public/c/bindings/lib/struct.c b/mojo/public/c/bindings/lib/struct.c
index 93a552c5c7f75de34f677275f37cc544b31e105b..a0581fada54adc45b2dc5ac5a797e870db4bfe10 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,37 @@ MojomValidationResult MojomStruct_Validate(
return MOJOM_VALIDATION_ERROR_NONE;
}
+
+struct MojomStructHeader* MojomStruct_DeepCopy(
+ struct MojomBuffer* buffer,
+ const struct MojomTypeDescriptorStruct* in_type_desc,
+ struct MojomStructHeader* in_struct) {
+ assert(in_type_desc);
+ assert(in_struct);
+ assert(buffer->buf_size - buffer->num_bytes_used >= in_struct->num_bytes);
+
+ struct MojomStructHeader* out_struct =
+ MojomBuffer_Allocate(buffer, in_struct->num_bytes);
+ 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);
+ MojomType_DispatchDeepCopy(
+ buffer,
+ entry->elem_type,
+ entry->elem_descriptor,
+ in_elem_data,
+ out_elem_data);
+ }
+
+ return out_struct;
+}

Powered by Google App Engine
This is Rietveld 408576698