]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
obstack: simplify default size, alignment
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 5 May 2025 21:02:21 +0000 (14:02 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 5 May 2025 21:16:23 +0000 (14:16 -0700)
* 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
lib/obstack.c
modules/obstack

index eb0ba5d16615e0bc7bd6d8bf32be41b08fd72669..20f9e7bc3c944759edf7c5f1b78b414b2172330a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2025-05-05  Paul Eggert  <eggert@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.
index b319c8f7d3a4dfe948af28ef2c2a0711731b7868..1fac1cd404b97d6e8bd6d22040245bc4a350b606 100644 (file)
@@ -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;
     }
 
index 4f62f64bba77e2280f44e85c0165b409b749cad7..f22a7a1cc48d0826717b513e110f81fdee26fa8e 100644 (file)
@@ -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]