GDB (API)
|
00001 /* Shared general utility routines for GDB, the GNU debugger. 00002 00003 Copyright (C) 1986-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #ifdef GDBSERVER 00021 #include "server.h" 00022 #else 00023 #include "defs.h" 00024 #endif 00025 00026 #include "gdb_assert.h" 00027 #include "gdb_string.h" 00028 00029 #include <stdlib.h> 00030 #include <stdio.h> 00031 00032 /* The xmalloc() (libiberty.h) family of memory management routines. 00033 00034 These are like the ISO-C malloc() family except that they implement 00035 consistent semantics and guard against typical memory management 00036 problems. */ 00037 00038 /* NOTE: These are declared using PTR to ensure consistency with 00039 "libiberty.h". xfree() is GDB local. */ 00040 00041 PTR /* ARI: PTR */ 00042 xmalloc (size_t size) 00043 { 00044 void *val; 00045 00046 /* See libiberty/xmalloc.c. This function need's to match that's 00047 semantics. It never returns NULL. */ 00048 if (size == 0) 00049 size = 1; 00050 00051 val = malloc (size); /* ARI: malloc */ 00052 if (val == NULL) 00053 malloc_failure (size); 00054 00055 return val; 00056 } 00057 00058 PTR /* ARI: PTR */ 00059 xrealloc (PTR ptr, size_t size) /* ARI: PTR */ 00060 { 00061 void *val; 00062 00063 /* See libiberty/xmalloc.c. This function need's to match that's 00064 semantics. It never returns NULL. */ 00065 if (size == 0) 00066 size = 1; 00067 00068 if (ptr != NULL) 00069 val = realloc (ptr, size); /* ARI: realloc */ 00070 else 00071 val = malloc (size); /* ARI: malloc */ 00072 if (val == NULL) 00073 malloc_failure (size); 00074 00075 return val; 00076 } 00077 00078 PTR /* ARI: PTR */ 00079 xcalloc (size_t number, size_t size) 00080 { 00081 void *mem; 00082 00083 /* See libiberty/xmalloc.c. This function need's to match that's 00084 semantics. It never returns NULL. */ 00085 if (number == 0 || size == 0) 00086 { 00087 number = 1; 00088 size = 1; 00089 } 00090 00091 mem = calloc (number, size); /* ARI: xcalloc */ 00092 if (mem == NULL) 00093 malloc_failure (number * size); 00094 00095 return mem; 00096 } 00097 00098 void * 00099 xzalloc (size_t size) 00100 { 00101 return xcalloc (1, size); 00102 } 00103 00104 void 00105 xfree (void *ptr) 00106 { 00107 if (ptr != NULL) 00108 free (ptr); /* ARI: free */ 00109 } 00110 00111 /* Like asprintf/vasprintf but get an internal_error if the call 00112 fails. */ 00113 00114 char * 00115 xstrprintf (const char *format, ...) 00116 { 00117 char *ret; 00118 va_list args; 00119 00120 va_start (args, format); 00121 ret = xstrvprintf (format, args); 00122 va_end (args); 00123 return ret; 00124 } 00125 00126 char * 00127 xstrvprintf (const char *format, va_list ap) 00128 { 00129 char *ret = NULL; 00130 int status = vasprintf (&ret, format, ap); 00131 00132 /* NULL is returned when there was a memory allocation problem, or 00133 any other error (for instance, a bad format string). A negative 00134 status (the printed length) with a non-NULL buffer should never 00135 happen, but just to be sure. */ 00136 if (ret == NULL || status < 0) 00137 internal_error (__FILE__, __LINE__, _("vasprintf call failed")); 00138 return ret; 00139 } 00140 00141 int 00142 xsnprintf (char *str, size_t size, const char *format, ...) 00143 { 00144 va_list args; 00145 int ret; 00146 00147 va_start (args, format); 00148 ret = vsnprintf (str, size, format, args); 00149 gdb_assert (ret < size); 00150 va_end (args); 00151 00152 return ret; 00153 } 00154 00155 char * 00156 savestring (const char *ptr, size_t len) 00157 { 00158 char *p = (char *) xmalloc (len + 1); 00159 00160 memcpy (p, ptr, len); 00161 p[len] = 0; 00162 return p; 00163 }