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.
16 #include <sys/types.h>
23 void *xmalloc(size_t size
)
27 if (unlikely(size
== 0))
28 panic("xmalloc: zero size\n");
31 if (unlikely(ptr
== NULL
))
32 panic("xmalloc: out of memory (allocating %zu bytes)\n",
38 void *xcalloc(size_t nmemb
, size_t size
)
42 if (unlikely(nmemb
== 0 || size
== 0))
43 panic("xcalloc: zero size\n");
45 ptr
= calloc(nmemb
, size
);
46 if (unlikely(ptr
== NULL
))
47 panic("xcalloc: out of memory (allocating %zu members of "
48 "%zu bytes)\n", nmemb
, size
);
53 void *xzmalloc(size_t size
)
55 void *ptr
= xmalloc(size
);
60 void *xmalloc_aligned(size_t size
, size_t alignment
)
65 if (unlikely(size
== 0))
66 panic("xmalloc_aligned: zero size\n");
68 ret
= posix_memalign(&ptr
, alignment
, size
);
69 if (unlikely(ret
!= 0))
70 panic("xmalloc_aligned: out of memory (allocating %zu "
76 void *xzmalloc_aligned(size_t size
, size_t alignment
)
78 void *ptr
= xmalloc_aligned(size
, alignment
);
83 void *xmallocz(size_t size
)
87 if (unlikely(size
+ 1 < size
))
88 panic("xmallocz: data too large to fit into virtual "
91 ptr
= xmalloc(size
+ 1);
92 ((char*) ptr
)[size
] = 0;
97 void *xmemdupz(const void *data
, size_t len
)
99 return memcpy(xmallocz(len
), data
, len
);
102 void *xrealloc(void *ptr
, size_t size
)
106 if (unlikely(size
== 0))
107 panic("xrealloc: zero size\n");
109 new_ptr
= realloc(ptr
, size
);
110 if (unlikely(new_ptr
== NULL
))
111 panic("xrealloc: out of memory (allocating %zu bytes)\n", size
);
116 void xfree_func(void *ptr
)
118 if (unlikely(ptr
== NULL
))
119 panic("xfree: NULL pointer given as argument\n");
124 char *xstrdup(const char *str
)
129 len
= strlen(str
) + 1;
132 strlcpy(cp
, str
, len
);
137 char *xstrndup(const char *str
, size_t size
)
142 len
= strlen(str
) + 1;
148 strlcpy(cp
, str
, len
);