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