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