Added header comment
[transsip.git] / src / xmalloc.c
blobd627aa09f72be23fa23f32f34f35c94f778ea382
1 /*
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.
7 */
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <malloc.h>
16 #ifndef SIZE_T_MAX
17 # define SIZE_T_MAX ((size_t) ~0)
18 #endif
20 #include "compiler.h"
21 #include "xmalloc.h"
22 #include "xutils.h"
23 #include "die.h"
25 __hidden void *xmalloc(size_t size)
27 void *ptr;
28 if (size == 0)
29 panic("xmalloc: zero size\n");
30 ptr = malloc(size);
31 if (ptr == NULL)
32 panic("xmalloc: out of memory (allocating %zu bytes)\n", size);
33 return ptr;
36 __hidden void *xvalloc(size_t size)
38 void *ptr;
39 if (size == 0)
40 panic("xmalloc: zero size\n");
41 ptr = valloc(size);
42 if (ptr == NULL)
43 panic("xvalloc: out of memory (allocating %zu bytes)\n", size);
44 return ptr;
47 __hidden void *xzmalloc(size_t size)
49 void *ptr;
50 if (size == 0)
51 panic("xzmalloc: zero size\n");
52 ptr = malloc(size);
53 if (ptr == NULL)
54 panic("xzmalloc: out of memory (allocating %zu bytes)\n", size);
55 memset(ptr, 0, size);
56 return ptr;
59 __hidden void *xmalloc_aligned(size_t size, size_t alignment)
61 int ret;
62 void *ptr;
63 if (size == 0)
64 panic("xmalloc_aligned: zero size\n");
65 ret = posix_memalign(&ptr, alignment, size);
66 if (ret != 0)
67 panic("xmalloc_aligned: out of memory (allocating %zu bytes)\n",
68 size);
69 return ptr;
72 __hidden void *xmallocz(size_t size)
74 void *ptr;
75 if (size + 1 < 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;
80 return ptr;
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)
90 void *new_ptr;
91 size_t new_size = nmemb * size;
92 if (new_size == 0)
93 panic("xrealloc: zero size\n");
94 if (SIZE_T_MAX / nmemb < size)
95 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
96 if (ptr == NULL)
97 new_ptr = malloc(new_size);
98 else
99 new_ptr = realloc(ptr, new_size);
100 if (new_ptr == NULL)
101 panic("xrealloc: out of memory (size %zu bytes)\n", new_size);
102 return new_ptr;
105 __hidden void xfree(void *ptr)
107 if (ptr == NULL)
108 panic("xfree: NULL pointer given as argument\n");
109 free(ptr);
112 __hidden char *xstrdup(const char *str)
114 size_t len;
115 char *cp;
116 len = strlen(str) + 1;
117 cp = xmalloc(len);
118 strlcpy(cp, str, len);
119 return cp;
122 __hidden char *xstrndup(const char *str, size_t size)
124 size_t len;
125 char *cp;
126 len = strlen(str) + 1;
127 if (size < len)
128 len = size;
129 cp = xmalloc(len);
130 strlcpy(cp, str, len);
131 return cp;
134 __hidden int xdup(int fd)
136 int ret = dup(fd);
137 if (ret < 0)
138 panic("xdup: dup failed\n");
139 return ret;