1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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 "common-utils.h"
22 #include "host-defs.h"
25 /* The xmalloc() (libiberty.h) family of memory management routines.
27 These are like the ISO-C malloc() family except that they implement
28 consistent semantics and guard against typical memory management
31 /* NOTE: These are declared using PTR to ensure consistency with
32 "libiberty.h". xfree() is GDB local. */
39 /* See libiberty/xmalloc.c. This function need's to match that's
40 semantics. It never returns NULL. */
44 val = malloc (size); /* ARI: malloc */
46 malloc_failure (size);
52 xrealloc (PTR ptr, size_t size) /* ARI: PTR */
56 /* See libiberty/xmalloc.c. This function need's to match that's
57 semantics. It never returns NULL. */
62 val = realloc (ptr, size); /* ARI: realloc */
64 val = malloc (size); /* ARI: malloc */
66 malloc_failure (size);
72 xcalloc (size_t number, size_t size)
76 /* See libiberty/xmalloc.c. This function need's to match that's
77 semantics. It never returns NULL. */
78 if (number == 0 || size == 0)
84 mem = calloc (number, size); /* ARI: xcalloc */
86 malloc_failure (number * size);
94 return xcalloc (1, size);
98 xmalloc_failed (size_t size)
100 malloc_failure (size);
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);
147 /* See documentation in common-utils.h. */
150 string_printf (const char* fmt, ...)
156 size = vsnprintf (NULL, 0, fmt, vp);
159 std::string str (size, '\0');
161 /* C++11 and later guarantee std::string uses contiguous memory and
162 always includes the terminating '\0'. */
164 vsprintf (&str[0], fmt, vp);
170 /* See documentation in common-utils.h. */
173 string_vprintf (const char* fmt, va_list args)
179 size = vsnprintf (NULL, 0, fmt, vp);
182 std::string str (size, '\0');
184 /* C++11 and later guarantee std::string uses contiguous memory and
185 always includes the terminating '\0'. */
186 vsprintf (&str[0], fmt, args);
192 /* See documentation in common-utils.h. */
195 string_appendf (std::string &str, const char *fmt, ...)
200 string_vappendf (str, fmt, vp);
205 /* See documentation in common-utils.h. */
208 string_vappendf (std::string &str, const char *fmt, va_list args)
214 grow_size = vsnprintf (NULL, 0, fmt, vp);
217 size_t curr_size = str.size ();
218 str.resize (curr_size + grow_size);
220 /* C++11 and later guarantee std::string uses contiguous memory and
221 always includes the terminating '\0'. */
222 vsprintf (&str[curr_size], fmt, args);
226 savestring (const char *ptr, size_t len)
228 char *p = (char *) xmalloc (len + 1);
230 memcpy (p, ptr, len);
235 /* The bit offset of the highest byte in a ULONGEST, for overflow
238 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
240 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
241 where 2 <= BASE <= 36. */
244 is_digit_in_base (unsigned char digit, int base)
246 if (!isalnum (digit))
249 return (isdigit (digit) && digit < base + '0');
251 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
255 digit_to_int (unsigned char c)
260 return tolower (c) - 'a' + 10;
263 /* As for strtoul, but for ULONGEST results. */
266 strtoulst (const char *num, const char **trailer, int base)
268 unsigned int high_part;
273 /* Skip leading whitespace. */
274 while (isspace (num[i]))
277 /* Handle prefixes. */
280 else if (num[i] == '-')
286 if (base == 0 || base == 16)
288 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
296 if (base == 0 && num[i] == '0')
302 if (base < 2 || base > 36)
308 result = high_part = 0;
309 for (; is_digit_in_base (num[i], base); i += 1)
311 result = result * base + digit_to_int (num[i]);
312 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
313 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
314 if (high_part > 0xff)
317 result = ~ (ULONGEST) 0;
327 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
334 /* See documentation in common-utils.h. */
337 skip_spaces (char *chp)
341 while (*chp && isspace (*chp))
346 /* A const-correct version of the above. */
349 skip_spaces (const char *chp)
353 while (*chp && isspace (*chp))
358 /* See documentation in common-utils.h. */
361 skip_to_space (const char *chp)
365 while (*chp && !isspace (*chp))
370 /* See documentation in common-utils.h. */
373 skip_to_space (char *chp)
375 return (char *) skip_to_space ((const char *) chp);
378 /* See common/common-utils.h. */
381 free_vector_argv (std::vector<char *> &v)
389 /* See common/common-utils.h. */
392 stringify_argv (const std::vector<char *> &args)
396 if (!args.empty () && args[0] != NULL)
405 /* Erase the last whitespace. */
406 ret.erase (ret.end () - 1);
412 /* See common/common-utils.h. */
415 align_up (ULONGEST v, int n)
417 /* Check that N is really a power of two. */
418 gdb_assert (n && (n & (n-1)) == 0);
419 return (v + n - 1) & -n;
422 /* See common/common-utils.h. */
425 align_down (ULONGEST v, int n)
427 /* Check that N is really a power of two. */
428 gdb_assert (n && (n & (n-1)) == 0);