xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/alloc.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Shared allocation functions for GDB, the GNU debugger.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 1986-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos /* This file is unusual.
217d62b00eSchristos 
227d62b00eSchristos    Because both libiberty and readline define xmalloc and friends, the
237d62b00eSchristos    functions in this file can't appear in a library -- that will cause
247d62b00eSchristos    link errors.
257d62b00eSchristos 
267d62b00eSchristos    And, because we want to turn the common code into a library, this
277d62b00eSchristos    file can't live there.
287d62b00eSchristos 
297d62b00eSchristos    So, it lives in gdb and is built separately by gdb and gdbserver.
307d62b00eSchristos    Please be aware of this when modifying it.
317d62b00eSchristos 
327d62b00eSchristos    This also explains why this file includes common-defs.h and not
337d62b00eSchristos    defs.h or server.h -- we'd prefer to avoid depending on the
347d62b00eSchristos    GDBSERVER define when possible, and for this file it seemed
357d62b00eSchristos    simple to do so.  */
367d62b00eSchristos 
377d62b00eSchristos #include "gdbsupport/common-defs.h"
387d62b00eSchristos #include "libiberty.h"
397d62b00eSchristos #include "gdbsupport/errors.h"
407d62b00eSchristos 
417d62b00eSchristos /* The xmalloc() (libiberty.h) family of memory management routines.
427d62b00eSchristos 
437d62b00eSchristos    These are like the ISO-C malloc() family except that they implement
447d62b00eSchristos    consistent semantics and guard against typical memory management
457d62b00eSchristos    problems.  */
467d62b00eSchristos 
47*6881a400Schristos void *
487d62b00eSchristos xmalloc (size_t size)
497d62b00eSchristos {
507d62b00eSchristos   void *val;
517d62b00eSchristos 
527d62b00eSchristos   /* See libiberty/xmalloc.c.  This function need's to match that's
537d62b00eSchristos      semantics.  It never returns NULL.  */
547d62b00eSchristos   if (size == 0)
557d62b00eSchristos     size = 1;
567d62b00eSchristos 
577d62b00eSchristos   val = malloc (size);         /* ARI: malloc */
587d62b00eSchristos   if (val == NULL)
597d62b00eSchristos     malloc_failure (size);
607d62b00eSchristos 
617d62b00eSchristos   return val;
627d62b00eSchristos }
637d62b00eSchristos 
64*6881a400Schristos void *
65*6881a400Schristos xrealloc (void *ptr, size_t size)
667d62b00eSchristos {
677d62b00eSchristos   void *val;
687d62b00eSchristos 
697d62b00eSchristos   /* See libiberty/xmalloc.c.  This function need's to match that's
707d62b00eSchristos      semantics.  It never returns NULL.  */
717d62b00eSchristos   if (size == 0)
727d62b00eSchristos     size = 1;
737d62b00eSchristos 
747d62b00eSchristos   if (ptr != NULL)
757d62b00eSchristos     val = realloc (ptr, size);	/* ARI: realloc */
767d62b00eSchristos   else
777d62b00eSchristos     val = malloc (size);	        /* ARI: malloc */
787d62b00eSchristos   if (val == NULL)
797d62b00eSchristos     malloc_failure (size);
807d62b00eSchristos 
817d62b00eSchristos   return val;
827d62b00eSchristos }
837d62b00eSchristos 
84*6881a400Schristos void *
857d62b00eSchristos xcalloc (size_t number, size_t size)
867d62b00eSchristos {
877d62b00eSchristos   void *mem;
887d62b00eSchristos 
897d62b00eSchristos   /* See libiberty/xmalloc.c.  This function need's to match that's
907d62b00eSchristos      semantics.  It never returns NULL.  */
917d62b00eSchristos   if (number == 0 || size == 0)
927d62b00eSchristos     {
937d62b00eSchristos       number = 1;
947d62b00eSchristos       size = 1;
957d62b00eSchristos     }
967d62b00eSchristos 
977d62b00eSchristos   mem = calloc (number, size);      /* ARI: xcalloc */
987d62b00eSchristos   if (mem == NULL)
997d62b00eSchristos     malloc_failure (number * size);
1007d62b00eSchristos 
1017d62b00eSchristos   return mem;
1027d62b00eSchristos }
1037d62b00eSchristos 
1047d62b00eSchristos void
1057d62b00eSchristos xmalloc_failed (size_t size)
1067d62b00eSchristos {
1077d62b00eSchristos   malloc_failure (size);
1087d62b00eSchristos }
109