[04/22] pb: Add a helper to collect single entry

Submitted by Pavel Emelianov on June 30, 2017, 10:56 a.m.

Details

Message ID adfe46d7-f352-0339-3687-d1cbb6611398@virtuozzo.com
State Accepted
Series "Merge images with different files into one"
Headers show

Commit Message

Pavel Emelianov June 30, 2017, 10:56 a.m.
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
---
 criu/include/protobuf.h |  1 +
 criu/protobuf.c         | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/include/protobuf.h b/criu/include/protobuf.h
index 6097ba0..fb7489e 100644
--- a/criu/include/protobuf.h
+++ b/criu/include/protobuf.h
@@ -40,6 +40,7 @@  struct collect_image_info {
 #define COLLECT_HAPPENED	0x4	/* image was opened and collected */
 
 extern int collect_image(struct collect_image_info *);
+extern int collect_entry(ProtobufCMessage *base, struct collect_image_info *cinfo);
 
 static inline int collect_images(struct collect_image_info **array, unsigned size)
 {
diff --git a/criu/protobuf.c b/criu/protobuf.c
index 219c7c0..8eb73e0 100644
--- a/criu/protobuf.c
+++ b/criu/protobuf.c
@@ -172,6 +172,37 @@  err:
 	return ret;
 }
 
+int collect_entry(ProtobufCMessage *msg, struct collect_image_info *cinfo)
+{
+	void *obj;
+	void *(*o_alloc)(size_t size) = malloc;
+	void (*o_free)(void *ptr) = free;
+
+	if (cinfo->flags & COLLECT_SHARED) {
+		o_alloc = shmalloc;
+		o_free = shfree_last;
+	}
+
+	if (cinfo->priv_size) {
+		obj = o_alloc(cinfo->priv_size);
+		if (!obj)
+			return -1;
+	} else
+		obj = NULL;
+
+	cinfo->flags |= COLLECT_HAPPENED;
+	if (cinfo->collect(obj, msg, NULL) < 0) {
+		o_free(obj);
+		cr_pb_descs[cinfo->pb_type].free(msg, NULL);
+		return -1;
+	}
+
+	if (!cinfo->priv_size && !(cinfo->flags & COLLECT_NOFREE))
+		cr_pb_descs[cinfo->pb_type].free(msg, NULL);
+
+	return 0;
+}
+
 int collect_image(struct collect_image_info *cinfo)
 {
 	int ret;