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