]> git.cworth.org Git - acre/blob - xmalloc.c
Add another dataset style: ACRE_STYLE_BARS
[acre] / xmalloc.c
1 /* malloc routines with error checking
2  *
3  * Copyright © 2007 Mozilla Corporation
4  * Copyright © 2009 Carl D. Worth
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Authors:
21  *      Vladimir Vukicevic <vladimir@pobox.com>
22  *      Carl Worth <cworth@cworth.org>
23  */
24
25 #include "xmalloc.h"
26
27 #include <stdio.h>
28 #include <string.h>
29
30 /* Allocate memory using malloc(), checking for errors.
31  *
32  * Errors: This function will exit(1) if out-of-memory occurs.
33  */
34 void *
35 xmalloc (size_t size)
36 {
37     void *ret;
38
39     ret = malloc (size);
40     if (ret == NULL) {
41         fprintf (stderr, "Error: out of memory. Exiting.\n");
42         exit (1);
43     }
44
45     return ret;
46 }
47
48 /* Re-allocate memory using realloc(), checking for errors.
49  *
50  * Errors: This function will exit(1) if out-of-memory occurs.
51  */
52 void *
53 xrealloc (void *ptr, size_t size)
54 {
55     void *ret;
56
57     ret = realloc (ptr, size);
58     if (ret == NULL) {
59         fprintf (stderr, "Error: out of memory. Exiting.\n");
60         exit (1);
61     }
62
63     return ret;
64 }
65
66 char *
67 xstrdup (const char *s)
68 {
69     char *ret;
70
71     ret = strdup (s);
72     if (ret == NULL) {
73         fprintf (stderr, "Error: out of memory. Exiting.\n");
74         exit (1);
75     }
76
77     return ret;
78 }