2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010, 2011, 2012 Daniel Borkmann.
4 * Copyright 2014, 2015 Tobias Klauser
5 * Subject to the GPL, version 2.
17 #include <sys/types.h>
24 void *xmalloc(size_t size
)
28 if (unlikely(size
== 0))
29 panic("xmalloc: zero size\n");
32 if (unlikely(ptr
== NULL
))
33 panic("xmalloc: out of memory (allocating %zu bytes)\n",
39 void *xcalloc(size_t nmemb
, size_t size
)
43 if (unlikely(nmemb
== 0 || size
== 0))
44 panic("xcalloc: zero size\n");
46 ptr
= calloc(nmemb
, size
);
47 if (unlikely(ptr
== NULL
))
48 panic("xcalloc: out of memory (allocating %zu members of "
49 "%zu bytes)\n", nmemb
, size
);
54 void *xzmalloc(size_t size
)
56 void *ptr
= xmalloc(size
);
61 void *xmalloc_aligned(size_t size
, size_t alignment
)
66 if (unlikely(size
== 0))
67 panic("xmalloc_aligned: zero size\n");
69 ret
= posix_memalign(&ptr
, alignment
, size
);
70 if (unlikely(ret
!= 0))
71 panic("xmalloc_aligned: out of memory (allocating %zu "
77 void *xzmalloc_aligned(size_t size
, size_t alignment
)
79 void *ptr
= xmalloc_aligned(size
, alignment
);
84 void *xmallocz(size_t size
)
88 if (unlikely(size
+ 1 < size
))
89 panic("xmallocz: data too large to fit into virtual "
92 ptr
= xmalloc(size
+ 1);
93 ((char*) ptr
)[size
] = 0;
98 void *xmemdupz(const void *data
, size_t len
)
100 return memcpy(xmallocz(len
), data
, len
);
103 void *xrealloc(void *ptr
, size_t size
)
107 if (unlikely(size
== 0))
108 panic("xrealloc: zero size\n");
110 new_ptr
= realloc(ptr
, size
);
111 if (unlikely(new_ptr
== NULL
))
112 panic("xrealloc: out of memory (allocating %zu bytes)\n", size
);
117 void xfree_func(void *ptr
)
119 if (unlikely(ptr
== NULL
))
120 panic("xfree: NULL pointer given as argument\n");
125 char *xstrdup(const char *str
)
130 len
= strlen(str
) + 1;
133 strlcpy(cp
, str
, len
);
138 char *xstrndup(const char *str
, size_t size
)
143 len
= strlen(str
) + 1;
149 strlcpy(cp
, str
, len
);