* lib/stdlib.c: New file.
* modules/stdlib (Files, lib_SOURCES): Add it.
(Depends-on): Add extern-inline.
* lib/stdlib.in.h: Use _GL_INLINE_HEADER_BEGIN, _GL_INLINE_HEADER_END.
(_GL_STDLIB_INLINE): New macro.
(gl_MB_CUR_MAX): Make it _GL_STDLIB_INLINE not static inline,
so that it can be called from extern inline functions.
Redo implementation to help GCC better optimize common expressions
like (MB_CUR_MAX == 1).
---
 ChangeLog       | 11 +++++++++++
 lib/stdlib.c    |  3 +++
 lib/stdlib.in.h | 16 ++++++++++++----
 modules/stdlib  |  5 +++--
 4 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 lib/stdlib.c

diff --git a/ChangeLog b/ChangeLog
index 8c54555f8f..761061e2b5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2024-11-04  Paul Eggert  <egg...@cs.ucla.edu>
 
+       stdlib: make MB_CUR_MAX usable from extern inline
+       * lib/stdlib.c: New file.
+       * modules/stdlib (Files, lib_SOURCES): Add it.
+       (Depends-on): Add extern-inline.
+       * lib/stdlib.in.h: Use _GL_INLINE_HEADER_BEGIN, _GL_INLINE_HEADER_END.
+       (_GL_STDLIB_INLINE): New macro.
+       (gl_MB_CUR_MAX): Make it _GL_STDLIB_INLINE not static inline,
+       so that it can be called from extern inline functions.
+       Redo implementation to help GCC better optimize common expressions
+       like (MB_CUR_MAX == 1).
+
        realloc-posix: set CHERI bounds
        * lib/ialloc.h, lib/xmalloc.c [__CHERI_PURE_CAPABILITY__]:
        Do nothing special, as realloc and reallocarray
diff --git a/lib/stdlib.c b/lib/stdlib.c
new file mode 100644
index 0000000000..be331ffbda
--- /dev/null
+++ b/lib/stdlib.c
@@ -0,0 +1,3 @@
+#include <config.h>
+#define _GL_STDLIB_INLINE _GL_EXTERN_INLINE
+#include <stdlib.h>
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index 855038327e..0d5c076d06 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -54,7 +54,7 @@
 
 /* This file uses _Noreturn, _GL_ATTRIBUTE_DEALLOC, _GL_ATTRIBUTE_MALLOC,
    _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NOTHROW, _GL_ATTRIBUTE_PURE,
-   GNULIB_POSIXCHECK, HAVE_RAW_DECL_*.  */
+   _GL_INLINE_HEADER_BEGIN, GNULIB_POSIXCHECK, HAVE_RAW_DECL_*.  */
 #if !_GL_CONFIG_H_INCLUDED
  #error "Please include config.h first."
 #endif
@@ -130,6 +130,11 @@ struct random_data
 # include <string>
 #endif
 
+_GL_INLINE_HEADER_BEGIN
+#ifndef _GL_STDLIB_INLINE
+# define _GL_STDLIB_INLINE _GL_INLINE
+#endif
+
 /* _GL_ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
    that can be freed by passing them as the Ith argument to the
    function F.  */
@@ -740,11 +745,12 @@ _GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant 
everywhere - "
 /* Return maximum number of bytes of a multibyte character.  */
 #if @REPLACE_MB_CUR_MAX@
 # if !GNULIB_defined_MB_CUR_MAX
-static inline
-int gl_MB_CUR_MAX (void)
+_GL_STDLIB_INLINE int
+gl_MB_CUR_MAX (void)
 {
   /* Turn the value 3 to the value 4, as needed for the UTF-8 encoding.  */
-  return MB_CUR_MAX + (MB_CUR_MAX == 3);
+  int gl_mb_cur_max = MB_CUR_MAX;
+  return gl_mb_cur_max == 3 ? 4 : gl_mb_cur_max;
 }
 #  undef MB_CUR_MAX
 #  define MB_CUR_MAX gl_MB_CUR_MAX ()
@@ -1967,6 +1973,8 @@ _GL_CXXALIASWARN (wctomb);
 #endif
 
 
+_GL_INLINE_HEADER_END
+
 #endif /* _@GUARD_PREFIX@_STDLIB_H */
 #endif /* _@GUARD_PREFIX@_STDLIB_H */
 #endif
diff --git a/modules/stdlib b/modules/stdlib
index 3dffbf56bb..ddf6d5725f 100644
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -2,6 +2,7 @@ Description:
 A GNU-like <stdlib.h>.
 
 Files:
+lib/stdlib.c
 lib/stdlib.in.h
 m4/stdlib_h.m4
 m4/locale-en.m4
@@ -9,6 +10,7 @@ m4/codeset.m4
 
 Depends-on:
 gen-header
+extern-inline
 include_next
 snippet/arg-nonnull
 snippet/c++defs
@@ -24,9 +26,8 @@ AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += stdlib.h
+lib_SOURCES += stdlib.c
 
-# We need the following in order to create <stdlib.h> when the system
-# doesn't have one that works with the given compiler.
 stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
   $(_NORETURN_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 @NMD@  $(AM_V_GEN)$(MKDIR_P) '%reldir%'
-- 
2.43.0


Reply via email to