alloc.c revision 1.1.1.3 1 1.1 christos /* Shared allocation functions for GDB, the GNU debugger.
2 1.1 christos
3 1.1.1.3 christos Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos /* This file is unusual.
21 1.1 christos
22 1.1 christos Because both libiberty and readline define xmalloc and friends, the
23 1.1 christos functions in this file can't appear in a library -- that will cause
24 1.1 christos link errors.
25 1.1 christos
26 1.1 christos And, because we want to turn the common code into a library, this
27 1.1 christos file can't live there.
28 1.1 christos
29 1.1 christos So, it lives in gdb and is built separately by gdb and gdbserver.
30 1.1.1.3 christos Please be aware of this when modifying it. */
31 1.1 christos
32 1.1 christos
33 1.1 christos #include "libiberty.h"
34 1.1 christos #include "gdbsupport/errors.h"
35 1.1 christos
36 1.1 christos /* The xmalloc() (libiberty.h) family of memory management routines.
37 1.1 christos
38 1.1 christos These are like the ISO-C malloc() family except that they implement
39 1.1 christos consistent semantics and guard against typical memory management
40 1.1 christos problems. */
41 1.1 christos
42 1.1.1.2 christos void *
43 1.1 christos xmalloc (size_t size)
44 1.1 christos {
45 1.1 christos void *val;
46 1.1 christos
47 1.1 christos /* See libiberty/xmalloc.c. This function need's to match that's
48 1.1 christos semantics. It never returns NULL. */
49 1.1 christos if (size == 0)
50 1.1 christos size = 1;
51 1.1 christos
52 1.1 christos val = malloc (size); /* ARI: malloc */
53 1.1 christos if (val == NULL)
54 1.1 christos malloc_failure (size);
55 1.1 christos
56 1.1 christos return val;
57 1.1 christos }
58 1.1 christos
59 1.1.1.2 christos void *
60 1.1.1.2 christos xrealloc (void *ptr, size_t size)
61 1.1 christos {
62 1.1 christos void *val;
63 1.1 christos
64 1.1 christos /* See libiberty/xmalloc.c. This function need's to match that's
65 1.1 christos semantics. It never returns NULL. */
66 1.1 christos if (size == 0)
67 1.1 christos size = 1;
68 1.1 christos
69 1.1 christos if (ptr != NULL)
70 1.1 christos val = realloc (ptr, size); /* ARI: realloc */
71 1.1 christos else
72 1.1 christos val = malloc (size); /* ARI: malloc */
73 1.1 christos if (val == NULL)
74 1.1 christos malloc_failure (size);
75 1.1 christos
76 1.1 christos return val;
77 1.1 christos }
78 1.1 christos
79 1.1.1.2 christos void *
80 1.1 christos xcalloc (size_t number, size_t size)
81 1.1 christos {
82 1.1 christos void *mem;
83 1.1 christos
84 1.1 christos /* See libiberty/xmalloc.c. This function need's to match that's
85 1.1 christos semantics. It never returns NULL. */
86 1.1 christos if (number == 0 || size == 0)
87 1.1 christos {
88 1.1 christos number = 1;
89 1.1 christos size = 1;
90 1.1 christos }
91 1.1 christos
92 1.1 christos mem = calloc (number, size); /* ARI: xcalloc */
93 1.1 christos if (mem == NULL)
94 1.1 christos malloc_failure (number * size);
95 1.1 christos
96 1.1 christos return mem;
97 1.1 christos }
98 1.1 christos
99 1.1 christos void
100 1.1 christos xmalloc_failed (size_t size)
101 1.1 christos {
102 1.1 christos malloc_failure (size);
103 1.1 christos }
104