2 * malloc routines with failure handling.
6 * extern xmalloc_handler_t memory_error;
7 * extern const char *string;
11 * xmalloc_error_handler = memory_error;
12 * buffer = xmalloc(1024);
13 * xrealloc(buffer, 2048);
15 * buffer = xcalloc(1024);
17 * buffer = xstrdup(string);
19 * buffer = xstrndup(string, 25);
21 * xasprintf(&buffer, "%s", "some string");
23 * xvasprintf(&buffer, "%s", args);
25 * xmalloc, xcalloc, xrealloc, and xstrdup behave exactly like their C library
26 * counterparts without the leading x except that they will never return NULL.
27 * Instead, on error, they call xmalloc_error_handler, passing it the name of
28 * the function whose memory allocation failed, the amount of the allocation,
29 * and the file and line number where the allocation function was invoked
30 * (from __FILE__ and __LINE__). This function may do whatever it wishes,
31 * such as some action to free up memory or a call to sleep to hope that
32 * system resources return. If the handler returns, the interrupted memory
33 * allocation function will try its allocation again (calling the handler
34 * again if it still fails).
36 * xstrndup behaves like xstrdup but only copies the given number of
37 * characters. It allocates an additional byte over its second argument and
38 * always nul-terminates the string.
40 * xasprintf and xvasprintf behave just like their GNU glibc library
41 * implementations except that they do the same checking as described above.
42 * xasprintf will only be able to provide accurate file and line information
43 * on systems that support variadic macros.
45 * The default error handler, if none is set by the caller, prints an error
46 * message to stderr and exits with exit status 1. An error handler must take
47 * a const char * (function name), size_t (bytes allocated), const char *
48 * (file), and int (line).
50 * xmalloc will return a pointer to a valid memory region on an xmalloc of 0
51 * bytes, ensuring this by allocating space for one character instead of 0
54 * The functions defined here are actually x_malloc, x_realloc, etc. The
55 * header file defines macros named xmalloc, etc. that pass the file name and
56 * line number to these functions.
58 * The canonical version of this file is maintained in the rra-c-util package,
59 * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
62 * The Board of Trustees of the Leland Stanford Junior University
63 * Copyright (c) 2004, 2005, 2006
64 * by Internet Systems Consortium, Inc. ("ISC")
65 * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
66 * 2002, 2003 by The Internet Software Consortium and Rich Salz
68 * This code is derived from software contributed to the Internet Software
69 * Consortium by Rich Salz.
71 * Permission to use, copy, modify, and distribute this software for any
72 * purpose with or without fee is hereby granted, provided that the above
73 * copyright notice and this permission notice appear in all copies.
75 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
76 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
77 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
78 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
79 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
80 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
81 * PERFORMANCE OF THIS SOFTWARE.
85 #include <portable/system.h>
87 #include <util/messages.h>
88 #include <util/xmalloc.h>
92 * The default error handler.
95 xmalloc_fail(const char *function, size_t size, const char *file, int line)
97 sysdie("failed to %s %lu bytes at %s line %d", function,
98 (unsigned long) size, file, line);
101 /* Assign to this variable to choose a handler other than the default. */
102 xmalloc_handler_type xmalloc_error_handler = xmalloc_fail;
106 x_malloc(size_t size, const char *file, int line)
111 real_size = (size > 0) ? size : 1;
112 p = malloc(real_size);
114 (*xmalloc_error_handler)("malloc", size, file, line);
115 p = malloc(real_size);
122 x_calloc(size_t n, size_t size, const char *file, int line)
127 size = (size > 0) ? size : 1;
130 (*xmalloc_error_handler)("calloc", n * size, file, line);
138 x_realloc(void *p, size_t size, const char *file, int line)
142 newp = realloc(p, size);
143 while (newp == NULL && size > 0) {
144 (*xmalloc_error_handler)("realloc", size, file, line);
145 newp = realloc(p, size);
152 x_strdup(const char *s, const char *file, int line)
160 (*xmalloc_error_handler)("strdup", len, file, line);
169 * Avoid using the system strndup function since it may not exist (on Mac OS
170 * X, for example), and there's no need to introduce another portability
174 x_strndup(const char *s, size_t size, const char *file, int line)
180 /* Don't assume that the source string is nul-terminated. */
181 for (p = s; (size_t) (p - s) < size && *p != '\0'; p++)
184 copy = malloc(length + 1);
185 while (copy == NULL) {
186 (*xmalloc_error_handler)("strndup", length + 1, file, line);
187 copy = malloc(length + 1);
189 memcpy(copy, s, length);
196 x_vasprintf(char **strp, const char *fmt, va_list args, const char *file,
202 va_copy(args_copy, args);
203 status = vasprintf(strp, fmt, args_copy);
206 va_copy(args_copy, args);
207 status = vsnprintf(NULL, 0, fmt, args_copy);
209 (*xmalloc_error_handler)("vasprintf", status + 1, file, line);
210 va_copy(args_copy, args);
211 status = vasprintf(strp, fmt, args_copy);
217 #if HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS
219 x_asprintf(char **strp, const char *file, int line, const char *fmt, ...)
221 va_list args, args_copy;
225 va_copy(args_copy, args);
226 status = vasprintf(strp, fmt, args_copy);
229 va_copy(args_copy, args);
230 status = vsnprintf(NULL, 0, fmt, args_copy);
232 (*xmalloc_error_handler)("asprintf", status + 1, file, line);
233 va_copy(args_copy, args);
234 status = vasprintf(strp, fmt, args_copy);
238 #else /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */
240 x_asprintf(char **strp, const char *fmt, ...)
242 va_list args, args_copy;
246 va_copy(args_copy, args);
247 status = vasprintf(strp, fmt, args_copy);
250 va_copy(args_copy, args);
251 status = vsnprintf(NULL, 0, fmt, args_copy);
253 (*xmalloc_error_handler)("asprintf", status + 1, __FILE__, __LINE__);
254 va_copy(args_copy, args);
255 status = vasprintf(strp, fmt, args_copy);
259 #endif /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */