]> git.cworth.org Git - acre/blob - xmalloc.h
Add another dataset style: ACRE_STYLE_BARS
[acre] / xmalloc.h
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 #ifndef XMALLOC_H
26 #define XMALLOC_H
27
28 #include <stdint.h>
29 #include <stdlib.h>
30
31 /* Allocate memory using malloc(), checking for errors.
32  *
33  * Errors: This function will exit(1) if out-of-memory occurs.
34  */
35 void *
36 xmalloc (size_t size);
37
38
39 /* Allocates nmemb*size memory using xmalloc(), taking care to not
40  * overflow when doing the multiplication.
41  *
42  * size should be a constant so that the compiler can optimize
43  * out a constant division.
44  *
45  * Return value: A pointer to the newly allocated memory or NULL in
46  * case of overflow.
47  *
48  * Errors: This function will exit(1) if out-of-memory occurs.
49  */
50 #define xmalloc_ab(nmemb, size) \
51   ((size) && (unsigned) (nmemb) >= INT32_MAX / (unsigned) (size) ? NULL : \
52    xmalloc((unsigned) (nmemb) * (unsigned) (size)))
53
54 /* Re-allocate memory using realloc(), checking for errors.
55  *
56  * Errors: This function will exit(1) if out-of-memory occurs.
57  */
58 void *
59 xrealloc (void *ptr, size_t size);
60
61 /* Reallocates ptr a block of nmemb*@size memory using xrealloc(), taking
62  * care to not overflow when doing the multiplication.
63  *
64  * size should be a constant so that the compiler can optimize
65  * out a constant division.
66  *
67  * Return value: A pointer to the newly allocated memory, or NULL in
68  * case of overflow (whereupon the original block of memory is left
69  * untouched).
70  *
71  * Errors: This function will exit(1) if out-of-memory occurs.
72  *
73  */
74 #define xrealloc_ab(ptr, nmemb, size) \
75   ((size) && (unsigned) (nmemb) >= INT32_MAX / (unsigned) (size) ? NULL : \
76    xrealloc(ptr, (unsigned) (nmemb) * (unsigned) (size)))
77
78 char *
79 xstrdup (const char *s);
80
81 #endif /* XMALLOC_H */