[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] addition: size_max.m4, ptrdiff_max.m4
From: |
Bruno Haible |
Subject: |
[Bug-gnulib] addition: size_max.m4, ptrdiff_max.m4 |
Date: |
Tue, 11 Nov 2003 12:58:50 +0100 |
User-agent: |
KMail/1.5 |
Hi,
I've added two macros that determine SIZE_MAX and PTRDIFF_MAX, respectively,
at configure time, and #define them in config.h if not already present in
limits.h or stdint.h. Should also work when cross-compiling.
2003-11-11 Bruno Haible <address@hidden>
* modules/xsize (Files): Add m4/size_max.m4.
* modules/xalloc (Files): Add m4/size_max.m4, m4_ptrdiff_max.m4.
* m4/size_max.m4: New file.
* m4/ptrdiff_max.m4: New file.
* m4/xsize,m4 (gl_XSIZE): Require gl_SIZE_MAX.
* m4/xalloc.m4 (gl_PREREQ_XALLOC): New file.
(gl_XALLOC): Invoke it.
* lib/xsize.h (SIZE_MAX): Remove fallback definition.
* lib/xalloc.h: Include limits.h. Assume SIZE_MAX and PTRDIFF_MAX
are defined.
=========================== m4/size_max.m4 ===========================
# size_max.m4 serial 1
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl From Bruno Haible.
AC_DEFUN([gl_SIZE_MAX],
[
AC_CHECK_HEADERS_ONCE(stdint.h)
dnl First test whether the system already has SIZE_MAX.
AC_MSG_CHECKING([for SIZE_MAX])
result=
AC_EGREP_CPP([Found it], [
#include <limits.h>
#if HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef SIZE_MAX
Found it
#endif
], result=yes)
if test -z "$result"; then
dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
dnl than the type 'unsigned long'.
dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr',
dnl which is guaranteed to work from LONG_MIN to LONG_MAX.
_AC_COMPUTE_INT([~(size_t)0 / 10], res_hi,
[#include <stddef.h>], result=?)
_AC_COMPUTE_INT([~(size_t)0 % 10], res_lo,
[#include <stddef.h>], result=?)
_AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint,
[#include <stddef.h>], result=?)
if test "$fits_in_uint" = 1; then
dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
AC_TRY_COMPILE([#include <stddef.h>
extern size_t foo;
extern unsigned long foo;
], [], fits_in_uint=0)
fi
if test -z "$result"; then
if test "$fits_in_uint" = 1; then
result="$res_hi$res_lo"U
else
result="$res_hi$res_lo"UL
fi
else
dnl Shouldn't happen, but who knows...
result='~(size_t)0'
fi
fi
AC_MSG_RESULT([$result])
if test "$result" != yes; then
AC_DEFINE_UNQUOTED([SIZE_MAX], [$result],
[Define as the maximum value of type 'size_t', if the system doesn't
define it.])
fi
])
=========================== m4/ptrdiff_max.m4 ===========================
# ptrdiff_max.m4 serial 1
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl From Bruno Haible.
AC_DEFUN([gl_PTRDIFF_MAX],
[
AC_CHECK_TYPE([ptrdiff_t], ,
[AC_DEFINE([ptrdiff_t], [long],
[Define as the type of the result of subtracting two pointers, if the
system doesn't define it.])
])
AC_CHECK_HEADERS_ONCE(stdint.h)
dnl First test whether the system already has PTRDIFF_MAX.
AC_MSG_CHECKING([for PTRDIFF_MAX])
result=
AC_EGREP_CPP([Found it], [
#include <limits.h>
#if HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef PTRDIFF_MAX
Found it
#endif
], result=yes)
if test -z "$result"; then
dnl Define it ourselves. Here we assume that the type 'ptrdiff_t' is not
dnl wider than the type 'long'.
dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr',
dnl which is guaranteed to work from LONG_MIN to LONG_MAX.
_AC_COMPUTE_INT([STYPE_MAXIMUM (ptrdiff_t)], res, [
#include <stddef.h>
#include <limits.h>
#define STYPE_MINIMUM(t) (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
#define STYPE_MAXIMUM(t) ((t) (~ (t) 0 - STYPE_MINIMUM (t)))
], result=?)
_AC_COMPUTE_INT([sizeof (ptrdiff_t) <= sizeof (int)], fits_in_int,
[#include <stddef.h>], result=?)
if test "$fits_in_int" = 1; then
dnl Even though PTRDIFF_MAX fits in an int, it must be of type
dnl 'long' if the type 'ptrdiff_t' is the same as 'long'.
AC_TRY_COMPILE([#include <stddef.h>
extern ptrdiff_t foo;
extern long foo;
], [], fits_in_int=0)
fi
if test -z "$result"; then
if test "$fits_in_int" = 1; then
result="$res"
else
result="$res"L
fi
else
dnl Shouldn't happen, but who knows...
result='((ptrdiff_t)(~(ptrdiff_t)0-(~(ptrdiff_t)0<<(sizeof(ptrdiff_t)*CHAR_BIT-1))))'
fi
fi
AC_MSG_RESULT([$result])
if test "$result" != yes; then
AC_DEFINE_UNQUOTED([PTRDIFF_MAX], [$result],
[Define as the maximum value of type 'ptrdiff_t', if the system doesn't
define it.])
fi
])
=========================================================================
*** modules/xsize 4 Nov 2003 12:06:18 -0000 1.1
--- modules/xsize 11 Nov 2003 11:50:18 -0000
***************
*** 4,9 ****
--- 4,10 ----
Files:
lib/xsize.h
m4/xsize.m4
+ m4/size_max.m4
Depends-on:
*** modules/xalloc 13 Oct 2003 06:07:11 -0000 1.7
--- modules/xalloc 11 Nov 2003 11:50:18 -0000
***************
*** 6,11 ****
--- 6,13 ----
lib/xmalloc.c
lib/xstrdup.c
m4/xalloc.m4
+ m4/size_max.m4
+ m4/ptrdiff_max.m4
Depends-on:
malloc
*** m4/xsize.m4 4 Nov 2003 12:06:17 -0000 1.1
--- m4/xsize.m4 11 Nov 2003 11:50:18 -0000
***************
*** 1,4 ****
! # xsize.m4 serial 1
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
--- 1,4 ----
! # xsize.m4 serial 2
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
***************
*** 9,13 ****
--- 9,14 ----
AC_DEFUN([gl_XSIZE],
[
dnl Prerequisites of lib/xsize.h.
+ AC_REQUIRE([gl_SIZE_MAX])
AC_CHECK_HEADERS(stdint.h)
])
*** m4/xalloc.m4 30 Oct 2003 06:33:40 -0000 1.6
--- m4/xalloc.m4 11 Nov 2003 11:50:18 -0000
***************
*** 1,4 ****
! # xalloc.m4 serial 6
dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
--- 1,4 ----
! # xalloc.m4 serial 7
dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
***************
*** 8,15 ****
--- 8,23 ----
AC_DEFUN([gl_XALLOC],
[
+ gl_PREREQ_XALLOC
gl_PREREQ_XMALLOC
gl_PREREQ_XSTRDUP
+ ])
+
+ # Prerequisites of lib/xalloc.h.
+ AC_DEFUN([gl_PREREQ_XALLOC], [
+ AC_REQUIRE([gl_SIZE_MAX])
+ AC_REQUIRE([gl_PTRDIFF_MAX])
+ :
])
# Prerequisites of lib/xmalloc.c.
*** lib/xsize.h 5 Nov 2003 11:36:59 -0000 1.2
--- lib/xsize.h 11 Nov 2003 11:50:18 -0000
***************
*** 27,35 ****
#if HAVE_STDINT_H
# include <stdint.h>
#endif
- #ifndef SIZE_MAX
- # define SIZE_MAX ((size_t) -1)
- #endif
/* The size of memory objects is often computed through expressions of
type size_t. Example:
--- 27,32 ----
*** lib/xalloc.h 10 Nov 2003 23:55:49 -0000 1.22
--- lib/xalloc.h 11 Nov 2003 11:50:18 -0000
***************
*** 20,26 ****
--- 20,30 ----
#ifndef XALLOC_H_
# define XALLOC_H_
+ /* Get size_t. */
# include <stddef.h>
+
+ /* Get SIZE_MAX, PTRDIFF_MAX. */
+ # include <limits.h>
# if HAVE_STDINT_H
# include <stdint.h>
# endif
***************
*** 74,83 ****
PTRDIFF_MAX < SIZE_MAX, so do not bother to test for
exactly-SIZE_MAX allocations on such hosts; this avoids a test and
branch when S is known to be 1. */
! # if defined PTRDIFF_MAX && PTRDIFF_MAX < SIZE_MAX
# define xalloc_oversized(n, s) (SIZE_MAX / (s) < (n))
! # else /* SIZE_MAX might not be defined, so avoid (SIZE_MAX - 1). */
! # define xalloc_oversized(n, s) ((size_t) -2 / (s) < (n))
# endif
/* These macros are deprecated; they will go away soon, and are retained
--- 78,87 ----
PTRDIFF_MAX < SIZE_MAX, so do not bother to test for
exactly-SIZE_MAX allocations on such hosts; this avoids a test and
branch when S is known to be 1. */
! # if PTRDIFF_MAX < SIZE_MAX
# define xalloc_oversized(n, s) (SIZE_MAX / (s) < (n))
! # else
! # define xalloc_oversized(n, s) ((SIZE_MAX - 1) / (s) < (n))
# endif
/* These macros are deprecated; they will go away soon, and are retained
- [Bug-gnulib] addition: size_max.m4, ptrdiff_max.m4,
Bruno Haible <=