3 * By Daniel Borkmann <daniel@netyack.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
16 # define SIZE_T_MAX ((size_t) ~0)
24 __hidden
void *xmalloc(size_t size
)
28 panic("xmalloc: zero size\n");
31 panic("xmalloc: out of memory (allocating %zu bytes)\n", size
);
35 __hidden
void *xvalloc(size_t size
)
39 panic("xmalloc: zero size\n");
42 panic("xvalloc: out of memory (allocating %zu bytes)\n", size
);
46 __hidden
void *xzmalloc(size_t size
)
50 panic("xzmalloc: zero size\n");
53 panic("xzmalloc: out of memory (allocating %zu bytes)\n", size
);
58 __hidden
void *xmalloc_aligned(size_t size
, size_t alignment
)
63 panic("xmalloc_aligned: zero size\n");
64 ret
= posix_memalign(&ptr
, alignment
, size
);
66 panic("xmalloc_aligned: out of memory (allocating %zu bytes)\n",
71 __hidden
void *xmallocz(size_t 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;
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
)
90 size_t new_size
= nmemb
* size
;
92 panic("xrealloc: zero size\n");
93 if (SIZE_T_MAX
/ nmemb
< size
)
94 panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
96 new_ptr
= malloc(new_size
);
98 new_ptr
= realloc(ptr
, new_size
);
100 panic("xrealloc: out of memory (size %zu bytes)\n", new_size
);
104 __hidden
void xfree(void *ptr
)
107 panic("xfree: NULL pointer given as argument\n");
111 __hidden
char *xstrdup(const char *str
)
115 len
= strlen(str
) + 1;
117 strlcpy(cp
, str
, len
);
121 __hidden
char *xstrndup(const char *str
, size_t size
)
125 len
= strlen(str
) + 1;
129 strlcpy(cp
, str
, len
);
133 __hidden
int xdup(int fd
)
137 panic("xdup: dup failed\n");