1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010, 2011 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "gdb_assert.h"
33 /* The xmalloc() (libiberty.h) family of memory management routines.
35 These are like the ISO-C malloc() family except that they implement
36 consistent semantics and guard against typical memory management
39 /* NOTE: These are declared using PTR to ensure consistency with
40 "libiberty.h". xfree() is GDB local. */
47 /* See libiberty/xmalloc.c. This function need's to match that's
48 semantics. It never returns NULL. */
52 val = malloc (size); /* ARI: malloc */
54 malloc_failure (size);
60 xrealloc (PTR ptr, size_t size) /* ARI: PTR */
64 /* See libiberty/xmalloc.c. This function need's to match that's
65 semantics. It never returns NULL. */
70 val = realloc (ptr, size); /* ARI: realloc */
72 val = malloc (size); /* ARI: malloc */
74 malloc_failure (size);
80 xcalloc (size_t number, size_t size)
84 /* See libiberty/xmalloc.c. This function need's to match that's
85 semantics. It never returns NULL. */
86 if (number == 0 || size == 0)
92 mem = calloc (number, size); /* ARI: xcalloc */
94 malloc_failure (number * size);
100 xzalloc (size_t size)
102 return xcalloc (1, size);
109 free (ptr); /* ARI: free */
112 /* Like asprintf/vasprintf but get an internal_error if the call
116 xstrprintf (const char *format, ...)
121 va_start (args, format);
122 ret = xstrvprintf (format, args);
128 xstrvprintf (const char *format, va_list ap)
131 int status = vasprintf (&ret, format, ap);
133 /* NULL is returned when there was a memory allocation problem, or
134 any other error (for instance, a bad format string). A negative
135 status (the printed length) with a non-NULL buffer should never
136 happen, but just to be sure. */
137 if (ret == NULL || status < 0)
138 internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
143 xasprintf (char **ret, const char *format, ...)
147 va_start (args, format);
148 (*ret) = xstrvprintf (format, args);
153 xvasprintf (char **ret, const char *format, va_list ap)
155 (*ret) = xstrvprintf (format, ap);
159 xsnprintf (char *str, size_t size, const char *format, ...)
164 va_start (args, format);
165 ret = vsnprintf (str, size, format, args);
166 gdb_assert (ret < size);