comparison kdiff3/src-QT4/gnudiff_xmalloc.cpp @ 75:08ea9b86c12c

KDiff3-0.9.91
author joachim99
date Sat, 04 Nov 2006 00:05:00 +0000
parents kdiff3/src/gnudiff_xmalloc.cpp@8febbfb1148c
children fcd146072e0c
comparison
equal deleted inserted replaced
74:069521efec1a 75:08ea9b86c12c
1 /* xmalloc.c -- malloc with out of memory checking
2
3 Modified for KDiff3 by Joachim Eibl 2003.
4 The original file was part of GNU DIFF.
5
6 Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <sys/types.h>
27
28
29 #include <stdlib.h>
30 #include <string.h>
31
32
33 #ifndef EXIT_FAILURE
34 # define EXIT_FAILURE 1
35 #endif
36
37 #include "gnudiff_diff.h"
38 /* If non NULL, call this function when memory is exhausted. */
39 //void (*xalloc_fail_func) PARAMS ((void)) = 0;
40 void (*xalloc_fail_func)(void) = 0;
41
42
43 void GnuDiff::xalloc_die (void)
44 {
45 if (xalloc_fail_func)
46 (*xalloc_fail_func) ();
47 //error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
48 /* The `noreturn' cannot be given to error, since it may return if
49 its first argument is 0. To help compilers understand the
50 xalloc_die does terminate, call exit. */
51 exit (EXIT_FAILURE);
52 }
53
54 /* Allocate N bytes of memory dynamically, with error checking. */
55
56 void *
57 GnuDiff::xmalloc (size_t n)
58 {
59 void *p;
60
61 p = malloc (n == 0 ? 1 : n); // There are systems where malloc returns 0 for n==0.
62 if (p == 0)
63 xalloc_die ();
64 return p;
65 }
66
67 /* Change the size of an allocated block of memory P to N bytes,
68 with error checking. */
69
70 void *
71 GnuDiff::xrealloc (void *p, size_t n)
72 {
73 p = realloc (p, n==0 ? 1 : n);
74 if (p == 0)
75 xalloc_die ();
76 return p;
77 }
78
79
80 /* Yield a new block of SIZE bytes, initialized to zero. */
81
82 void *
83 GnuDiff::zalloc (size_t size)
84 {
85 void *p = xmalloc (size);
86 memset (p, 0, size);
87 return p;
88 }