checkin of 0.3.0
[pspp-builds.git] / src / alloc.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "str.h"
25
26 static void out_of_memory (void);
27 \f
28 /* Public functions. */
29
30 /* Allocates a block of SIZE bytes and returns it.
31    If SIZE is 0, returns a null pointer.
32    Aborts if unsuccessful. */
33 void *
34 xmalloc (size_t size)
35 {
36   void *vp;
37   if (size == 0)
38     return NULL;
39
40   vp = malloc (size);
41   if (!vp)
42     out_of_memory ();
43
44   return vp;
45 }
46
47 /* Allocates a block of SIZE bytes, fill it with all-bits-0, and
48    returns it.
49    If SIZE is 0, returns a null pointer.
50    Aborts if unsuccessful. */
51 void *
52 xcalloc (size_t size)
53 {
54   void *vp = xmalloc (size);
55   memset (vp, 0, size);
56   return vp;
57 }
58
59 /* If SIZE is 0, then block PTR is freed and a null pointer is
60    returned.
61    Otherwise, if PTR is a null pointer, then a new block is allocated
62    and returned.
63    Otherwise, block PTR is reallocated to be SIZE bytes in size and
64    the new location of the block is returned.
65    Aborts if unsuccessful. */
66 void *
67 xrealloc (void *ptr, size_t size)
68 {
69   void *vp;
70   if (!size)
71     {
72       if (ptr)
73         free (ptr);
74
75       return NULL;
76     }
77
78   if (ptr)
79     vp = realloc (ptr, size);
80   else
81     vp = malloc (size);
82
83   if (!vp)
84     out_of_memory ();
85
86   return vp;
87 }
88
89 /* Makes a copy of string S in malloc()'d memory and returns the copy.
90    S must not be a null pointer. */
91 char *
92 xstrdup (const char *s)
93 {
94   size_t size;
95   char *t;
96
97   assert (s != NULL);
98
99   size = strlen (s) + 1;
100
101   t = malloc (size);
102   if (!t)
103     out_of_memory ();
104
105   memcpy (t, s, size);
106   return t;
107 }
108 \f
109 /* Private functions. */
110
111 /* Report an out-of-memory condition and abort execution. */
112 static void
113 out_of_memory (void)
114 {
115 #if __CHECKER__
116   fprintf (stderr, "Out of memory: inducing segfault\n");
117   *((int *) 0) = 0;
118 #else
119   fprintf (stderr, "virtual memory exhausted\n");
120   exit (EXIT_FAILURE);
121 #endif
122 }