1cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* xmalloc.c -- malloc with out of memory checking
2cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 1990-2000, 2002-2006, 2008-2012 Free Software Foundation, Inc.
4cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is free software: you can redistribute it and/or modify
6cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   it under the terms of the GNU General Public License as published by
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation; either version 3 of the License, or
805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   (at your option) any later version.
9cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
10cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   This program is distributed in the hope that it will be useful,
11cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   but WITHOUT ANY WARRANTY; without even the implied warranty of
12cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   GNU General Public License for more details.
14cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
15cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   You should have received a copy of the GNU General Public License
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <config.h>
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define XALLOC_INLINE _GL_EXTERN_INLINE
21cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
22cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include "xalloc.h"
23cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
24cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include <stdlib.h>
25cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#include <string.h>
26cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
27cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* 1 if calloc is known to be compatible with GNU calloc.  This
28cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   matters if we are not also using the calloc module, which defines
2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   HAVE_CALLOC_GNU and supports the GNU API even on non-GNU platforms.  */
3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#if defined HAVE_CALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__)
31cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectenum { HAVE_GNU_CALLOC = 1 };
32cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#else
33cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectenum { HAVE_GNU_CALLOC = 0 };
34cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project#endif
35cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
36cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Allocate N bytes of memory dynamically, with error checking.  */
37cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
38cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
39cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectxmalloc (size_t n)
40cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang  void *p = malloc (n);
4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang  if (!p && n != 0)
43cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    xalloc_die ();
44cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return p;
45cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
46cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
47cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Change the size of an allocated block of memory P to N bytes,
48cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   with error checking.  */
49cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
50cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
51cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectxrealloc (void *p, size_t n)
52cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
5305436638acc7c010349a69c3395f1a57c642dc62Ying Wang  if (!n && p)
54cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    {
5505436638acc7c010349a69c3395f1a57c642dc62Ying Wang      /* The GNU and C99 realloc behaviors disagree here.  Act like
5605436638acc7c010349a69c3395f1a57c642dc62Ying Wang         GNU, even if the underlying realloc is C99.  */
5705436638acc7c010349a69c3395f1a57c642dc62Ying Wang      free (p);
5805436638acc7c010349a69c3395f1a57c642dc62Ying Wang      return NULL;
59cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    }
60cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
6105436638acc7c010349a69c3395f1a57c642dc62Ying Wang  p = realloc (p, n);
6205436638acc7c010349a69c3395f1a57c642dc62Ying Wang  if (!p && n)
6305436638acc7c010349a69c3395f1a57c642dc62Ying Wang    xalloc_die ();
6405436638acc7c010349a69c3395f1a57c642dc62Ying Wang  return p;
65cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
66cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
67cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* If P is null, allocate a block of at least *PN bytes; otherwise,
68cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   reallocate P so that it contains more than *PN bytes.  *PN must be
69cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   nonzero unless P is null.  Set *PN to the new block's size, and
70cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   return the pointer to the new block.  *PN is never set to zero, and
71cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   the returned pointer is never null.  */
72cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
73cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
74cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectx2realloc (void *p, size_t *pn)
75cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
7605436638acc7c010349a69c3395f1a57c642dc62Ying Wang  return x2nrealloc (p, pn, 1);
77cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
78cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
79cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Allocate S bytes of zeroed memory dynamically, with error checking.
80cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   There's no need for xnzalloc (N, S), since it would be equivalent
81cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   to xcalloc (N, S).  */
82cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
83cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
84cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectxzalloc (size_t s)
85cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
86cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return memset (xmalloc (s), 0, s);
87cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
88cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
89cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Allocate zeroed memory for N elements of S bytes, with error
90cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   checking.  S must be nonzero.  */
91cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
92cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
93cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectxcalloc (size_t n, size_t s)
94cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
95cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  void *p;
96cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  /* Test for overflow, since some calloc implementations don't have
97cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     proper overflow checks.  But omit overflow and size-zero tests if
98cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
99cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project     returns NULL if successful.  */
100cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
101cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project      || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
102cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project    xalloc_die ();
103cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return p;
104cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
105cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
106cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Clone an object P of size S, with error checking.  There's no need
107cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
108cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project   need for an arithmetic overflow check.  */
109cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
110cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectvoid *
111cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectxmemdup (void const *p, size_t s)
112cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
113cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return memcpy (xmalloc (s), p, s);
114cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
115cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
116cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project/* Clone STRING.  */
117cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project
118cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectchar *
119cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Projectxstrdup (char const *string)
120cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project{
121cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project  return xmemdup (string, strlen (string) + 1);
122cea198a11f15a2eb071d98491ca9a8bc8cebfbc4The Android Open Source Project}
123