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