xref: /netbsd-src/external/gpl3/gdb/dist/gdb/alloc.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Shared allocation functions for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2024 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 
33 #include "libiberty.h"
34 #include "gdbsupport/errors.h"
35 
36 /* The xmalloc() (libiberty.h) family of memory management routines.
37 
38    These are like the ISO-C malloc() family except that they implement
39    consistent semantics and guard against typical memory management
40    problems.  */
41 
42 void *
43 xmalloc (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 
59 void *
60 xrealloc (void *ptr, size_t size)
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 
79 void *
80 xcalloc (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 
99 void
100 xmalloc_failed (size_t size)
101 {
102   malloc_failure (size);
103 }
104