xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/realloc.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* realloc() function that is glibc compatible.
28dffb485Schristos 
3*4b169a6bSchristos    Copyright (C) 1997, 2003-2004, 2006-2007, 2009-2022 Free Software
48dffb485Schristos    Foundation, Inc.
58dffb485Schristos 
6*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
7*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
8*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
9*4b169a6bSchristos    License, or (at your option) any later version.
108dffb485Schristos 
11*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
128dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*4b169a6bSchristos    GNU Lesser General Public License for more details.
158dffb485Schristos 
16*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
178dffb485Schristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
188dffb485Schristos 
198dffb485Schristos /* written by Jim Meyering and Bruno Haible */
208dffb485Schristos 
218dffb485Schristos #include <config.h>
228dffb485Schristos 
238dffb485Schristos #include <stdlib.h>
248dffb485Schristos 
258dffb485Schristos #include <errno.h>
268dffb485Schristos 
27*4b169a6bSchristos #include "xalloc-oversized.h"
28*4b169a6bSchristos 
29*4b169a6bSchristos /* Call the system's realloc below.  This file does not define
30*4b169a6bSchristos    _GL_USE_STDLIB_ALLOC because it needs Gnulib's malloc if present.  */
31*4b169a6bSchristos #undef realloc
32*4b169a6bSchristos 
338dffb485Schristos /* Change the size of an allocated block of memory P to N bytes,
34*4b169a6bSchristos    with error checking.  If P is NULL, use malloc.  Otherwise if N is zero,
35*4b169a6bSchristos    free P and return NULL.  */
368dffb485Schristos 
378dffb485Schristos void *
rpl_realloc(void * p,size_t n)388dffb485Schristos rpl_realloc (void *p, size_t n)
398dffb485Schristos {
408dffb485Schristos   if (p == NULL)
41*4b169a6bSchristos     return malloc (n);
428dffb485Schristos 
43*4b169a6bSchristos   if (n == 0)
44*4b169a6bSchristos     {
45*4b169a6bSchristos       free (p);
46*4b169a6bSchristos       return NULL;
47*4b169a6bSchristos     }
48*4b169a6bSchristos 
49*4b169a6bSchristos   if (xalloc_oversized (n, 1))
50*4b169a6bSchristos     {
51*4b169a6bSchristos       errno = ENOMEM;
52*4b169a6bSchristos       return NULL;
53*4b169a6bSchristos     }
54*4b169a6bSchristos 
55*4b169a6bSchristos   void *result = realloc (p, n);
56*4b169a6bSchristos 
57*4b169a6bSchristos #if !HAVE_MALLOC_POSIX
588dffb485Schristos   if (result == NULL)
598dffb485Schristos     errno = ENOMEM;
608dffb485Schristos #endif
618dffb485Schristos 
628dffb485Schristos   return result;
638dffb485Schristos }
64