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