1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
21 #include "host-defs.h"
24 /* The xmalloc() (libiberty.h) family of memory management routines.
26 These are like the ISO-C malloc() family except that they implement
27 consistent semantics and guard against typical memory management
30 /* NOTE: These are declared using PTR to ensure consistency with
31 "libiberty.h". xfree() is GDB local. */
38 /* See libiberty/xmalloc.c. This function need's to match that's
39 semantics. It never returns NULL. */
43 val = malloc (size); /* ARI: malloc */
45 malloc_failure (size);
51 xrealloc (PTR ptr, size_t size) /* ARI: PTR */
55 /* See libiberty/xmalloc.c. This function need's to match that's
56 semantics. It never returns NULL. */
61 val = realloc (ptr, size); /* ARI: realloc */
63 val = malloc (size); /* ARI: malloc */
65 malloc_failure (size);
71 xcalloc (size_t number, size_t size)
75 /* See libiberty/xmalloc.c. This function need's to match that's
76 semantics. It never returns NULL. */
77 if (number == 0 || size == 0)
83 mem = calloc (number, size); /* ARI: xcalloc */
85 malloc_failure (number * size);
93 return xcalloc (1, size);
100 free (ptr); /* ARI: free */
103 /* Like asprintf/vasprintf but get an internal_error if the call
107 xstrprintf (const char *format, ...)
112 va_start (args, format);
113 ret = xstrvprintf (format, args);
119 xstrvprintf (const char *format, va_list ap)
122 int status = vasprintf (&ret, format, ap);
124 /* NULL is returned when there was a memory allocation problem, or
125 any other error (for instance, a bad format string). A negative
126 status (the printed length) with a non-NULL buffer should never
127 happen, but just to be sure. */
128 if (ret == NULL || status < 0)
129 internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
134 xsnprintf (char *str, size_t size, const char *format, ...)
139 va_start (args, format);
140 ret = vsnprintf (str, size, format, args);
141 gdb_assert (ret < size);
148 savestring (const char *ptr, size_t len)
150 char *p = (char *) xmalloc (len + 1);
152 memcpy (p, ptr, len);
157 /* The bit offset of the highest byte in a ULONGEST, for overflow
160 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
162 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
163 where 2 <= BASE <= 36. */
166 is_digit_in_base (unsigned char digit, int base)
168 if (!isalnum (digit))
171 return (isdigit (digit) && digit < base + '0');
173 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
177 digit_to_int (unsigned char c)
182 return tolower (c) - 'a' + 10;
185 /* As for strtoul, but for ULONGEST results. */
188 strtoulst (const char *num, const char **trailer, int base)
190 unsigned int high_part;
195 /* Skip leading whitespace. */
196 while (isspace (num[i]))
199 /* Handle prefixes. */
202 else if (num[i] == '-')
208 if (base == 0 || base == 16)
210 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
218 if (base == 0 && num[i] == '0')
224 if (base < 2 || base > 36)
230 result = high_part = 0;
231 for (; is_digit_in_base (num[i], base); i += 1)
233 result = result * base + digit_to_int (num[i]);
234 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
235 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
236 if (high_part > 0xff)
239 result = ~ (ULONGEST) 0;
249 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
256 /* See documentation in cli-utils.h. */
259 skip_spaces (char *chp)
263 while (*chp && isspace (*chp))
268 /* A const-correct version of the above. */
271 skip_spaces_const (const char *chp)
275 while (*chp && isspace (*chp))
280 /* See documentation in cli-utils.h. */
283 skip_to_space_const (const char *chp)
287 while (*chp && !isspace (*chp))