No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / texinfo / intl / xsize.h
blob0486077ee098e1bed5e1244bcc077f7ceffe8003
1 /* $NetBSD$ */
3 /* xsize.h -- Checked size_t computations.
5 Copyright (C) 2003 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Library General Public License as published
9 by the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 USA. */
22 #ifndef _XSIZE_H
23 #define _XSIZE_H
25 /* Get size_t. */
26 #include <stddef.h>
28 /* Get SIZE_MAX. */
29 #include <limits.h>
30 #if HAVE_STDINT_H
31 # include <stdint.h>
32 #endif
34 /* The size of memory objects is often computed through expressions of
35 type size_t. Example:
36 void* p = malloc (header_size + n * element_size).
37 These computations can lead to overflow. When this happens, malloc()
38 returns a piece of memory that is way too small, and the program then
39 crashes while attempting to fill the memory.
40 To avoid this, the functions and macros in this file check for overflow.
41 The convention is that SIZE_MAX represents overflow.
42 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
43 implementation that uses mmap --, it's recommended to use size_overflow_p()
44 or size_in_bounds_p() before invoking malloc().
45 The example thus becomes:
46 size_t size = xsum (header_size, xtimes (n, element_size));
47 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
50 /* Convert an arbitrary value >= 0 to type size_t. */
51 #define xcast_size_t(N) \
52 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
54 /* Sum of two sizes, with overflow check. */
55 static inline size_t
56 #if __GNUC__ >= 3
57 __attribute__ ((__pure__))
58 #endif
59 xsum (size_t size1, size_t size2)
61 size_t sum = size1 + size2;
62 return (sum >= size1 ? sum : SIZE_MAX);
65 /* Sum of three sizes, with overflow check. */
66 static inline size_t
67 #if __GNUC__ >= 3
68 __attribute__ ((__pure__))
69 #endif
70 xsum3 (size_t size1, size_t size2, size_t size3)
72 return xsum (xsum (size1, size2), size3);
75 /* Sum of four sizes, with overflow check. */
76 static inline size_t
77 #if __GNUC__ >= 3
78 __attribute__ ((__pure__))
79 #endif
80 xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
82 return xsum (xsum (xsum (size1, size2), size3), size4);
85 /* Maximum of two sizes, with overflow check. */
86 static inline size_t
87 #if __GNUC__ >= 3
88 __attribute__ ((__pure__))
89 #endif
90 xmax (size_t size1, size_t size2)
92 /* No explicit check is needed here, because for any n:
93 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
94 return (size1 >= size2 ? size1 : size2);
97 /* Multiplication of a count with an element size, with overflow check.
98 The count must be >= 0 and the element size must be > 0.
99 This is a macro, not an inline function, so that it works correctly even
100 when N is of a wider tupe and N > SIZE_MAX. */
101 #define xtimes(N, ELSIZE) \
102 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
104 /* Check for overflow. */
105 #define size_overflow_p(SIZE) \
106 ((SIZE) == SIZE_MAX)
107 /* Check against overflow. */
108 #define size_in_bounds_p(SIZE) \
109 ((SIZE) != SIZE_MAX)
111 #endif /* _XSIZE_H */