2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010, 2011, 2012 Daniel Borkmann.
4 * Subject to the GPL, version 2.
15 #include <sys/types.h>
22 void *xmalloc(size_t size
)
26 if (unlikely(size
== 0))
27 panic("xmalloc: zero size\n");
30 if (unlikely(ptr
== NULL
))
31 panic("xmalloc: out of memory (allocating %zu bytes)\n",
37 void *xzmalloc(size_t size
)
39 void *ptr
= xmalloc(size
);
44 void *xmalloc_aligned(size_t size
, size_t alignment
)
49 if (unlikely(size
== 0))
50 panic("xmalloc_aligned: zero size\n");
52 ret
= posix_memalign(&ptr
, alignment
, size
);
53 if (unlikely(ret
!= 0))
54 panic("xmalloc_aligned: out of memory (allocating %zu "
60 void *xzmalloc_aligned(size_t size
, size_t alignment
)
62 void *ptr
= xmalloc_aligned(size
, alignment
);
67 void *xmallocz(size_t size
)
71 if (unlikely(size
+ 1 < size
))
72 panic("xmallocz: data too large to fit into virtual "
75 ptr
= xmalloc(size
+ 1);
76 ((char*) ptr
)[size
] = 0;
81 void *xmemdupz(const void *data
, size_t len
)
83 return memcpy(xmallocz(len
), data
, len
);
86 void *xrealloc(void *ptr
, size_t nmemb
, size_t size
)
89 size_t new_size
= nmemb
* size
;
91 if (unlikely(new_size
== 0))
92 panic("xrealloc: zero size\n");
93 if (unlikely(((size_t) ~0) / nmemb
< size
))
94 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
97 new_ptr
= malloc(new_size
);
99 new_ptr
= realloc(ptr
, new_size
);
101 if (unlikely(new_ptr
== NULL
))
102 panic("xrealloc: out of memory (new_size %zu bytes)\n",
108 void xfree_func(void *ptr
)
110 if (unlikely(ptr
== NULL
))
111 panic("xfree: NULL pointer given as argument\n");
116 char *xstrdup(const char *str
)
121 len
= strlen(str
) + 1;
124 strlcpy(cp
, str
, len
);
129 char *xstrndup(const char *str
, size_t size
)
134 len
= strlen(str
) + 1;
140 strlcpy(cp
, str
, len
);