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