Debian Codegraph @

m4/size_max.m4

raw
1 # size_max.m4
2 # serial 12
3 dnl Copyright (C) 2003, 2005-2006, 2008-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 dnl From Bruno Haible.
10
11 AC_PREREQ([2.61])
12
13 AC_DEFUN([gl_SIZE_MAX],
14 [
15 AC_CHECK_HEADERS([stdint.h])
16 dnl First test whether the system already has SIZE_MAX.
17 AC_CACHE_CHECK([for SIZE_MAX], [gl_cv_size_max], [
18 gl_cv_size_max=no
19 AC_EGREP_CPP([Found it], [
20 #include <limits.h>
21 #if HAVE_STDINT_H
22 #include <stdint.h>
23 #endif
24 #ifdef SIZE_MAX
25 Found it
26 #endif
27 ], [gl_cv_size_max=yes])
28 if test $gl_cv_size_max != yes; then
29 dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
30 dnl than the type 'unsigned long'. Try hard to find a definition that can
31 dnl be used in a preprocessor #if, i.e. doesn't contain a cast.
32 AC_COMPUTE_INT([size_t_bits_minus_1], [sizeof (size_t) * CHAR_BIT - 1],
33 [#include <stddef.h>
34 #include <limits.h>], [size_t_bits_minus_1=])
35 AC_COMPUTE_INT([fits_in_uint], [sizeof (size_t) <= sizeof (unsigned int)],
36 [#include <stddef.h>], [fits_in_uint=])
37 if test -n "$size_t_bits_minus_1" && test -n "$fits_in_uint"; then
38 if test $fits_in_uint = 1; then
39 dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
40 dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
41 AC_COMPILE_IFELSE(
42 [AC_LANG_PROGRAM(
43 [[#include <stddef.h>
44 extern size_t foo;
45 extern unsigned long foo;
46 ]],
47 [[]])],
48 [fits_in_uint=0])
49 fi
50 dnl We cannot use 'expr' to simplify this expression, because 'expr'
51 dnl works only with 'long' integers in the host environment, while we
52 dnl might be cross-compiling from a 32-bit platform to a 64-bit platform.
53 if test $fits_in_uint = 1; then
54 gl_cv_size_max="(((1U << $size_t_bits_minus_1) - 1) * 2 + 1)"
55 else
56 gl_cv_size_max="(((1UL << $size_t_bits_minus_1) - 1) * 2 + 1)"
57 fi
58 else
59 dnl Shouldn't happen, but who knows...
60 gl_cv_size_max='((size_t)~(size_t)0)'
61 fi
62 fi
63 ])
64 if test "$gl_cv_size_max" != yes; then
65 AC_DEFINE_UNQUOTED([SIZE_MAX], [$gl_cv_size_max],
66 [Define as the maximum value of type 'size_t', if the system doesn't define it.])
67 fi
68 dnl Don't redefine SIZE_MAX in config.h if config.h is re-included after
69 dnl <stdint.h>. Remember that the #undef in AH_VERBATIM gets replaced with
70 dnl #define by AC_DEFINE_UNQUOTED.
71 AH_VERBATIM([SIZE_MAX],
72 [/* Define as the maximum value of type 'size_t', if the system doesn't define
73 it. */
74 #ifndef SIZE_MAX
75 # undef SIZE_MAX
76 #endif])
77 ])