Include string.h in common-defs.h
[external/binutils.git] / gdb / common / common-utils.c
1 /* Shared general utility routines for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #ifdef GDBSERVER
21 #include "server.h"
22 #else
23 #include "defs.h"
24 #endif
25
26 /* The xmalloc() (libiberty.h) family of memory management routines.
27
28    These are like the ISO-C malloc() family except that they implement
29    consistent semantics and guard against typical memory management
30    problems.  */
31
32 /* NOTE: These are declared using PTR to ensure consistency with
33    "libiberty.h".  xfree() is GDB local.  */
34
35 PTR                            /* ARI: PTR */
36 xmalloc (size_t size)
37 {
38   void *val;
39
40   /* See libiberty/xmalloc.c.  This function need's to match that's
41      semantics.  It never returns NULL.  */
42   if (size == 0)
43     size = 1;
44
45   val = malloc (size);         /* ARI: malloc */
46   if (val == NULL)
47     malloc_failure (size);
48
49   return val;
50 }
51
52 PTR                              /* ARI: PTR */
53 xrealloc (PTR ptr, size_t size)          /* ARI: PTR */
54 {
55   void *val;
56
57   /* See libiberty/xmalloc.c.  This function need's to match that's
58      semantics.  It never returns NULL.  */
59   if (size == 0)
60     size = 1;
61
62   if (ptr != NULL)
63     val = realloc (ptr, size);  /* ARI: realloc */
64   else
65     val = malloc (size);                /* ARI: malloc */
66   if (val == NULL)
67     malloc_failure (size);
68
69   return val;
70 }
71
72 PTR                            /* ARI: PTR */           
73 xcalloc (size_t number, size_t size)
74 {
75   void *mem;
76
77   /* See libiberty/xmalloc.c.  This function need's to match that's
78      semantics.  It never returns NULL.  */
79   if (number == 0 || size == 0)
80     {
81       number = 1;
82       size = 1;
83     }
84
85   mem = calloc (number, size);      /* ARI: xcalloc */
86   if (mem == NULL)
87     malloc_failure (number * size);
88
89   return mem;
90 }
91
92 void *
93 xzalloc (size_t size)
94 {
95   return xcalloc (1, size);
96 }
97
98 void
99 xfree (void *ptr)
100 {
101   if (ptr != NULL)
102     free (ptr);         /* ARI: free */
103 }
104
105 /* Like asprintf/vasprintf but get an internal_error if the call
106    fails. */
107
108 char *
109 xstrprintf (const char *format, ...)
110 {
111   char *ret;
112   va_list args;
113
114   va_start (args, format);
115   ret = xstrvprintf (format, args);
116   va_end (args);
117   return ret;
118 }
119
120 char *
121 xstrvprintf (const char *format, va_list ap)
122 {
123   char *ret = NULL;
124   int status = vasprintf (&ret, format, ap);
125
126   /* NULL is returned when there was a memory allocation problem, or
127      any other error (for instance, a bad format string).  A negative
128      status (the printed length) with a non-NULL buffer should never
129      happen, but just to be sure.  */
130   if (ret == NULL || status < 0)
131     internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
132   return ret;
133 }
134
135 int
136 xsnprintf (char *str, size_t size, const char *format, ...)
137 {
138   va_list args;
139   int ret;
140
141   va_start (args, format);
142   ret = vsnprintf (str, size, format, args);
143   gdb_assert (ret < size);
144   va_end (args);
145
146   return ret;
147 }
148
149 char *
150 savestring (const char *ptr, size_t len)
151 {
152   char *p = (char *) xmalloc (len + 1);
153
154   memcpy (p, ptr, len);
155   p[len] = 0;
156   return p;
157 }