[CRIU] [PATCH] protobuf: add helper function to calculate field size

Kinsbursky Stanislav skinsbursky at openvz.org
Fri Jul 27 09:53:19 EDT 2012


Signed-off-by: Stanislav Kinsbursky <skinsbursky at openvz.org>

---
 protobuf.c |   35 ++++++++++++++++++++++++++++++-----
 1 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/protobuf.c b/protobuf.c
index f5b5bd4..5970494 100644
--- a/protobuf.c
+++ b/protobuf.c
@@ -97,12 +97,41 @@ static void show_enum(void *msg, pb_pr_ctl_t *ctl)
 		pr_msg("%d", val);
 }
 
+static size_t get_field_size(int type)
+{
+	switch (type) {
+		case PROTOBUF_C_TYPE_INT32:
+		case PROTOBUF_C_TYPE_SINT32:
+		case PROTOBUF_C_TYPE_UINT32:
+		case PROTOBUF_C_TYPE_SFIXED32:
+		case PROTOBUF_C_TYPE_ENUM:
+		case PROTOBUF_C_TYPE_FLOAT:
+			return 4;
+		case PROTOBUF_C_TYPE_INT64:
+		case PROTOBUF_C_TYPE_SINT64:
+		case PROTOBUF_C_TYPE_SFIXED64:
+		case PROTOBUF_C_TYPE_FIXED32:
+		case PROTOBUF_C_TYPE_UINT64:
+		case PROTOBUF_C_TYPE_FIXED64:
+		case PROTOBUF_C_TYPE_DOUBLE:
+			return 8;
+		case PROTOBUF_C_TYPE_STRING:
+		case PROTOBUF_C_TYPE_MESSAGE:
+			return sizeof (void *);
+		case PROTOBUF_C_TYPE_BOOL:
+			return sizeof (protobuf_c_boolean);
+		case PROTOBUF_C_TYPE_BYTES:
+			return sizeof (ProtobufCBinaryData);
+	}
+	return 0;
+}
+
 static void pb_show_field(const ProtobufCFieldDescriptor *fd, void *where,
 			  unsigned long nr_fields, pb_pr_ctl_t *ctl)
 {
 	pb_pr_field_t *show;
 	unsigned long counter;
-	size_t fsize;
+	size_t fsize = get_field_size(fd->type);
 
 	print_tabs(ctl);
 
@@ -114,7 +143,6 @@ static void pb_show_field(const ProtobufCFieldDescriptor *fd, void *where,
 		case PROTOBUF_C_TYPE_UINT32:
 		case PROTOBUF_C_TYPE_SFIXED32:
 			show = pb_msg_int32x;
-			fsize = 4;
 			break;
 		case PROTOBUF_C_TYPE_INT64:
 		case PROTOBUF_C_TYPE_SINT64:
@@ -123,17 +151,14 @@ static void pb_show_field(const ProtobufCFieldDescriptor *fd, void *where,
 		case PROTOBUF_C_TYPE_UINT64:
 		case PROTOBUF_C_TYPE_FIXED64:
 			show = pb_msg_int64x;
-			fsize = 8;
 			break;
 		case PROTOBUF_C_TYPE_STRING:
 			show = pb_msg_string;
-			fsize = sizeof (void *);
 			break;
 		case PROTOBUF_C_TYPE_MESSAGE:
 			where = (void *)(*(long *)where);
 			ctl->arg = (void *)fd->descriptor;
 			show = show_nested_message;
-			fsize = sizeof (void *);
 			break;
 		case PROTOBUF_C_TYPE_ENUM:
 			show = show_enum;



More information about the CRIU mailing list