* lib/obstack.c (MAX, DEFAULT_ALIGNMENT, DEFAULT_ROUNDING): Remove.
(_obstack_begin_worker): Use __alignof__ (max_align_t)
instead of calculating it by hand, possibly incorrectly
on oddball platforms.  Estimate a good size of 4000 bytes
rather than using a no-longer-valid value.
* modules/obstack (Depends-on): Add stddef-h, for max_align_t.
---
 ChangeLog       |  8 ++++++++
 lib/obstack.c   | 36 +++---------------------------------
 modules/obstack |  1 +
 3 files changed, 12 insertions(+), 33 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eb0ba5d166..20f9e7bc3c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2025-05-05  Paul Eggert  <egg...@cs.ucla.edu>
 
+       obstack: simplify default size, alignment
+       * lib/obstack.c (MAX, DEFAULT_ALIGNMENT, DEFAULT_ROUNDING): Remove.
+       (_obstack_begin_worker): Use __alignof__ (max_align_t)
+       instead of calculating it by hand, possibly incorrectly
+       on oddball platforms.  Estimate a good size of 4000 bytes
+       rather than using a no-longer-valid value.
+       * modules/obstack (Depends-on): Add stddef-h, for max_align_t.
+
        obstack: use signed chunk sizes
        * lib/obstack.in.h (_OBSTACK_CHUNK_SIZE_T) [!__GL_GNULIB_HEADER]:
        Now long, not unsigned long, since that’s what glibc does.
diff --git a/lib/obstack.c b/lib/obstack.c
index b319c8f7d3..1fac1cd404 100644
--- a/lib/obstack.c
+++ b/lib/obstack.c
@@ -57,24 +57,6 @@ align_size_up (size_t mask, size_t size)
   return size + (mask & -size);
 }
 
-#ifndef MAX
-# define MAX(a,b) ((a) > (b) ? (a) : (b))
-#endif
-
-/* Determine default alignment.  */
-
-/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
-   But in fact it might be less smart and round addresses to as much as
-   DEFAULT_ROUNDING.  So we prepare for it to do that.
-
-   DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h.  */
-#define DEFAULT_ALIGNMENT MAX (__alignof__ (long double),                    \
-                               MAX (__alignof__ (uintmax_t),                 \
-                                    __alignof__ (void *)))
-#define DEFAULT_ROUNDING MAX (sizeof (long double),                          \
-                               MAX (sizeof (uintmax_t),                        
      \
-                                    sizeof (void *)))
-
 /* Call functions with either the traditional malloc/free calling
    interface, or the mmalloc/mfree interface (that adds an extra first
    argument), based on the value of use_extra_arg.  */
@@ -111,7 +93,7 @@ _obstack_begin_worker (struct obstack *h,
   struct _obstack_chunk *chunk; /* points to new chunk */
 
   if (alignment == 0)
-    alignment = DEFAULT_ALIGNMENT;
+    alignment = __alignof__ (max_align_t);
 
   /* The minimum size to request from the allocator, such that the
      result is guaranteed to have enough room to start with the struct
@@ -131,20 +113,8 @@ _obstack_begin_worker (struct obstack *h,
       size = aligned_prefix_size;
 
       /* For speed in the typical case, allocate at least a "good" size.  */
-
-      /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
-         Use the values for range checking, because if range checking is off,
-         the extra bytes won't be missed terribly, but if range checking is on
-         and we used a larger request, a whole extra 4096 bytes would be
-         allocated.
-
-         These number are irrelevant to the new GNU malloc.  I suspect it is
-         less sensitive to the size of the request.  */
-      int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
-                    + 4 + DEFAULT_ROUNDING - 1)
-                   & ~(DEFAULT_ROUNDING - 1));
-      int good_size = 4096 - extra;
-      if (0 <= good_size && size < good_size)
+      int good_size = 4000;
+      if (size < good_size)
         size = good_size;
     }
 
diff --git a/modules/obstack b/modules/obstack
index 4f62f64bba..f22a7a1cc4 100644
--- a/modules/obstack
+++ b/modules/obstack
@@ -13,6 +13,7 @@ flexmember      [test $HAVE_OBSTACK = 0 || test 
$REPLACE_OBSTACK = 1]
 gettext-h       [test $HAVE_OBSTACK = 0 || test $REPLACE_OBSTACK = 1]
 gnulib-i18n     [test $HAVE_OBSTACK = 0 || test $REPLACE_OBSTACK = 1]
 exitfail        [test $HAVE_OBSTACK = 0 || test $REPLACE_OBSTACK = 1]
+stddef-h        [test $HAVE_OBSTACK = 0 || test $REPLACE_OBSTACK = 1]
 stdint-h        [test $HAVE_OBSTACK = 0 || test $REPLACE_OBSTACK = 1]
 stdlib-h        [test $HAVE_OBSTACK = 0 || test $REPLACE_OBSTACK = 1]
 
-- 
2.49.0


Reply via email to