From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [RFC 4/4] string: Move strerror_l pointer to tls_internal.h
Date: Wed, 13 May 2020 17:26:30 -0300 [thread overview]
Message-ID: <20200513202630.2123238-5-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20200513202630.2123238-1-adhemerval.zanella@linaro.org>
Similar to strsignal_l, move the per-thread message pointer to
tls_internal.h (which on Linux is allocated on struct per-thread).
Checked on x86-64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
and s390x-linux-gnu.
---
string/strerror_l.c | 34 ++++++++++++---------------
sysdeps/generic/tls_internal-struct.h | 1 +
sysdeps/mach/strerror_l.c | 24 +++++++++----------
3 files changed, 27 insertions(+), 32 deletions(-)
diff --git a/string/strerror_l.c b/string/strerror_l.c
index 40e7d0e896..baa489dd90 100644
--- a/string/strerror_l.c
+++ b/string/strerror_l.c
@@ -15,15 +15,11 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
+#include <stdio.h>
#include <libintl.h>
#include <locale.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-#include <sys/param.h>
-#include <libc-symbols.h>
-
-static __thread char *last_value;
+#include <tls_internal.h>
static const char *
@@ -40,23 +36,23 @@ translate (const char *str, locale_t loc)
char *
strerror_l (int errnum, locale_t loc)
{
- if (__glibc_unlikely (errnum < 0 || errnum >= _sys_nerr_internal
- || _sys_errlist_internal[errnum] == NULL))
- {
- free (last_value);
- if (__asprintf (&last_value, "%s%d",
- translate ("Unknown error ", loc), errnum) == -1)
- last_value = NULL;
-
- return last_value;
- }
-
- return (char *) translate (_sys_errlist_internal[errnum], loc);
+ char *r = __strerror_lookup (errnum);
+ if (r != NULL)
+ return (char *) translate (r, loc);
+
+ struct tls_internal_t *tls_internal = __glibc_tls_internal ();
+ free (tls_internal->strerror_l_buf);
+ if (__asprintf (&tls_internal->strerror_l_buf, "%s%d",
+ translate ("Unknown error ", loc), errnum) == -1)
+ tls_internal->strerror_l_buf = NULL;
+
+ return tls_internal->strerror_l_buf;
}
void
__strerror_thread_freeres (void)
{
- free (last_value);
+ free (__glibc_tls_internal()->strerror_l_buf);
}
text_set_element (__libc_subfreeres, __strerror_thread_freeres);
+
diff --git a/sysdeps/generic/tls_internal-struct.h b/sysdeps/generic/tls_internal-struct.h
index 4416228757..a5ae278f81 100644
--- a/sysdeps/generic/tls_internal-struct.h
+++ b/sysdeps/generic/tls_internal-struct.h
@@ -30,6 +30,7 @@ struct tls_internal_t
char strsignal_buf[strsignal_str_len + INT_STRLEN_BOUND (int) + 1];
char *strsignal_l_buf;
char strerror_buf[strerror_str_len + INT_STRLEN_BOUND (int) + 1];
+ char *strerror_l_buf;
};
#define TLS_INTERNAL_STRERROR_LEN \
diff --git a/sysdeps/mach/strerror_l.c b/sysdeps/mach/strerror_l.c
index f514af341e..d6a69c3784 100644
--- a/sysdeps/mach/strerror_l.c
+++ b/sysdeps/mach/strerror_l.c
@@ -24,10 +24,7 @@
#include <mach/error.h>
#include <errorlib.h>
#include <sys/param.h>
-#include <libc-symbols.h>
-
-
-static __thread char *last_value;
+#include <tls_internal.h>
static const char *
@@ -56,15 +53,16 @@ strerror_l (int errnum, locale_t loc)
sub = err_get_sub (errnum);
code = err_get_code (errnum);
+ struct tls_internal_t *tls_internal = __glibc_tls_internal ();
if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
{
- free (last_value);
- if (__asprintf (&last_value, "%s%X",
+ free (tls_internal->strerror_l_buf);
+ if (__asprintf (&tls_internal->strerror_l_buf, "%s%X",
translate ("Error in unknown error system: ", loc),
errnum) == -1)
- last_value = NULL;
+ tls_internal->strerror_l_buf = NULL;
- return last_value;
+ return tls_internal->strerror_l_buf;
}
es = &__mach_error_systems[system];
@@ -74,14 +72,14 @@ strerror_l (int errnum, locale_t loc)
if (code >= es->subsystem[sub].max_code)
{
- free (last_value);
- if (__asprintf (&last_value, "%s%s %d",
+ free (tls_internal->strerror_l_buf);
+ if (__asprintf (&tls_internal->strerror_l_buf, "%s%s %d",
translate ("Unknown error ", loc),
translate (es->subsystem[sub].subsys_name, loc),
errnum) == -1)
- last_value = NULL;
+ tls_internal->strerror_l_buf = NULL;
- return last_value;
+ return tls_internal->strerror_l_buf;
}
return (char *) translate (es->subsystem[sub].codes[code], loc);
@@ -91,6 +89,6 @@ strerror_l (int errnum, locale_t loc)
void
__strerror_thread_freeres (void)
{
- free (last_value);
+ free (__glibc_tls_internal()->strerror_l_buf);
}
text_set_element (__libc_subfreeres, __strerror_thread_freeres);
--
2.25.1
next prev parent reply other threads:[~2020-05-13 20:26 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-13 20:26 [RFC 0/4] Make strsignal and strerror async-signal-safe Adhemerval Zanella
2020-05-13 20:26 ` [RFC 1/4] string: Make strsignal async-signal-safe Adhemerval Zanella
2020-05-14 7:15 ` Andreas Schwab
2020-05-13 20:26 ` [RFC 2/4] string: Add strsignal_l Adhemerval Zanella
2020-05-14 0:16 ` Joseph Myers
2020-05-13 20:26 ` [RFC 3/4] string: Make strerror async-signal-safe Adhemerval Zanella
2020-05-14 7:12 ` Andreas Schwab
2020-05-13 20:26 ` Adhemerval Zanella [this message]
2020-05-14 12:05 ` [RFC 0/4] Make strsignal and " Florian Weimer
2020-05-14 12:47 ` Adhemerval Zanella
2020-05-14 15:11 ` Zack Weinberg
2020-05-14 16:42 ` Adhemerval Zanella
2020-05-14 18:39 ` Florian Weimer
2020-05-14 15:16 ` Florian Weimer
2020-05-14 16:28 ` Adhemerval Zanella
2020-05-14 16:40 ` Florian Weimer
2020-05-14 16:46 ` Adhemerval Zanella
2020-05-14 16:51 ` Florian Weimer
2020-05-14 16:55 ` Adhemerval Zanella
2020-05-14 17:02 ` Florian Weimer
2020-05-14 17:34 ` Adhemerval Zanella
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200513202630.2123238-5-adhemerval.zanella@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).