Back to index

nagios-plugins  1.4.16
xmalloc.c
Go to the documentation of this file.
00001 /* xmalloc.c -- malloc with out of memory checking
00002 
00003    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
00004    2000, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Free Software
00005    Foundation, Inc.
00006 
00007    This program is free software: you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include <config.h>
00021 
00022 #if ! HAVE_INLINE
00023 # define static_inline
00024 #endif
00025 #include "xalloc.h"
00026 #undef static_inline
00027 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 /* 1 if calloc is known to be compatible with GNU calloc.  This
00032    matters if we are not also using the calloc module, which defines
00033    HAVE_CALLOC and supports the GNU API even on non-GNU platforms.  */
00034 #if defined HAVE_CALLOC || defined __GLIBC__
00035 enum { HAVE_GNU_CALLOC = 1 };
00036 #else
00037 enum { HAVE_GNU_CALLOC = 0 };
00038 #endif
00039 
00040 /* Allocate N bytes of memory dynamically, with error checking.  */
00041 
00042 void *
00043 xmalloc (size_t n)
00044 {
00045   void *p = malloc (n);
00046   if (!p && n != 0)
00047     xalloc_die ();
00048   return p;
00049 }
00050 
00051 /* Change the size of an allocated block of memory P to N bytes,
00052    with error checking.  */
00053 
00054 void *
00055 xrealloc (void *p, size_t n)
00056 {
00057   p = realloc (p, n);
00058   if (!p && n != 0)
00059     xalloc_die ();
00060   return p;
00061 }
00062 
00063 /* If P is null, allocate a block of at least *PN bytes; otherwise,
00064    reallocate P so that it contains more than *PN bytes.  *PN must be
00065    nonzero unless P is null.  Set *PN to the new block's size, and
00066    return the pointer to the new block.  *PN is never set to zero, and
00067    the returned pointer is never null.  */
00068 
00069 void *
00070 x2realloc (void *p, size_t *pn)
00071 {
00072   return x2nrealloc (p, pn, 1);
00073 }
00074 
00075 /* Allocate S bytes of zeroed memory dynamically, with error checking.
00076    There's no need for xnzalloc (N, S), since it would be equivalent
00077    to xcalloc (N, S).  */
00078 
00079 void *
00080 xzalloc (size_t s)
00081 {
00082   return memset (xmalloc (s), 0, s);
00083 }
00084 
00085 /* Allocate zeroed memory for N elements of S bytes, with error
00086    checking.  S must be nonzero.  */
00087 
00088 void *
00089 xcalloc (size_t n, size_t s)
00090 {
00091   void *p;
00092   /* Test for overflow, since some calloc implementations don't have
00093      proper overflow checks.  But omit overflow and size-zero tests if
00094      HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
00095      returns NULL if successful.  */
00096   if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
00097       || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
00098     xalloc_die ();
00099   return p;
00100 }
00101 
00102 /* Clone an object P of size S, with error checking.  There's no need
00103    for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
00104    need for an arithmetic overflow check.  */
00105 
00106 void *
00107 xmemdup (void const *p, size_t s)
00108 {
00109   return memcpy (xmalloc (s), p, s);
00110 }
00111 
00112 /* Clone STRING.  */
00113 
00114 char *
00115 xstrdup (char const *string)
00116 {
00117   return xmemdup (string, strlen (string) + 1);
00118 }