2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010, 2011, 2012 Daniel Borkmann.
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 *xzmalloc(size_t size
)
40 void *ptr
= xmalloc(size
);
45 void *xmalloc_aligned(size_t size
, size_t alignment
)
50 if (unlikely(size
== 0))
51 panic("xmalloc_aligned: zero size\n");
53 ret
= posix_memalign(&ptr
, alignment
, size
);
54 if (unlikely(ret
!= 0))
55 panic("xmalloc_aligned: out of memory (allocating %zu "
61 void *xzmalloc_aligned(size_t size
, size_t alignment
)
63 void *ptr
= xmalloc_aligned(size
, alignment
);
68 void *xmallocz(size_t size
)
72 if (unlikely(size
+ 1 < size
))
73 panic("xmallocz: data too large to fit into virtual "
76 ptr
= xmalloc(size
+ 1);
77 ((char*) ptr
)[size
] = 0;
82 void *xmemdupz(const void *data
, size_t len
)
84 return memcpy(xmallocz(len
), data
, len
);
87 void *xrealloc(void *ptr
, size_t nmemb
, size_t size
)
90 size_t new_size
= nmemb
* size
;
92 if (unlikely(new_size
== 0))
93 panic("xrealloc: zero size\n");
94 if (unlikely(((size_t) ~0) / nmemb
< size
))
95 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
98 new_ptr
= malloc(new_size
);
100 new_ptr
= realloc(ptr
, new_size
);
102 if (unlikely(new_ptr
== NULL
))
103 panic("xrealloc: out of memory (new_size %zu bytes)\n",
109 void xfree_func(void *ptr
)
111 if (unlikely(ptr
== NULL
))
112 panic("xfree: NULL pointer given as argument\n");
117 char *xstrdup(const char *str
)
122 len
= strlen(str
) + 1;
125 strlcpy(cp
, str
, len
);
130 char *xstrndup(const char *str
, size_t size
)
135 len
= strlen(str
) + 1;
141 strlcpy(cp
, str
, len
);
149 if (unlikely(ret
< 0))
150 panic("xdup: dup failed\n");