docs: tiny update
[ana-net.git] / app / xmalloc.c
blob8d2715012aadd17cb2fb86475ae1f67d349269b0
1 /*
2 * Lightweight Autonomic Network Architecture
3 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
4 * Swiss federal institute of technology (ETH Zurich)
5 * Subject to the GPL.
6 */
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <malloc.h>
15 #ifndef SIZE_T_MAX
16 # define SIZE_T_MAX ((size_t) ~0)
17 #endif
19 #include "compiler.h"
20 #include "xmalloc.h"
21 #include "strlcpy.h"
22 #include "die.h"
24 __hidden void *xmalloc(size_t size)
26 void *ptr;
27 if (size == 0)
28 panic("xmalloc: zero size\n");
29 ptr = malloc(size);
30 if (ptr == NULL)
31 panic("xmalloc: out of memory (allocating %zu bytes)\n", size);
32 return ptr;
35 __hidden void *xvalloc(size_t size)
37 void *ptr;
38 if (size == 0)
39 panic("xmalloc: zero size\n");
40 ptr = valloc(size);
41 if (ptr == NULL)
42 panic("xvalloc: out of memory (allocating %zu bytes)\n", size);
43 return ptr;
46 __hidden void *xzmalloc(size_t size)
48 void *ptr;
49 if (size == 0)
50 panic("xzmalloc: zero size\n");
51 ptr = malloc(size);
52 if (ptr == NULL)
53 panic("xzmalloc: out of memory (allocating %zu bytes)\n", size);
54 memset(ptr, 0, size);
55 return ptr;
58 __hidden void *xmalloc_aligned(size_t size, size_t alignment)
60 int ret;
61 void *ptr;
62 if (size == 0)
63 panic("xmalloc_aligned: zero size\n");
64 ret = posix_memalign(&ptr, alignment, size);
65 if (ret != 0)
66 panic("xmalloc_aligned: out of memory (allocating %zu bytes)\n",
67 size);
68 return ptr;
71 __hidden void *xmallocz(size_t size)
73 void *ptr;
74 if (size + 1 < size)
75 panic("xmallocz: data too large to fit "
76 "into virtual memory space\n");
77 ptr = xmalloc(size + 1);
78 ((char *) ptr)[size] = 0;
79 return ptr;
82 __hidden void *xmemdupz(const void *data, size_t len)
84 return memcpy(xmallocz(len), data, len);
87 __hidden void *xrealloc(void *ptr, size_t nmemb, size_t size)
89 void *new_ptr;
90 size_t new_size = nmemb * size;
91 if (new_size == 0)
92 panic("xrealloc: zero size\n");
93 if (SIZE_T_MAX / nmemb < size)
94 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
95 if (ptr == NULL)
96 new_ptr = malloc(new_size);
97 else
98 new_ptr = realloc(ptr, new_size);
99 if (new_ptr == NULL)
100 panic("xrealloc: out of memory (size %zu bytes)\n", new_size);
101 return new_ptr;
104 __hidden void xfree(void *ptr)
106 if (ptr == NULL)
107 panic("xfree: NULL pointer given as argument\n");
108 free(ptr);
111 __hidden char *xstrdup(const char *str)
113 size_t len;
114 char *cp;
115 len = strlen(str) + 1;
116 cp = xmalloc(len);
117 strlcpy(cp, str, len);
118 return cp;
121 __hidden char *xstrndup(const char *str, size_t size)
123 size_t len;
124 char *cp;
125 len = strlen(str) + 1;
126 if (size < len)
127 len = size;
128 cp = xmalloc(len);
129 strlcpy(cp, str, len);
130 return cp;
133 __hidden int xdup(int fd)
135 int ret = dup(fd);
136 if (ret < 0)
137 panic("xdup: dup failed\n");
138 return ret;