File: xmalloc.h

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (68 lines) | stat: -rw-r--r-- 2,079 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef __COMMON_XMALLOC_H__
#define __COMMON_XMALLOC_H__

#include <stdlib.h>
#include <string.h>

#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__ */