xmalloc.c (131447) | xmalloc.c (133543) |
---|---|
1/* xmalloc.c -- malloc with out of memory checking 2 | 1/* xmalloc.c -- malloc with out of memory checking 2 |
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003, 4 1999, 2000, 2002, 2003 Free Software Foundation, Inc. | 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 4 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. |
5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of --- 8 unchanged lines hidden (view full) --- 21# include <config.h> 22#endif 23 24#include "xalloc.h" 25 26#include <stdlib.h> 27#include <string.h> 28 | 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of --- 8 unchanged lines hidden (view full) --- 21# include <config.h> 22#endif 23 24#include "xalloc.h" 25 26#include <stdlib.h> 27#include <string.h> 28 |
29#include "gettext.h" 30#define _(msgid) gettext (msgid) 31#define N_(msgid) msgid 32 33#include "error.h" 34#include "exitfail.h" 35 | |
36#ifndef SIZE_MAX 37# define SIZE_MAX ((size_t) -1) 38#endif 39 | 29#ifndef SIZE_MAX 30# define SIZE_MAX ((size_t) -1) 31#endif 32 |
40#ifndef HAVE_MALLOC 41"you must run the autoconf test for a GNU libc compatible malloc" 42#endif 43 44#ifndef HAVE_REALLOC 45"you must run the autoconf test for a GNU libc compatible realloc" 46#endif 47 48/* If non NULL, call this function when memory is exhausted. */ 49void (*xalloc_fail_func) (void) = 0; 50 51/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message 52 before exiting when memory is exhausted. Goes through gettext. */ 53char const xalloc_msg_memory_exhausted[] = N_("memory exhausted"); 54 55void 56xalloc_die (void) 57{ 58 if (xalloc_fail_func) 59 (*xalloc_fail_func) (); 60 error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); 61 /* The `noreturn' cannot be given to error, since it may return if 62 its first argument is 0. To help compilers understand the 63 xalloc_die does terminate, call abort. */ 64 abort (); 65} 66 | |
67/* Allocate an array of N objects, each with S bytes of memory, 68 dynamically, with error checking. S must be nonzero. */ 69 70static inline void * 71xnmalloc_inline (size_t n, size_t s) 72{ 73 void *p; | 33/* Allocate an array of N objects, each with S bytes of memory, 34 dynamically, with error checking. S must be nonzero. */ 35 36static inline void * 37xnmalloc_inline (size_t n, size_t s) 38{ 39 void *p; |
74 if (xalloc_oversized (n, s) || ! (p = malloc (n * s))) | 40 if (xalloc_oversized (n, s) || (! (p = malloc (n * s)) && n != 0)) |
75 xalloc_die (); 76 return p; 77} 78 79void * 80xnmalloc (size_t n, size_t s) 81{ 82 return xnmalloc_inline (n, s); --- 8 unchanged lines hidden (view full) --- 91} 92 93/* Change the size of an allocated block of memory P to an array of N 94 objects each of S bytes, with error checking. S must be nonzero. */ 95 96static inline void * 97xnrealloc_inline (void *p, size_t n, size_t s) 98{ | 41 xalloc_die (); 42 return p; 43} 44 45void * 46xnmalloc (size_t n, size_t s) 47{ 48 return xnmalloc_inline (n, s); --- 8 unchanged lines hidden (view full) --- 57} 58 59/* Change the size of an allocated block of memory P to an array of N 60 objects each of S bytes, with error checking. S must be nonzero. */ 61 62static inline void * 63xnrealloc_inline (void *p, size_t n, size_t s) 64{ |
99 if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s))) | 65 if (xalloc_oversized (n, s) || (! (p = realloc (p, n * s)) && n != 0)) |
100 xalloc_die (); 101 return p; 102} 103 104void * 105xnrealloc (void *p, size_t n, size_t s) 106{ 107 return xnrealloc_inline (p, n, s); --- 126 unchanged lines hidden (view full) --- 234 checking. S must be nonzero. */ 235 236void * 237xcalloc (size_t n, size_t s) 238{ 239 void *p; 240 /* Test for overflow, since some calloc implementations don't have 241 proper overflow checks. */ | 66 xalloc_die (); 67 return p; 68} 69 70void * 71xnrealloc (void *p, size_t n, size_t s) 72{ 73 return xnrealloc_inline (p, n, s); --- 126 unchanged lines hidden (view full) --- 200 checking. S must be nonzero. */ 201 202void * 203xcalloc (size_t n, size_t s) 204{ 205 void *p; 206 /* Test for overflow, since some calloc implementations don't have 207 proper overflow checks. */ |
242 if (xalloc_oversized (n, s) || ! (p = calloc (n, s))) | 208 if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0)) |
243 xalloc_die (); 244 return p; 245} 246 247/* Clone an object P of size S, with error checking. There's no need 248 for xnclone (P, N, S), since xclone (P, N * S) works without any 249 need for an arithmetic overflow check. */ 250 251void * 252xclone (void const *p, size_t s) 253{ 254 return memcpy (xmalloc (s), p, s); 255} | 209 xalloc_die (); 210 return p; 211} 212 213/* Clone an object P of size S, with error checking. There's no need 214 for xnclone (P, N, S), since xclone (P, N * S) works without any 215 need for an arithmetic overflow check. */ 216 217void * 218xclone (void const *p, size_t s) 219{ 220 return memcpy (xmalloc (s), p, s); 221} |