From: Bruno Haible <bruno@clisp.org>
Date: Mon, 17 Jul 2023 17:11:38 +0000 (+0200)
Subject: mbchar: Reduce size of 'struct mbchar'.
X-Git-Tag: v1.0~1086
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=49d66352f71f4eeba3169a56862ee041e124a136;p=gnulib.git

mbchar: Reduce size of 'struct mbchar'.

* modules/mbfile (configure.ac): Define GNULIB_MBFILE as an indicator.
* lib/mbchar.h (MBCHAR_BUF_SIZE): Set to 4.
(struct mbchar): Disable member 'buf' if the module 'mbfile' is not in
use.
---

diff --git a/ChangeLog b/ChangeLog
index 5ec78e6bd4..b6a3321d7a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-07-17  Bruno Haible  <bruno@clisp.org>
+
+	mbchar: Reduce size of 'struct mbchar'.
+	* modules/mbfile (configure.ac): Define GNULIB_MBFILE as an indicator.
+	* lib/mbchar.h (MBCHAR_BUF_SIZE): Set to 4.
+	(struct mbchar): Disable member 'buf' if the module 'mbfile' is not in
+	use.
+
 2023-07-17  Bruno Haible  <bruno@clisp.org>
 
 	mbszero: Fix for Minix.
diff --git a/lib/mbchar.h b/lib/mbchar.h
index 82c373f47e..d1900b5a51 100644
--- a/lib/mbchar.h
+++ b/lib/mbchar.h
@@ -156,7 +156,9 @@ _GL_INLINE_HEADER_BEGIN
 # define MBCHAR_INLINE _GL_INLINE
 #endif
 
-#define MBCHAR_BUF_SIZE 24
+/* The longest multibyte characters, nowadays, are 4 bytes long.
+   Regardless of the values of MB_CUR_MAX and MB_LEN_MAX.  */
+#define MBCHAR_BUF_SIZE 4
 
 struct mbchar
 {
@@ -164,7 +166,9 @@ struct mbchar
   size_t bytes;         /* number of bytes of current character, > 0 */
   bool wc_valid;        /* true if wc is a valid 32-bit wide character */
   char32_t wc;          /* if wc_valid: the current character */
+#if defined GNULIB_MBFILE
   char buf[MBCHAR_BUF_SIZE]; /* room for the bytes, used for file input only */
+#endif
 };
 
 /* EOF (not a real character) is represented with bytes = 0 and
diff --git a/modules/mbfile b/modules/mbfile
index af3674ea79..09593fcc3f 100644
--- a/modules/mbfile
+++ b/modules/mbfile
@@ -18,6 +18,10 @@ stdbool
 
 configure.ac:
 gl_MBFILE
+dnl Do not use gl_MODULE_INDICATOR([mbfile]) here: we don't want 'struct mbchar'
+dnl to have a different size in lib/ than in tests/.
+AC_DEFINE([GNULIB_MBFILE], [1],
+  [Define to 1 if the gnulib module 'mbfile' is in use.])
 
 Makefile.am:
 lib_SOURCES += mbfile.h mbfile.c