gdb/common/common-utils.c - gdb

Functions defined

Source code

  1. /* Shared general utility routines for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "common-defs.h"

  15. /* The xmalloc() (libiberty.h) family of memory management routines.

  16.    These are like the ISO-C malloc() family except that they implement
  17.    consistent semantics and guard against typical memory management
  18.    problems.  */

  19. /* NOTE: These are declared using PTR to ensure consistency with
  20.    "libiberty.h".  xfree() is GDB local.  */

  21. PTR                            /* ARI: PTR */
  22. xmalloc (size_t size)
  23. {
  24.   void *val;

  25.   /* See libiberty/xmalloc.c.  This function need's to match that's
  26.      semantics.  It never returns NULL.  */
  27.   if (size == 0)
  28.     size = 1;

  29.   val = malloc (size);         /* ARI: malloc */
  30.   if (val == NULL)
  31.     malloc_failure (size);

  32.   return val;
  33. }

  34. PTR                              /* ARI: PTR */
  35. xrealloc (PTR ptr, size_t size)          /* ARI: PTR */
  36. {
  37.   void *val;

  38.   /* See libiberty/xmalloc.c.  This function need's to match that's
  39.      semantics.  It never returns NULL.  */
  40.   if (size == 0)
  41.     size = 1;

  42.   if (ptr != NULL)
  43.     val = realloc (ptr, size);        /* ARI: realloc */
  44.   else
  45.     val = malloc (size);                /* ARI: malloc */
  46.   if (val == NULL)
  47.     malloc_failure (size);

  48.   return val;
  49. }

  50. PTR                            /* ARI: PTR */
  51. xcalloc (size_t number, size_t size)
  52. {
  53.   void *mem;

  54.   /* See libiberty/xmalloc.c.  This function need's to match that's
  55.      semantics.  It never returns NULL.  */
  56.   if (number == 0 || size == 0)
  57.     {
  58.       number = 1;
  59.       size = 1;
  60.     }

  61.   mem = calloc (number, size);      /* ARI: xcalloc */
  62.   if (mem == NULL)
  63.     malloc_failure (number * size);

  64.   return mem;
  65. }

  66. void *
  67. xzalloc (size_t size)
  68. {
  69.   return xcalloc (1, size);
  70. }

  71. void
  72. xfree (void *ptr)
  73. {
  74.   if (ptr != NULL)
  75.     free (ptr);                /* ARI: free */
  76. }

  77. /* Like asprintf/vasprintf but get an internal_error if the call
  78.    fails. */

  79. char *
  80. xstrprintf (const char *format, ...)
  81. {
  82.   char *ret;
  83.   va_list args;

  84.   va_start (args, format);
  85.   ret = xstrvprintf (format, args);
  86.   va_end (args);
  87.   return ret;
  88. }

  89. char *
  90. xstrvprintf (const char *format, va_list ap)
  91. {
  92.   char *ret = NULL;
  93.   int status = vasprintf (&ret, format, ap);

  94.   /* NULL is returned when there was a memory allocation problem, or
  95.      any other error (for instance, a bad format string).  A negative
  96.      status (the printed length) with a non-NULL buffer should never
  97.      happen, but just to be sure.  */
  98.   if (ret == NULL || status < 0)
  99.     internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
  100.   return ret;
  101. }

  102. int
  103. xsnprintf (char *str, size_t size, const char *format, ...)
  104. {
  105.   va_list args;
  106.   int ret;

  107.   va_start (args, format);
  108.   ret = vsnprintf (str, size, format, args);
  109.   gdb_assert (ret < size);
  110.   va_end (args);

  111.   return ret;
  112. }

  113. char *
  114. savestring (const char *ptr, size_t len)
  115. {
  116.   char *p = (char *) xmalloc (len + 1);

  117.   memcpy (p, ptr, len);
  118.   p[len] = 0;
  119.   return p;
  120. }