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
)
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 __hidden
void *xzmalloc(size_t size
)
40 void *ptr
= xmalloc(size
);
45 __hidden
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 __hidden
void *xmallocz(size_t size
)
65 if (unlikely(size
+ 1 < size
))
66 panic("xmallocz: data too large to fit into virtual "
69 ptr
= xmalloc(size
+ 1);
70 ((char*) ptr
)[size
] = 0;
75 __hidden
void *xmemdupz(const void *data
, size_t len
)
77 return memcpy(xmallocz(len
), data
, len
);
80 __hidden
void *xrealloc(void *ptr
, size_t nmemb
, size_t size
)
83 size_t new_size
= nmemb
* size
;
85 if (unlikely(new_size
== 0))
86 panic("xrealloc: zero size\n");
87 if (unlikely(((size_t) ~0) / nmemb
< size
))
88 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
91 new_ptr
= malloc(new_size
);
93 new_ptr
= realloc(ptr
, new_size
);
95 if (unlikely(new_ptr
== NULL
))
96 panic("xrealloc: out of memory (new_size %zu bytes)\n",
102 __hidden
void xfree_func(void *ptr
)
104 if (unlikely(ptr
== NULL
))
105 panic("xfree: NULL pointer given as argument\n");
110 __hidden
char *xstrdup(const char *str
)
115 len
= strlen(str
) + 1;
118 strlcpy(cp
, str
, len
);
123 __hidden
char *xstrndup(const char *str
, size_t size
)
128 len
= strlen(str
) + 1;
134 strlcpy(cp
, str
, len
);
139 __hidden
int xdup(int fd
)
142 if (unlikely(ret
< 0))
143 panic("xdup: dup failed\n");