1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by David Chisnall under sponsorship from 8 * the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #ifndef _XLOCALE_PRIVATE__H_ 35 #define _XLOCALE_PRIVATE__H_ 36 37 #include <xlocale.h> 38 #include <locale.h> 39 #include <stdlib.h> 40 #include <stdint.h> 41 #include <sys/types.h> 42 #include <machine/atomic.h> 43 #include "setlocale.h" 44 45 /** 46 * The XLC_ values are indexes into the components array. They are defined in 47 * the same order as the LC_ values in locale.h, but without the LC_ALL zero 48 * value. Translating from LC_X to XLC_X is done by subtracting one. 49 * 50 * Any reordering of this enum should ensure that these invariants are not 51 * violated. 52 */ 53 enum { 54 XLC_COLLATE = 0, 55 XLC_CTYPE, 56 XLC_MONETARY, 57 XLC_NUMERIC, 58 XLC_TIME, 59 XLC_MESSAGES, 60 XLC_LAST 61 }; 62 63 _Static_assert(XLC_LAST - XLC_COLLATE == 6, "XLC values should be contiguous"); 64 _Static_assert(XLC_COLLATE == LC_COLLATE - 1, 65 "XLC_COLLATE doesn't match the LC_COLLATE value."); 66 _Static_assert(XLC_CTYPE == LC_CTYPE - 1, 67 "XLC_CTYPE doesn't match the LC_CTYPE value."); 68 _Static_assert(XLC_MONETARY == LC_MONETARY - 1, 69 "XLC_MONETARY doesn't match the LC_MONETARY value."); 70 _Static_assert(XLC_NUMERIC == LC_NUMERIC - 1, 71 "XLC_NUMERIC doesn't match the LC_NUMERIC value."); 72 _Static_assert(XLC_TIME == LC_TIME - 1, 73 "XLC_TIME doesn't match the LC_TIME value."); 74 _Static_assert(XLC_MESSAGES == LC_MESSAGES - 1, 75 "XLC_MESSAGES doesn't match the LC_MESSAGES value."); 76 77 /** 78 * Header used for objects that are reference counted. Objects may optionally 79 * have a destructor associated, which is responsible for destroying the 80 * structure. Global / static versions of the structure should have no 81 * destructor set - they can then have their reference counts manipulated as 82 * normal, but will not do anything with them. 83 * 84 * The header stores a retain count - objects are assumed to have a reference 85 * count of 1 when they are created, but the retain count is 0. When the 86 * retain count is less than 0, they are freed. 87 */ 88 struct xlocale_refcounted { 89 /** Number of references to this component. */ 90 long retain_count; 91 /** Function used to destroy this component, if one is required*/ 92 void(*destructor)(void*); 93 }; 94 /** 95 * Header for a locale component. All locale components must begin with this 96 * header. 97 */ 98 struct xlocale_component { 99 struct xlocale_refcounted header; 100 /** Name of the locale used for this component. */ 101 char locale[ENCODING_LEN+1]; 102 }; 103 104 /** 105 * xlocale structure, stores per-thread locale information. 106 */ 107 struct _xlocale { 108 struct xlocale_refcounted header; 109 /** Components for the locale. */ 110 struct xlocale_component *components[XLC_LAST]; 111 /** Flag indicating if components[XLC_MONETARY] has changed since the 112 * last call to localeconv_l() with this locale. */ 113 int monetary_locale_changed; 114 /** Flag indicating whether this locale is actually using a locale for 115 * LC_MONETARY (1), or if it should use the C default instead (0). */ 116 int using_monetary_locale; 117 /** Flag indicating if components[XLC_NUMERIC] has changed since the 118 * last call to localeconv_l() with this locale. */ 119 int numeric_locale_changed; 120 /** Flag indicating whether this locale is actually using a locale for 121 * LC_NUMERIC (1), or if it should use the C default instead (0). */ 122 int using_numeric_locale; 123 /** Flag indicating whether this locale is actually using a locale for 124 * LC_TIME (1), or if it should use the C default instead (0). */ 125 int using_time_locale; 126 /** Flag indicating whether this locale is actually using a locale for 127 * LC_MESSAGES (1), or if it should use the C default instead (0). */ 128 int using_messages_locale; 129 /** The structure to be returned from localeconv_l() for this locale. */ 130 struct lconv lconv; 131 /** Buffer used by nl_langinfo_l() */ 132 char *csym; 133 }; 134 135 /** 136 * Increments the reference count of a reference-counted structure. 137 */ 138 __attribute__((unused)) static void* 139 xlocale_retain(void *val) 140 { 141 struct xlocale_refcounted *obj = val; 142 atomic_add_long(&(obj->retain_count), 1); 143 return (val); 144 } 145 /** 146 * Decrements the reference count of a reference-counted structure, freeing it 147 * if this is the last reference, calling its destructor if it has one. 148 */ 149 __attribute__((unused)) static void 150 xlocale_release(void *val) 151 { 152 struct xlocale_refcounted *obj = val; 153 long count; 154 155 count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1; 156 if (count < 0 && obj->destructor != NULL) 157 obj->destructor(obj); 158 } 159 160 /** 161 * Load functions. Each takes the name of a locale and a pointer to the data 162 * to be initialised as arguments. Two special values are allowed for the 163 */ 164 extern void* __collate_load(const char*, locale_t); 165 extern void* __ctype_load(const char*, locale_t); 166 extern void* __messages_load(const char*, locale_t); 167 extern void* __monetary_load(const char*, locale_t); 168 extern void* __numeric_load(const char*, locale_t); 169 extern void* __time_load(const char*, locale_t); 170 171 extern struct _xlocale __xlocale_global_locale; 172 extern struct _xlocale __xlocale_C_locale; 173 174 /** 175 * Caches the rune table in TLS for fast access. 176 */ 177 void __set_thread_rune_locale(locale_t loc); 178 /** 179 * Flag indicating whether a per-thread locale has been set. If no per-thread 180 * locale has ever been set, then we always use the global locale. 181 */ 182 extern int __has_thread_locale; 183 #ifndef __NO_TLS 184 /** 185 * The per-thread locale. Avoids the need to use pthread lookup functions when 186 * getting the per-thread locale. 187 */ 188 extern _Thread_local locale_t __thread_locale; 189 190 /** 191 * Returns the current locale for this thread, or the global locale if none is 192 * set. The caller does not have to free the locale. The return value from 193 * this call is not guaranteed to remain valid after the locale changes. As 194 * such, this should only be called within libc functions. 195 */ 196 static inline locale_t __get_locale(void) 197 { 198 199 if (!__has_thread_locale) { 200 return (&__xlocale_global_locale); 201 } 202 return (__thread_locale ? __thread_locale : &__xlocale_global_locale); 203 } 204 #else 205 locale_t __get_locale(void); 206 #endif 207 208 /** 209 * Two magic values are allowed for locale_t objects. NULL and -1. This 210 * function maps those to the real locales that they represent. 211 */ 212 static inline locale_t get_real_locale(locale_t locale) 213 { 214 switch ((intptr_t)locale) { 215 case 0: return (&__xlocale_C_locale); 216 case -1: return (&__xlocale_global_locale); 217 default: return (locale); 218 } 219 } 220 221 /** 222 * Replace a placeholder locale with the real global or thread-local locale_t. 223 */ 224 #define FIX_LOCALE(l) (l = get_real_locale(l)) 225 226 #endif 227