Adam Tkac | fc4cd71 | 2009-03-11 11:14:37 +0000 | [diff] [blame^] | 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 | |
| 7 | dnl From Bruno Haible. |
| 8 | |
| 9 | AC_DEFUN([gl_SIZE_MAX], |
| 10 | [ |
| 11 | AC_CHECK_HEADERS(stdint.h) |
| 12 | dnl First test whether the system already has SIZE_MAX. |
| 13 | AC_MSG_CHECKING([for SIZE_MAX]) |
| 14 | result= |
| 15 | AC_EGREP_CPP([Found it], [ |
| 16 | #include <limits.h> |
| 17 | #if HAVE_STDINT_H |
| 18 | #include <stdint.h> |
| 19 | #endif |
| 20 | #ifdef SIZE_MAX |
| 21 | Found it |
| 22 | #endif |
| 23 | ], result=yes) |
| 24 | if test -z "$result"; then |
| 25 | dnl Define it ourselves. Here we assume that the type 'size_t' is not wider |
| 26 | dnl than the type 'unsigned long'. |
| 27 | dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr', |
| 28 | dnl which is guaranteed to work from LONG_MIN to LONG_MAX. |
| 29 | _AC_COMPUTE_INT([~(size_t)0 / 10], res_hi, |
| 30 | [#include <stddef.h>], result=?) |
| 31 | _AC_COMPUTE_INT([~(size_t)0 % 10], res_lo, |
| 32 | [#include <stddef.h>], result=?) |
| 33 | _AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint, |
| 34 | [#include <stddef.h>], result=?) |
| 35 | if test "$fits_in_uint" = 1; then |
| 36 | dnl Even though SIZE_MAX fits in an unsigned int, it must be of type |
| 37 | dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'. |
| 38 | AC_TRY_COMPILE([#include <stddef.h> |
| 39 | extern size_t foo; |
| 40 | extern unsigned long foo; |
| 41 | ], [], fits_in_uint=0) |
| 42 | fi |
| 43 | if test -z "$result"; then |
| 44 | if test "$fits_in_uint" = 1; then |
| 45 | result="$res_hi$res_lo"U |
| 46 | else |
| 47 | result="$res_hi$res_lo"UL |
| 48 | fi |
| 49 | else |
| 50 | dnl Shouldn't happen, but who knows... |
| 51 | result='~(size_t)0' |
| 52 | fi |
| 53 | fi |
| 54 | AC_MSG_RESULT([$result]) |
| 55 | if test "$result" != yes; then |
| 56 | AC_DEFINE_UNQUOTED([SIZE_MAX], [$result], |
| 57 | [Define as the maximum value of type 'size_t', if the system doesn't define it.]) |
| 58 | fi |
| 59 | ]) |