>From 060f7b7c07539493d70d971bbdfd0d64491faeed Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 17 Sep 2026 14:11:48 -0400 Subject: [PATCH 16/18] move global_locale object out of libc struct this is mainly needed because it will need to contain lconv, without pulling locale.h into every file that needs libc.h. but also there is no reason for the global locale to be directly accessible with the same base pointer as the libc object; it's almost always accessed via the thread pointer in code paths where efficiency is a consideration. --- src/env/__init_tls.c | 2 +- src/internal/libc.h | 9 ++------- src/internal/locale_impl.h | 4 ++++ src/locale/duplocale.c | 2 +- src/locale/global_locale.c | 3 +++ src/locale/setlocale.c | 10 +++++----- src/locale/uselocale.c | 2 +- src/thread/pthread_create.c | 2 +- 8 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 src/locale/global_locale.c diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c index a93141ed..99bb8381 100644 --- a/src/env/__init_tls.c +++ b/src/env/__init_tls.c @@ -20,7 +20,7 @@ int __init_tp(void *p) if (!r) libc.can_do_threads = 1; td->detach_state = DT_JOINABLE; td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock); - td->locale = &libc.global_locale; + td->locale = &__global_locale; td->robust_list.head = &td->robust_list.head; td->sysinfo = __sysinfo; td->next = td->prev = td; diff --git a/src/internal/libc.h b/src/internal/libc.h index 619bba86..d9ec3b93 100644 --- a/src/internal/libc.h +++ b/src/internal/libc.h @@ -5,12 +5,6 @@ #include #include -struct __locale_map; - -struct __locale_struct { - const struct __locale_map *cat[6]; -}; - struct tls_module { struct tls_module *next; void *image; @@ -27,9 +21,10 @@ struct __libc { struct tls_module *tls_head; size_t tls_size, tls_align, tls_cnt; size_t page_size; - struct __locale_struct global_locale; }; +extern hidden struct __locale_struct __global_locale; + #ifndef PAGE_SIZE #define PAGE_SIZE libc.page_size #endif diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h index 65e3d245..9e30afd9 100644 --- a/src/internal/locale_impl.h +++ b/src/internal/locale_impl.h @@ -26,6 +26,10 @@ struct __locale_map { struct collation collate; }; +struct __locale_struct { + const struct __locale_map *cat[6]; +}; + extern hidden volatile int __locale_lock[1]; extern hidden const struct __locale_map __c_dot_utf8; diff --git a/src/locale/duplocale.c b/src/locale/duplocale.c index 5ce33ae6..bc7a4f60 100644 --- a/src/locale/duplocale.c +++ b/src/locale/duplocale.c @@ -12,7 +12,7 @@ locale_t __duplocale(locale_t old) { locale_t new = malloc(sizeof *new); if (!new) return 0; - if (old == LC_GLOBAL_LOCALE) old = &libc.global_locale; + if (old == LC_GLOBAL_LOCALE) old = &__global_locale; *new = *old; return new; } diff --git a/src/locale/global_locale.c b/src/locale/global_locale.c new file mode 100644 index 00000000..998142fe --- /dev/null +++ b/src/locale/global_locale.c @@ -0,0 +1,3 @@ +#include "locale_impl.h" + +struct __locale_struct __global_locale; diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c index 360c4437..a14b0ea6 100644 --- a/src/locale/setlocale.c +++ b/src/locale/setlocale.c @@ -40,15 +40,15 @@ char *setlocale(int cat, const char *name) } tmp_locale.cat[i] = lm; } - libc.global_locale = tmp_locale; + __global_locale = tmp_locale; } char *s = buf; const char *part; int same = 0; for (i=0; iname : "C"; size_t l = strlen(part); memcpy(s, part, l); @@ -66,9 +66,9 @@ char *setlocale(int cat, const char *name) UNLOCK(__locale_lock); return 0; } - libc.global_locale.cat[cat] = lm; + __global_locale.cat[cat] = lm; } else { - lm = libc.global_locale.cat[cat]; + lm = __global_locale.cat[cat]; } char *ret = lm ? (char *)lm->name : "C"; diff --git a/src/locale/uselocale.c b/src/locale/uselocale.c index 0fc5ecbc..be077b47 100644 --- a/src/locale/uselocale.c +++ b/src/locale/uselocale.c @@ -6,7 +6,7 @@ locale_t __uselocale(locale_t new) { pthread_t self = __pthread_self(); locale_t old = self->locale; - locale_t global = &libc.global_locale; + locale_t global = &__global_locale; if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new; diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c index 087f6206..dfb02ddd 100644 --- a/src/thread/pthread_create.c +++ b/src/thread/pthread_create.c @@ -318,7 +318,7 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att new->guard_size = guard; new->self = new; new->tsd = (void *)tsd; - new->locale = &libc.global_locale; + new->locale = &__global_locale; if (attr._a_detach) { new->detach_state = DT_DETACHED; } else { -- 2.21.0