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