]> code.delx.au - gnu-emacs/blob - lib/xalloc-oversized.h
Merge from origin/emacs-24
[gnu-emacs] / lib / xalloc-oversized.h
1 /* xalloc-oversized.h -- memory allocation size checking
2
3 Copyright (C) 1990-2000, 2003-2004, 2006-2015 Free Software
4 Foundation, Inc.
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 3 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,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #ifndef XALLOC_OVERSIZED_H_
20 # define XALLOC_OVERSIZED_H_
21
22 # include <stddef.h>
23
24 /* Return 1 if an array of N objects, each of size S, cannot exist due
25 to size arithmetic overflow. S must be positive and N must be
26 nonnegative. This is a macro, not a function, so that it
27 works correctly even when SIZE_MAX < N.
28
29 By gnulib convention, SIZE_MAX represents overflow in size
30 calculations, so the conservative dividend to use here is
31 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
32 However, malloc (SIZE_MAX) fails on all known hosts where
33 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
34 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
35 branch when S is known to be 1. */
36 # define xalloc_oversized(n, s) \
37 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
38
39 #endif /* !XALLOC_OVERSIZED_H_ */