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