size_max.m4 2.8 KB

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