[PATCH] headers: util.h -- Move memory helpers to xmalloc.h

Cyrill Gorcunov gorcunov at openvz.org
Mon Apr 1 11:25:09 EDT 2013


To reuse them outside of crtools code.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/util.h    | 33 +--------------------------------
 include/xmalloc.h | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 32 deletions(-)
 create mode 100644 include/xmalloc.h

diff --git a/include/util.h b/include/util.h
index 5afffc7..4374f50 100644
--- a/include/util.h
+++ b/include/util.h
@@ -14,6 +14,7 @@
 
 #include "compiler.h"
 #include "asm/types.h"
+#include "xmalloc.h"
 #include "bug.h"
 #include "log.h"
 #include "err.h"
@@ -101,9 +102,6 @@ static inline int read_img_buf(int fd, void *ptr, int size)
 
 #define read_img(fd, ptr)	read_img_buf((fd), (ptr), sizeof(*(ptr)))
 
-#define memzero_p(p)		memset(p, 0, sizeof(*p))
-#define memzero(p, size)	memset(p, 0, size)
-
 struct vma_area;
 struct list_head;
 
@@ -203,35 +201,6 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...);
 		__f;							\
 	 })
 
-#define __xalloc(op, size, ...)						\
-	({								\
-		void *___p = op( __VA_ARGS__ );				\
-		if (!___p)						\
-			pr_err("%s: Can't allocate %li bytes\n",	\
-			       __func__, (long)(size));			\
-		___p;							\
-	})
-
-#include <stdlib.h>
-
-#define xstrdup(str)		__xalloc(strdup, strlen(str) + 1, str)
-#define xmalloc(size)		__xalloc(malloc, size, size)
-#define xzalloc(size)		__xalloc(calloc, size, 1, size)
-#define xrealloc(p, size)	__xalloc(realloc, size, p, size)
-
-#define xfree(p)		do { if (p) free(p); } while (0)
-
-#define xrealloc_safe(pptr, size)					\
-	({								\
-		int __ret = -1;						\
-		void *new = xrealloc(*pptr, size);			\
-		if (new) {						\
-			*pptr = new;					\
-			__ret = 0;					\
-		}							\
-		__ret;							\
-	 })
-
 #define pr_img_head(type, ...)	pr_msg("\n"#type __VA_ARGS__ "\n----------------\n")
 #define pr_img_tail(type)	pr_msg("----------------\n")
 
diff --git a/include/xmalloc.h b/include/xmalloc.h
new file mode 100644
index 0000000..a5cc608
--- /dev/null
+++ b/include/xmalloc.h
@@ -0,0 +1,39 @@
+#ifndef __CR_XMALLOC_H__
+#define __CR_XMALLOC_H__
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "log.h"
+
+#define __xalloc(op, size, ...)						\
+	({								\
+		void *___p = op( __VA_ARGS__ );				\
+		if (!___p)						\
+			pr_err("%s: Can't allocate %li bytes\n",	\
+			       __func__, (long)(size));			\
+		___p;							\
+	})
+
+#define xstrdup(str)		__xalloc(strdup, strlen(str) + 1, str)
+#define xmalloc(size)		__xalloc(malloc, size, size)
+#define xzalloc(size)		__xalloc(calloc, size, 1, size)
+#define xrealloc(p, size)	__xalloc(realloc, size, p, size)
+
+#define xfree(p)		do { if (p) free(p); } while (0)
+
+#define xrealloc_safe(pptr, size)					\
+	({								\
+		int __ret = -1;						\
+		void *new = xrealloc(*pptr, size);			\
+		if (new) {						\
+			*pptr = new;					\
+			__ret = 0;					\
+		}							\
+		__ret;							\
+	 })
+
+#define memzero_p(p)		memset(p, 0, sizeof(*p))
+#define memzero(p, size)	memset(p, 0, size)
+
+#endif /* __CR_XMALLOC_H__ */
-- 
1.8.1.4


--WIyZ46R2i8wDzkSu--


More information about the CRIU mailing list