2011-07-22 Kwok Cheung Yeung <kcy@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / common / common-utils.c
CommitLineData
d26e3629
KY
1/* Shared general utility routines for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010, 2011 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#ifdef GDBSERVER
23#include "server.h"
24#else
25#include "defs.h"
26#endif
27
28#include "gdb_assert.h"
29
30#include <stdlib.h>
31#include <stdio.h>
32
33/* The xmalloc() (libiberty.h) family of memory management routines.
34
35 These are like the ISO-C malloc() family except that they implement
36 consistent semantics and guard against typical memory management
37 problems. */
38
39/* NOTE: These are declared using PTR to ensure consistency with
40 "libiberty.h". xfree() is GDB local. */
41
42PTR /* ARI: PTR */
43xmalloc (size_t size)
44{
45 void *val;
46
47 /* See libiberty/xmalloc.c. This function need's to match that's
48 semantics. It never returns NULL. */
49 if (size == 0)
50 size = 1;
51
52 val = malloc (size); /* ARI: malloc */
53 if (val == NULL)
54 malloc_failure (size);
55
56 return val;
57}
58
59PTR /* ARI: PTR */
60xrealloc (PTR ptr, size_t size) /* ARI: PTR */
61{
62 void *val;
63
64 /* See libiberty/xmalloc.c. This function need's to match that's
65 semantics. It never returns NULL. */
66 if (size == 0)
67 size = 1;
68
69 if (ptr != NULL)
70 val = realloc (ptr, size); /* ARI: realloc */
71 else
72 val = malloc (size); /* ARI: malloc */
73 if (val == NULL)
74 malloc_failure (size);
75
76 return val;
77}
78
79PTR /* ARI: PTR */
80xcalloc (size_t number, size_t size)
81{
82 void *mem;
83
84 /* See libiberty/xmalloc.c. This function need's to match that's
85 semantics. It never returns NULL. */
86 if (number == 0 || size == 0)
87 {
88 number = 1;
89 size = 1;
90 }
91
92 mem = calloc (number, size); /* ARI: xcalloc */
93 if (mem == NULL)
94 malloc_failure (number * size);
95
96 return mem;
97}
98
99void *
100xzalloc (size_t size)
101{
102 return xcalloc (1, size);
103}
104
105void
106xfree (void *ptr)
107{
108 if (ptr != NULL)
109 free (ptr); /* ARI: free */
110}
111
112/* Like asprintf/vasprintf but get an internal_error if the call
113 fails. */
114
115char *
116xstrprintf (const char *format, ...)
117{
118 char *ret;
119 va_list args;
120
121 va_start (args, format);
122 ret = xstrvprintf (format, args);
123 va_end (args);
124 return ret;
125}
126
127char *
128xstrvprintf (const char *format, va_list ap)
129{
130 char *ret = NULL;
131 int status = vasprintf (&ret, format, ap);
132
133 /* NULL is returned when there was a memory allocation problem, or
134 any other error (for instance, a bad format string). A negative
135 status (the printed length) with a non-NULL buffer should never
136 happen, but just to be sure. */
137 if (ret == NULL || status < 0)
138 internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
139 return ret;
140}
141
142void
143xasprintf (char **ret, const char *format, ...)
144{
145 va_list args;
146
147 va_start (args, format);
148 (*ret) = xstrvprintf (format, args);
149 va_end (args);
150}
151
152void
153xvasprintf (char **ret, const char *format, va_list ap)
154{
155 (*ret) = xstrvprintf (format, ap);
156}
157
158int
159xsnprintf (char *str, size_t size, const char *format, ...)
160{
161 va_list args;
162 int ret;
163
164 va_start (args, format);
165 ret = vsnprintf (str, size, format, args);
166 gdb_assert (ret < size);
167 va_end (args);
168
169 return ret;
170}
This page took 0.029074 seconds and 4 git commands to generate.