2 * transsip - the telephony network
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
17 # define SIZE_T_MAX ((size_t) ~0)
25 __hidden
void *xmalloc(size_t size
)
29 panic("xmalloc: zero size\n");
32 panic("xmalloc: out of memory (allocating %zu bytes)\n", size
);
36 __hidden
void *xvalloc(size_t size
)
40 panic("xmalloc: zero size\n");
43 panic("xvalloc: out of memory (allocating %zu bytes)\n", size
);
47 __hidden
void *xzmalloc(size_t size
)
51 panic("xzmalloc: zero size\n");
54 panic("xzmalloc: out of memory (allocating %zu bytes)\n", size
);
59 __hidden
void *xmalloc_aligned(size_t size
, size_t alignment
)
64 panic("xmalloc_aligned: zero size\n");
65 ret
= posix_memalign(&ptr
, alignment
, size
);
67 panic("xmalloc_aligned: out of memory (allocating %zu bytes)\n",
72 __hidden
void *xmallocz(size_t size
)
76 panic("xmallocz: data too large to fit "
77 "into virtual memory space\n");
78 ptr
= xmalloc(size
+ 1);
79 ((char *) ptr
)[size
] = 0;
83 __hidden
void *xmemdupz(const void *data
, size_t len
)
85 return memcpy(xmallocz(len
), data
, len
);
88 __hidden
void *xrealloc(void *ptr
, size_t nmemb
, size_t size
)
91 size_t new_size
= nmemb
* size
;
93 panic("xrealloc: zero size\n");
94 if (SIZE_T_MAX
/ nmemb
< size
)
95 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
97 new_ptr
= malloc(new_size
);
99 new_ptr
= realloc(ptr
, new_size
);
101 panic("xrealloc: out of memory (size %zu bytes)\n", new_size
);
105 __hidden
void xfree(void *ptr
)
108 panic("xfree: NULL pointer given as argument\n");
112 __hidden
char *xstrdup(const char *str
)
116 len
= strlen(str
) + 1;
118 strlcpy(cp
, str
, len
);
122 __hidden
char *xstrndup(const char *str
, size_t size
)
126 len
= strlen(str
) + 1;
130 strlcpy(cp
, str
, len
);
134 __hidden
int xdup(int fd
)
138 panic("xdup: dup failed\n");