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