[CRIU] [PATCH 3/4] protbuf: Add PB helpers

Cyrill Gorcunov gorcunov at openvz.org
Fri Jul 6 08:10:47 EDT 2012


To not bloat util.h or image.h the protobuf.h
introduced to handle all PB needs. Note there
is no protobuf.c file because we don't need
it yet.

We represent our PB objects as a pairs of records

  | 4 bytes for object packed size
  +---
  | X bytes -- object itself

and to read/write them in one line code
pb_write, pb_read, pb_read_eof helpers added.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/protobuf.h |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)
 create mode 100644 include/protobuf.h

diff --git a/include/protobuf.h b/include/protobuf.h
new file mode 100644
index 0000000..5534d37
--- /dev/null
+++ b/include/protobuf.h
@@ -0,0 +1,52 @@
+#ifndef PROTOBUF_H__
+#define PROTOBUF_H__
+
+#include "types.h"
+#include "compiler.h"
+#include "util.h"
+
+#define pb_read_op(__fd, __obj_pptr, __unpack, __op)				\
+({										\
+	int __ret = -1;								\
+	u32 __size;								\
+	u8 *__buf;								\
+										\
+	__ret = read_img ##__op(__fd, &__size);					\
+	if (__ret > 0) {							\
+		__buf = xmalloc(__size);					\
+		if (__buf) {							\
+			__ret = read_img_buf(__fd, __buf, __size);		\
+			if (__ret > 0)						\
+				*(__obj_pptr) = __unpack(NULL, __size, __buf);	\
+			xfree(__buf);						\
+		} else								\
+			__ret = -1;						\
+	}									\
+										\
+	__ret;									\
+})
+
+#define pb_read(__fd, __obj_pptr, __unpack)					\
+	pb_read_op(__fd, __obj_pptr, __unpack, )
+
+#define pb_read_eof(__fd, __obj_pptr, __unpack)					\
+	pb_read_op(__fd, __obj_pptr, __unpack, _eof)
+
+#define pb_write(__fd, __obj_ptr, __get_pksize, __pack)				\
+({										\
+	u32 __size = __get_pksize(__obj_ptr);					\
+	u8 *__buf = xmalloc(__size);						\
+	int __ret = -1;								\
+										\
+	if (__buf) {								\
+		__pack(__obj_ptr, __buf);					\
+		__ret = write_img(__fd, &__size);				\
+		if (!__ret)							\
+			__ret = write_img_buf(__fd, __buf, __size);		\
+		xfree(__buf);							\
+	}									\
+										\
+	__ret;									\
+})
+
+#endif /* PROTOBUF_H__ */
-- 
1.7.7.6



More information about the CRIU mailing list