[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnulib] addition: size_max.m4, ptrdiff_max.m4
From: |
Paul Eggert |
Subject: |
Re: [Bug-gnulib] addition: size_max.m4, ptrdiff_max.m4 |
Date: |
12 Nov 2003 23:45:28 -0800 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 |
Bruno Haible <address@hidden> writes:
> 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=?)
Ouch. This is a hassle. Perhaps Autoconf should be fixed so that
_AC_COMPUTE_INT can handle values outside the range
LONG_MIN..LONG_MAX? That would be better than having to do this sort
of thing by hand all the time. (This wouldn't fix the immediate
problem of course; I'm thinking more long-term.)
I thought about the problem with xalloc.h a bit more, and came up with
a different heuristic that should be just as good in practice, and
does not rely on SIZE_MAX or PTRDIFF_MAX, so we can remove that
dependency. This also avoids the need for xalloc.h to include
<limits.h> and <stdint.h>, which is a minor win. Here's what I
installed:
2003-11-12 Paul Eggert <address@hidden>
* lib/xalloc.h: Do not include <limits.h> or <stdint.h>.
(xalloc_oversized): Use sizeof (ptrdiff_t) and sizeof (size_t) for
the heuristic, rather than PTRDIFF_MAX and SIZE_MAX. This
heuristic is just as accurate as far as we know, and it removes a
dependency on size_max.m4 and ptrdiff_max.m4.
* m4/xalloc.m4 (gl_PREREQ_XALLOC): Do not require gl_SIZE_MAX or
gl_PTRDIFF_MAX.
* modules/xalloc (Files): Undo latest change, since xalloc.h
no longer needs SIZE_MAX or PTRDIFF_MAX.
Index: lib/xalloc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xalloc.h,v
retrieving revision 1.23
diff -p -u -r1.23 xalloc.h
--- lib/xalloc.h 11 Nov 2003 11:58:43 -0000 1.23
+++ lib/xalloc.h 13 Nov 2003 07:17:18 -0000
@@ -20,15 +20,8 @@
#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
-
# ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
# define __attribute__(x)
@@ -75,14 +68,11 @@ char *xstrdup (const char *str);
calculations, so the conservative dividend to use here is
SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
However, malloc (SIZE_MAX) fails on all known hosts where
- PTRDIFF_MAX < SIZE_MAX, so do not bother to test for
+ sizeof (ptrdiff_t) <= sizeof (size_t), 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
+# define xalloc_oversized(n, s) \
+ ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
/* These macros are deprecated; they will go away soon, and are retained
temporarily only to ease conversion to the functions described above. */
Index: m4/xalloc.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/xalloc.m4,v
retrieving revision 1.7
diff -p -u -r1.7 xalloc.m4
--- m4/xalloc.m4 11 Nov 2003 11:58:43 -0000 1.7
+++ m4/xalloc.m4 13 Nov 2003 07:17:19 -0000
@@ -1,4 +1,4 @@
-# xalloc.m4 serial 7
+# xalloc.m4 serial 8
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
@@ -15,8 +15,6 @@ AC_DEFUN([gl_XALLOC],
# Prerequisites of lib/xalloc.h.
AC_DEFUN([gl_PREREQ_XALLOC], [
- AC_REQUIRE([gl_SIZE_MAX])
- AC_REQUIRE([gl_PTRDIFF_MAX])
:
])
Index: modules/xalloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/xalloc,v
retrieving revision 1.8
diff -p -u -r1.8 xalloc
--- modules/xalloc 11 Nov 2003 11:58:43 -0000 1.8
+++ modules/xalloc 13 Nov 2003 07:17:19 -0000
@@ -6,8 +6,6 @@ lib/xalloc.h
lib/xmalloc.c
lib/xstrdup.c
m4/xalloc.m4
-m4/size_max.m4
-m4/ptrdiff_max.m4
Depends-on:
malloc