joachim99@52: /* xmalloc.c -- malloc with out of memory checking joachim99@52: joachim99@52: Modified for KDiff3 by Joachim Eibl 2003. joachim99@53: The original file was part of GNU DIFF. joachim99@52: joachim99@52: Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc. joachim99@52: joachim99@52: This program is free software; you can redistribute it and/or modify joachim99@52: it under the terms of the GNU General Public License as published by joachim99@52: the Free Software Foundation; either version 2, or (at your option) joachim99@52: any later version. joachim99@52: joachim99@52: This program is distributed in the hope that it will be useful, joachim99@52: but WITHOUT ANY WARRANTY; without even the implied warranty of joachim99@52: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the joachim99@52: GNU General Public License for more details. joachim99@52: joachim99@52: You should have received a copy of the GNU General Public License joachim99@52: along with this program; if not, write to the Free Software Foundation, joachim99@69: Inc., 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. */ joachim99@52: joachim99@80: # include joachim99@52: joachim99@52: #include joachim99@52: joachim99@52: joachim99@52: #include joachim99@52: #include joachim99@52: joachim99@52: joachim99@52: #ifndef EXIT_FAILURE joachim99@52: # define EXIT_FAILURE 1 joachim99@52: #endif joachim99@52: joachim99@53: #include "gnudiff_diff.h" joachim99@52: /* If non NULL, call this function when memory is exhausted. */ joachim99@52: //void (*xalloc_fail_func) PARAMS ((void)) = 0; joachim99@52: void (*xalloc_fail_func)(void) = 0; joachim99@52: joachim99@52: joachim99@53: void GnuDiff::xalloc_die (void) joachim99@52: { joachim99@52: if (xalloc_fail_func) joachim99@52: (*xalloc_fail_func) (); joachim99@52: //error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); joachim99@52: /* The `noreturn' cannot be given to error, since it may return if joachim99@52: its first argument is 0. To help compilers understand the joachim99@52: xalloc_die does terminate, call exit. */ joachim99@52: exit (EXIT_FAILURE); joachim99@52: } joachim99@52: joachim99@52: /* Allocate N bytes of memory dynamically, with error checking. */ joachim99@52: joachim99@52: void * joachim99@53: GnuDiff::xmalloc (size_t n) joachim99@52: { joachim99@52: void *p; joachim99@52: joachim99@66: p = malloc (n == 0 ? 1 : n); // There are systems where malloc returns 0 for n==0. joachim99@52: if (p == 0) joachim99@52: xalloc_die (); joachim99@52: return p; joachim99@52: } joachim99@52: joachim99@52: /* Change the size of an allocated block of memory P to N bytes, joachim99@52: with error checking. */ joachim99@52: joachim99@52: void * joachim99@53: GnuDiff::xrealloc (void *p, size_t n) joachim99@52: { joachim99@66: p = realloc (p, n==0 ? 1 : n); joachim99@52: if (p == 0) joachim99@52: xalloc_die (); joachim99@52: return p; joachim99@52: } joachim99@52: joachim99@52: joachim99@52: /* Yield a new block of SIZE bytes, initialized to zero. */ joachim99@52: joachim99@52: void * joachim99@53: GnuDiff::zalloc (size_t size) joachim99@52: { joachim99@52: void *p = xmalloc (size); joachim99@52: memset (p, 0, size); joachim99@52: return p; joachim99@52: }