size_max.m4 3.0 KB

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