1 /* malloc routines with error checking
3 * Copyright © 2007 Mozilla Corporation
4 * Copyright © 2009 Carl D. Worth
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.
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.
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.
21 * Vladimir Vukicevic <vladimir@pobox.com>
22 * Carl Worth <cworth@cworth.org>
31 /* Allocate memory using malloc(), checking for errors.
33 * Errors: This function will exit(1) if out-of-memory occurs.
36 xmalloc (size_t size);
39 /* Allocates nmemb*size memory using xmalloc(), taking care to not
40 * overflow when doing the multiplication.
42 * size should be a constant so that the compiler can optimize
43 * out a constant division.
45 * Return value: A pointer to the newly allocated memory or NULL in
48 * Errors: This function will exit(1) if out-of-memory occurs.
50 #define xmalloc_ab(nmemb, size) \
51 ((size) && (unsigned) (nmemb) >= INT32_MAX / (unsigned) (size) ? NULL : \
52 xmalloc((unsigned) (nmemb) * (unsigned) (size)))
54 /* Re-allocate memory using realloc(), checking for errors.
56 * Errors: This function will exit(1) if out-of-memory occurs.
59 xrealloc (void *ptr, size_t size);
61 /* Reallocates ptr a block of nmemb*@size memory using xrealloc(), taking
62 * care to not overflow when doing the multiplication.
64 * size should be a constant so that the compiler can optimize
65 * out a constant division.
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
71 * Errors: This function will exit(1) if out-of-memory occurs.
74 #define xrealloc_ab(ptr, nmemb, size) \
75 ((size) && (unsigned) (nmemb) >= INT32_MAX / (unsigned) (size) ? NULL : \
76 xrealloc(ptr, (unsigned) (nmemb) * (unsigned) (size)))
78 #endif /* XMALLOC_H */