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