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