1/* Shared allocation functions for GDB, the GNU debugger. 2 3 Copyright (C) 1986-2020 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/* This file is unusual. 21 22 Because both libiberty and readline define xmalloc and friends, the 23 functions in this file can't appear in a library -- that will cause 24 link errors. 25 26 And, because we want to turn the common code into a library, this 27 file can't live there. 28 29 So, it lives in gdb and is built separately by gdb and gdbserver. 30 Please be aware of this when modifying it. 31 32 This also explains why this file includes common-defs.h and not 33 defs.h or server.h -- we'd prefer to avoid depending on the 34 GDBSERVER define when possible, and for this file it seemed 35 simple to do so. */ 36 37#include "gdbsupport/common-defs.h" 38#include "libiberty.h" 39#include "gdbsupport/errors.h" 40 41/* The xmalloc() (libiberty.h) family of memory management routines. 42 43 These are like the ISO-C malloc() family except that they implement 44 consistent semantics and guard against typical memory management 45 problems. */ 46 47/* NOTE: These are declared using PTR to ensure consistency with 48 "libiberty.h". xfree() is GDB local. */ 49 50PTR /* ARI: PTR */ 51xmalloc (size_t size) 52{ 53 void *val; 54 55 /* See libiberty/xmalloc.c. This function need's to match that's 56 semantics. It never returns NULL. */ 57 if (size == 0) 58 size = 1; 59 60 val = malloc (size); /* ARI: malloc */ 61 if (val == NULL) 62 malloc_failure (size); 63 64 return val; 65} 66 67PTR /* ARI: PTR */ 68xrealloc (PTR ptr, size_t size) /* ARI: PTR */ 69{ 70 void *val; 71 72 /* See libiberty/xmalloc.c. This function need's to match that's 73 semantics. It never returns NULL. */ 74 if (size == 0) 75 size = 1; 76 77 if (ptr != NULL) 78 val = realloc (ptr, size); /* ARI: realloc */ 79 else 80 val = malloc (size); /* ARI: malloc */ 81 if (val == NULL) 82 malloc_failure (size); 83 84 return val; 85} 86 87PTR /* ARI: PTR */ 88xcalloc (size_t number, size_t size) 89{ 90 void *mem; 91 92 /* See libiberty/xmalloc.c. This function need's to match that's 93 semantics. It never returns NULL. */ 94 if (number == 0 || size == 0) 95 { 96 number = 1; 97 size = 1; 98 } 99 100 mem = calloc (number, size); /* ARI: xcalloc */ 101 if (mem == NULL) 102 malloc_failure (number * size); 103 104 return mem; 105} 106 107void 108xmalloc_failed (size_t size) 109{ 110 malloc_failure (size); 111} 112