Blame m4/size_max.m4

Packit aea12f
# size_max.m4 serial 11
Packit aea12f
dnl Copyright (C) 2003, 2005-2006, 2008-2019 Free Software Foundation, Inc.
Packit aea12f
dnl This file is free software; the Free Software Foundation
Packit aea12f
dnl gives unlimited permission to copy and/or distribute it,
Packit aea12f
dnl with or without modifications, as long as this notice is preserved.
Packit aea12f
Packit aea12f
dnl From Bruno Haible.
Packit aea12f
Packit aea12f
AC_PREREQ([2.61])
Packit aea12f
Packit aea12f
AC_DEFUN([gl_SIZE_MAX],
Packit aea12f
[
Packit aea12f
  AC_CHECK_HEADERS([stdint.h])
Packit aea12f
  dnl First test whether the system already has SIZE_MAX.
Packit aea12f
  AC_CACHE_CHECK([for SIZE_MAX], [gl_cv_size_max], [
Packit aea12f
    gl_cv_size_max=
Packit aea12f
    AC_EGREP_CPP([Found it], [
Packit aea12f
#include <limits.h>
Packit aea12f
#if HAVE_STDINT_H
Packit aea12f
#include <stdint.h>
Packit aea12f
#endif
Packit aea12f
#ifdef SIZE_MAX
Packit aea12f
Found it
Packit aea12f
#endif
Packit aea12f
], [gl_cv_size_max=yes])
Packit aea12f
    if test -z "$gl_cv_size_max"; then
Packit aea12f
      dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
Packit aea12f
      dnl than the type 'unsigned long'. Try hard to find a definition that can
Packit aea12f
      dnl be used in a preprocessor #if, i.e. doesn't contain a cast.
Packit aea12f
      AC_COMPUTE_INT([size_t_bits_minus_1], [sizeof (size_t) * CHAR_BIT - 1],
Packit aea12f
        [#include <stddef.h>
Packit aea12f
#include <limits.h>], [size_t_bits_minus_1=])
Packit aea12f
      AC_COMPUTE_INT([fits_in_uint], [sizeof (size_t) <= sizeof (unsigned int)],
Packit aea12f
        [#include <stddef.h>], [fits_in_uint=])
Packit aea12f
      if test -n "$size_t_bits_minus_1" && test -n "$fits_in_uint"; then
Packit aea12f
        if test $fits_in_uint = 1; then
Packit aea12f
          dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
Packit aea12f
          dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
Packit aea12f
          AC_COMPILE_IFELSE(
Packit aea12f
            [AC_LANG_PROGRAM(
Packit aea12f
               [[#include <stddef.h>
Packit aea12f
                 extern size_t foo;
Packit aea12f
                 extern unsigned long foo;
Packit aea12f
               ]],
Packit aea12f
               [[]])],
Packit aea12f
            [fits_in_uint=0])
Packit aea12f
        fi
Packit aea12f
        dnl We cannot use 'expr' to simplify this expression, because 'expr'
Packit aea12f
        dnl works only with 'long' integers in the host environment, while we
Packit aea12f
        dnl might be cross-compiling from a 32-bit platform to a 64-bit platform.
Packit aea12f
        if test $fits_in_uint = 1; then
Packit aea12f
          gl_cv_size_max="(((1U << $size_t_bits_minus_1) - 1) * 2 + 1)"
Packit aea12f
        else
Packit aea12f
          gl_cv_size_max="(((1UL << $size_t_bits_minus_1) - 1) * 2 + 1)"
Packit aea12f
        fi
Packit aea12f
      else
Packit aea12f
        dnl Shouldn't happen, but who knows...
Packit aea12f
        gl_cv_size_max='((size_t)~(size_t)0)'
Packit aea12f
      fi
Packit aea12f
    fi
Packit aea12f
  ])
Packit aea12f
  if test "$gl_cv_size_max" != yes; then
Packit aea12f
    AC_DEFINE_UNQUOTED([SIZE_MAX], [$gl_cv_size_max],
Packit aea12f
      [Define as the maximum value of type 'size_t', if the system doesn't define it.])
Packit aea12f
  fi
Packit aea12f
  dnl Don't redefine SIZE_MAX in config.h if config.h is re-included after
Packit aea12f
  dnl <stdint.h>. Remember that the #undef in AH_VERBATIM gets replaced with
Packit aea12f
  dnl #define by AC_DEFINE_UNQUOTED.
Packit aea12f
  AH_VERBATIM([SIZE_MAX],
Packit aea12f
[/* Define as the maximum value of type 'size_t', if the system doesn't define
Packit aea12f
   it. */
Packit aea12f
#ifndef SIZE_MAX
Packit aea12f
# undef SIZE_MAX
Packit aea12f
#endif])
Packit aea12f
])