rm_/lib/realloc.c

64 lines
1.6 KiB
C
Raw Permalink Normal View History

2020-09-02 16:47:03 +08:00
/* realloc() function that is glibc compatible.
2022-07-28 14:16:50 +08:00
Copyright (C) 1997, 2003-2004, 2006-2007, 2009-2022 Free Software
2020-09-02 16:47:03 +08:00
Foundation, Inc.
2022-07-28 14:16:50 +08:00
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
2020-09-02 16:47:03 +08:00
2022-07-28 14:16:50 +08:00
This file is distributed in the hope that it will be useful,
2020-09-02 16:47:03 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2022-07-28 14:16:50 +08:00
GNU Lesser General Public License for more details.
2020-09-02 16:47:03 +08:00
2022-07-28 14:16:50 +08:00
You should have received a copy of the GNU Lesser General Public License
2020-09-02 16:47:03 +08:00
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* written by Jim Meyering and Bruno Haible */
#include <config.h>
#include <stdlib.h>
#include <errno.h>
2022-07-28 14:16:50 +08:00
#include "xalloc-oversized.h"
/* Call the system's realloc below. This file does not define
_GL_USE_STDLIB_ALLOC because it needs Gnulib's malloc if present. */
#undef realloc
2020-09-02 16:47:03 +08:00
/* Change the size of an allocated block of memory P to N bytes,
2022-07-28 14:16:50 +08:00
with error checking. If P is NULL, use malloc. Otherwise if N is zero,
free P and return NULL. */
2020-09-02 16:47:03 +08:00
void *
rpl_realloc (void *p, size_t n)
{
2022-07-28 14:16:50 +08:00
if (p == NULL)
return malloc (n);
2020-09-02 16:47:03 +08:00
if (n == 0)
{
free (p);
2022-07-28 14:16:50 +08:00
return NULL;
2020-09-02 16:47:03 +08:00
}
2022-07-28 14:16:50 +08:00
if (xalloc_oversized (n, 1))
2020-09-02 16:47:03 +08:00
{
2022-07-28 14:16:50 +08:00
errno = ENOMEM;
return NULL;
2020-09-02 16:47:03 +08:00
}
2022-07-28 14:16:50 +08:00
void *result = realloc (p, n);
#if !HAVE_MALLOC_POSIX
2020-09-02 16:47:03 +08:00
if (result == NULL)
errno = ENOMEM;
#endif
return result;
}