size_max.m4 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # size_max.m4 serial 2
  2. dnl Copyright (C) 2003 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_MSG_CHECKING([for SIZE_MAX])
  12. result=
  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. ], result=yes)
  22. if test -z "$result"; then
  23. dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
  24. dnl than the type 'unsigned long'.
  25. dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr',
  26. dnl which is guaranteed to work from LONG_MIN to LONG_MAX.
  27. _AC_COMPUTE_INT([~(size_t)0 / 10], res_hi,
  28. [#include <stddef.h>], result=?)
  29. _AC_COMPUTE_INT([~(size_t)0 % 10], res_lo,
  30. [#include <stddef.h>], result=?)
  31. _AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint,
  32. [#include <stddef.h>], result=?)
  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_TRY_COMPILE([#include <stddef.h>
  37. extern size_t foo;
  38. extern unsigned long foo;
  39. ], [], fits_in_uint=0)
  40. fi
  41. if test -z "$result"; then
  42. if test "$fits_in_uint" = 1; then
  43. result="$res_hi$res_lo"U
  44. else
  45. result="$res_hi$res_lo"UL
  46. fi
  47. else
  48. dnl Shouldn't happen, but who knows...
  49. result='~(size_t)0'
  50. fi
  51. fi
  52. AC_MSG_RESULT([$result])
  53. if test "$result" != yes; then
  54. AC_DEFINE_UNQUOTED([SIZE_MAX], [$result],
  55. [Define as the maximum value of type 'size_t', if the system doesn't define it.])
  56. fi
  57. ])