The code depends on the presence of uintptr_t, not on whether ptrdiff_t is narrower than a pointer, so use C macros that reflect this. Also, clean up the namespace in the typical GCC-like case * lib/obstack.in.h (_OBSTACK_UINTPTR_TYPE): New macro. Include <stdint.h> only if needed (i.e., not GCC-like). (__PTR_ALIGN): Use new macro instead of relying on __GL_SMALL_PTRDIFF_T__ and directly on uintptr_t. * m4/obstack.m4 (gl_FUNC_OBSTACK): Simplify by removing __GL_SMALL_PTRDIFF_T__. --- ChangeLog | 12 ++++++++++++ lib/obstack.in.h | 23 ++++++++++++++++------- m4/obstack.m4 | 23 +---------------------- 3 files changed, 29 insertions(+), 29 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 7448c09ebc..cc7e7e3256 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2025-05-05 Paul Eggert <egg...@cs.ucla.edu> + obstack: use presence of uintptr_t + The code depends on the presence of uintptr_t, + not on whether ptrdiff_t is narrower than a pointer, + so use C macros that reflect this. + Also, clean up the namespace in the typical GCC-like case + * lib/obstack.in.h (_OBSTACK_UINTPTR_TYPE): New macro. + Include <stdint.h> only if needed (i.e., not GCC-like). + (__PTR_ALIGN): Use new macro instead of relying on + __GL_SMALL_PTRDIFF_T__ and directly on uintptr_t. + * m4/obstack.m4 (gl_FUNC_OBSTACK): Simplify by removing + __GL_SMALL_PTRDIFF_T__. + obstack: define PTR_INT_TYPE * lib/obstack.in.h (PTR_INT_TYPE): Define, for compatibility with glibc even though we don’t need the type for anything diff --git a/lib/obstack.in.h b/lib/obstack.in.h index 6b51ebde5d..aacd29edb4 100644 --- a/lib/obstack.in.h +++ b/lib/obstack.in.h @@ -125,7 +125,17 @@ # define PTR_INT_TYPE ptrdiff_t #endif -#include <stdint.h> /* For uintptr_t. */ +/* An integer type wide enough to hold a pointer value, + if such a type is available. */ +#ifdef __UINTPTR_TYPE__ +# define _OBSTACK_UINTPTR_TYPE __UINTPTR_TYPE__ +#elif defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ +# include <stdint.h> +# ifdef UINTPTR_MAX +# define _OBSTACK_UINTPTR_TYPE uintptr_t +# endif +#endif + #include <string.h> /* For memcpy, size_t. */ /* These macros highlight the places where this implementation @@ -144,18 +154,17 @@ # define _OBSTACK_CHUNK_CONTENTS_SIZE 4 #endif -/* __PTR_ALIGN(B, P, A) returns the result of aligning P to the next multiple - of A + 1. B must be the base of an object addressed by P. B and P must be - of type char *. A + 1 must be a power of 2. - If ptrdiff_t is narrower than a pointer (e.g., the AS/400), play it +/* If B is the base of an object addressed by P, return the result of + aligning P to the next multiple of A + 1. B and P must be of type + char *. A + 1 must be a power of 2. With no uintptr_t, play it safe and compute the alignment relative to B. Otherwise, use the faster strategy of computing the alignment through uintptr_t. */ -#if defined __GL_SMALL_PTRDIFF_T__ +#ifndef _OBSTACK_UINTPTR_TYPE # define __PTR_ALIGN(B, P, A) \ ((B) + (((P) - (B) + (A)) & ~(A))) #else # define __PTR_ALIGN(B, P, A) \ - ((P) + ((- (uintptr_t) (P)) & (A))) + ((P) + ((- (_OBSTACK_UINTPTR_TYPE) (P)) & (A))) #endif #ifndef __attribute_pure__ diff --git a/m4/obstack.m4 b/m4/obstack.m4 index 60948c9d5c..15f667332a 100644 --- a/m4/obstack.m4 +++ b/m4/obstack.m4 @@ -1,5 +1,5 @@ # obstack.m4 -# serial 1 +# serial 2 dnl Copyright 1996-2025 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -48,25 +48,4 @@ AC_DEFUN([gl_FUNC_OBSTACK] GL_GENERATE_OBSTACK_H=false fi AC_SUBST([REPLACE_OBSTACK]) - - dnl Test whether a 'ptrdiff_t' has at least as many bits as a pointer. - AC_CACHE_CHECK([whether ptrdiff_t is at least as large as a pointer], - [gl_cv_ptrdiff_t_wide_enough], - [AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[#include <stddef.h> - typedef int check[2 * (sizeof (ptrdiff_t) >= sizeof (void *)) - 1]; - ]], - [[]])], - [gl_cv_ptrdiff_t_wide_enough=yes], - [gl_cv_ptrdiff_t_wide_enough=no]) - ]) - if test $gl_cv_ptrdiff_t_wide_enough = yes; then - SMALL_PTRDIFF_T=0 - else - SMALL_PTRDIFF_T=1 - fi - dnl SMALL_PTRDIFF_T is expected to be 1 only on old / exotic platforms - dnl such as Windows 3.1 and AS/400. - AC_SUBST([SMALL_PTRDIFF_T]) ]) -- 2.49.0