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 __hidden
void *xmalloc(size_t size
)
26 if (unlikely(size
== 0))
27 panic("xmalloc: zero size\n");
29 if (unlikely(ptr
== NULL
))
30 panic("xmalloc: out of memory (allocating %zu bytes)\n",
35 __hidden
void *xzmalloc(size_t size
)
38 if (unlikely(size
== 0))
39 panic("xzmalloc: zero size\n");
41 if (unlikely(ptr
== NULL
))
42 panic("xzmalloc: out of memory (allocating %zu bytes)\n",
48 __hidden
void *xmalloc_aligned(size_t size
, size_t alignment
)
52 if (unlikely(size
== 0))
53 panic("xmalloc_aligned: zero size\n");
54 ret
= posix_memalign(&ptr
, alignment
, size
);
55 if (unlikely(ret
!= 0))
56 panic("xmalloc_aligned: out of memory (allocating %zu "
61 __hidden
void *xmallocz(size_t size
)
64 if (unlikely(size
+ 1 < size
))
65 panic("xmallocz: data too large to fit into virtual "
67 ptr
= xmalloc(size
+ 1);
68 ((char*) ptr
)[size
] = 0;
72 __hidden
void *xmemdupz(const void *data
, size_t len
)
74 return memcpy(xmallocz(len
), data
, len
);
77 __hidden
void *xrealloc(void *ptr
, size_t nmemb
, size_t size
)
80 size_t new_size
= nmemb
* size
;
81 if (unlikely(new_size
== 0))
82 panic("xrealloc: zero size\n");
83 if (unlikely(((size_t) ~0) / nmemb
< size
))
84 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
86 new_ptr
= malloc(new_size
);
88 new_ptr
= realloc(ptr
, new_size
);
89 if (unlikely(new_ptr
== NULL
))
90 panic("xrealloc: out of memory (new_size %zu bytes)\n",
95 __hidden
void xfree_func(void *ptr
)
97 if (unlikely(ptr
== NULL
))
98 panic("xfree: NULL pointer given as argument\n");
102 __hidden
char *xstrdup(const char *str
)
106 len
= strlen(str
) + 1;
108 strlcpy(cp
, str
, len
);
112 __hidden
char *xstrndup(const char *str
, size_t size
)
116 len
= strlen(str
) + 1;
120 strlcpy(cp
, str
, len
);
124 __hidden
int xdup(int fd
)
127 if (unlikely(ret
< 0))
128 panic("xdup: dup failed\n");