]> git.cworth.org Git - acre/blob - xmalloc.c
Remove code duplication for X and Y ticks
[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 #include <stdio.h>
27
28 /* Allocate memory using malloc(), checking for errors.
29  *
30  * Errors: This function will exit(1) if out-of-memory occurs.
31  */
32 void *
33 xmalloc (size_t size)
34 {
35     void *ret;
36
37     ret = malloc (size);
38     if (ret == NULL) {
39         fprintf (stderr, "Error: out of memory. Exiting.\n");
40         exit (1);
41     }
42
43     return ret;
44 }
45
46 /* Re-allocate memory using realloc(), checking for errors.
47  *
48  * Errors: This function will exit(1) if out-of-memory occurs.
49  */
50 void *
51 xrealloc (void *ptr, size_t size)
52 {
53     void *ret;
54
55     ret = realloc (ptr, size);
56     if (ret == NULL) {
57         fprintf (stderr, "Error: out of memory. Exiting.\n");
58         exit (1);
59     }
60
61     return ret;
62 }