2 * transsip - the telephony toolkit
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011, 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
5 * Subject to the GPL, version 2.
20 __hidden
void *xmalloc(size_t size
)
25 panic("xmalloc: zero size\n");
29 panic("xmalloc: out of memory (allocating %zu bytes)\n", size
);
34 __hidden
void *xzmalloc(size_t size
)
39 panic("xzmalloc: zero size\n");
43 panic("xzmalloc: out of memory (allocating %zu bytes)\n", size
);
50 __hidden
void *xmalloc_aligned(size_t size
, size_t alignment
)
56 panic("xmalloc_aligned: zero size\n");
58 ret
= posix_memalign(&ptr
, alignment
, size
);
60 panic("xmalloc_aligned: out of memory (allocating %zu bytes)\n",
66 __hidden
void *xmemdup(const void *data
, size_t len
)
68 return memcpy(xmalloc(len
), data
, len
);
71 __hidden
void *xrealloc(void *ptr
, size_t nmemb
, size_t size
)
74 size_t new_size
= nmemb
* size
;
76 if (unlikely(new_size
== 0))
77 panic("xrealloc: zero size\n");
79 if (unlikely(((size_t) ~0) / nmemb
< size
))
80 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
83 new_ptr
= malloc(new_size
);
85 new_ptr
= realloc(ptr
, new_size
);
87 if (unlikely(new_ptr
== NULL
))
88 panic("xrealloc: out of memory (new_size %zu bytes)\n",
94 __hidden
void xfree(void *ptr
)
97 panic("xfree: NULL pointer given as argument\n");
102 __hidden
char *xstrdup(const char *str
)
107 len
= strlen(str
) + 1;
109 strlcpy(cp
, str
, len
);