From 41aa20ab7e370d70cd82b07e7768ebe641f05218 Mon Sep 17 00:00:00 2001 From: Pavel Emelyanov Date: Thu, 27 Oct 2016 19:24:40 +0300 Subject: [PATCH] headers: Move xmalloc to common travis-ci: success for headers: Finalize include/common/ Signed-off-by: Pavel Emelyanov --- criu/include/xmalloc.h | 67 +------------------------------------- include/common/xmalloc.h | 69 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 include/common/xmalloc.h diff --git a/criu/include/xmalloc.h b/criu/include/xmalloc.h index e5ce279fa..09838b5c9 100644 --- a/criu/include/xmalloc.h +++ b/criu/include/xmalloc.h @@ -1,67 +1,2 @@ -#ifndef __CR_XMALLOC_H__ -#define __CR_XMALLOC_H__ - -#include -#include - #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) free(p) - -#define xrealloc_safe(pptr, size) \ - ({ \ - int __ret = -1; \ - void *new = xrealloc(*pptr, size); \ - if (new) { \ - *pptr = new; \ - __ret = 0; \ - } \ - __ret; \ - }) - -#define xmemdup(ptr, size) \ - ({ \ - void *new = xmalloc(size); \ - if (new) \ - memcpy(new, ptr, size); \ - new; \ - }) - -#define memzero_p(p) memset(p, 0, sizeof(*p)) -#define memzero(p, size) memset(p, 0, size) - -/* - * Helper for allocating trees with single xmalloc. - * This one advances the void *pointer on s bytes and - * returns the previous value. Use like this - * - * m = xmalloc(total_size); - * a = xptr_pull(&m, tree_root_t); - * a->b = xptr_pull(&m, leaf_a_t); - * a->c = xptr_pull(&m, leaf_c_t); - * ... - */ -static inline void *xptr_pull_s(void **m, size_t s) -{ - void *ret = (*m); - (*m) += s; - return ret; -} - -#define xptr_pull(m, type) xptr_pull_s(m, sizeof(type)) - -#endif /* __CR_XMALLOC_H__ */ +#include "common/xmalloc.h" diff --git a/include/common/xmalloc.h b/include/common/xmalloc.h new file mode 100644 index 000000000..d377c83a0 --- /dev/null +++ b/include/common/xmalloc.h @@ -0,0 +1,69 @@ +#ifndef __COMMON_XMALLOC_H__ +#define __COMMON_XMALLOC_H__ + +#include +#include + +#ifndef pr_err +#error "Macro pr_err is needed." +#endif + +#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) free(p) + +#define xrealloc_safe(pptr, size) \ + ({ \ + int __ret = -1; \ + void *new = xrealloc(*pptr, size); \ + if (new) { \ + *pptr = new; \ + __ret = 0; \ + } \ + __ret; \ + }) + +#define xmemdup(ptr, size) \ + ({ \ + void *new = xmalloc(size); \ + if (new) \ + memcpy(new, ptr, size); \ + new; \ + }) + +#define memzero_p(p) memset(p, 0, sizeof(*p)) +#define memzero(p, size) memset(p, 0, size) + +/* + * Helper for allocating trees with single xmalloc. + * This one advances the void *pointer on s bytes and + * returns the previous value. Use like this + * + * m = xmalloc(total_size); + * a = xptr_pull(&m, tree_root_t); + * a->b = xptr_pull(&m, leaf_a_t); + * a->c = xptr_pull(&m, leaf_c_t); + * ... + */ +static inline void *xptr_pull_s(void **m, size_t s) +{ + void *ret = (*m); + (*m) += s; + return ret; +} + +#define xptr_pull(m, type) xptr_pull_s(m, sizeof(type)) + +#endif /* __CR_XMALLOC_H__ */