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: head/lib/libc/locale/xlocale_private.h 326193 2017-11-25 17:12:48Z pfg $
30 */
31
32 #ifndef __LIBC
33 #error "Userland tools should not use this private header."
34 #endif
35
36 #ifndef _XLOCALE_PRIVATE__H_
37 #define _XLOCALE_PRIVATE__H_
38
39 #include <xlocale.h>
40 #include <locale.h>
41 #include <stdlib.h>
42 #include <stdint.h>
43 #include <sys/types.h>
44 #include <machine/atomic.h>
45 #include <machine/wchar.h> /* for __mbstate_t, XXX use mbstate_t here? */
46 #include "setlocale.h"
47
48 /**
49 * The XLC_ values are indexes into the components array. They are defined in
50 * the same order as the LC_ values in locale.h, but without the LC_ALL zero
51 * value. Translating from LC_X to XLC_X is done by subtracting one.
52 *
53 * Any reordering of this enum should ensure that these invariants are not
54 * violated.
55 */
56 enum {
57 XLC_COLLATE = 0,
58 XLC_CTYPE,
59 XLC_MONETARY,
60 XLC_NUMERIC,
61 XLC_TIME,
62 XLC_MESSAGES,
63 XLC_LAST
64 };
65
66 _Static_assert(XLC_LAST - XLC_COLLATE == 6, "XLC values should be contiguous");
67 _Static_assert(XLC_COLLATE == LC_COLLATE - 1,
68 "XLC_COLLATE doesn't match the LC_COLLATE value.");
69 _Static_assert(XLC_CTYPE == LC_CTYPE - 1,
70 "XLC_CTYPE doesn't match the LC_CTYPE value.");
71 _Static_assert(XLC_MONETARY == LC_MONETARY - 1,
72 "XLC_MONETARY doesn't match the LC_MONETARY value.");
73 _Static_assert(XLC_NUMERIC == LC_NUMERIC - 1,
74 "XLC_NUMERIC doesn't match the LC_NUMERIC value.");
75 _Static_assert(XLC_TIME == LC_TIME - 1,
76 "XLC_TIME doesn't match the LC_TIME value.");
77 _Static_assert(XLC_MESSAGES == LC_MESSAGES - 1,
78 "XLC_MESSAGES doesn't match the LC_MESSAGES value.");
79
80 /**
81 * Header used for objects that are reference counted. Objects may optionally
82 * have a destructor associated, which is responsible for destroying the
83 * structure. Global / static versions of the structure should have no
84 * destructor set - they can then have their reference counts manipulated as
85 * normal, but will not do anything with them.
86 *
87 * The header stores a retain count - objects are assumed to have a reference
88 * count of 1 when they are created, but the retain count is 0. When the
89 * retain count is less than 0, they are freed.
90 */
91 struct xlocale_refcounted {
92 /** Number of references to this component. */
93 long retain_count;
94 /** Function used to destroy this component, if one is required*/
95 void(*destructor)(void*);
96 };
97 /**
98 * Header for a locale component. All locale components must begin with this
99 * header.
100 */
101 struct xlocale_component {
102 struct xlocale_refcounted header;
103 /** Name of the locale used for this component. */
104 char locale[ENCODING_LEN+1];
105 };
106
107 /**
108 * xlocale structure, stores per-thread locale information.
109 */
110 struct _xlocale {
111 struct xlocale_refcounted header;
112 /** Components for the locale. */
113 struct xlocale_component *components[XLC_LAST];
114 /** Flag indicating if components[XLC_MONETARY] has changed since the
115 * last call to localeconv_l() with this locale. */
116 int monetary_locale_changed;
117 /** Flag indicating whether this locale is actually using a locale for
118 * LC_MONETARY (1), or if it should use the C default instead (0). */
119 int using_monetary_locale;
120 /** Flag indicating if components[XLC_NUMERIC] has changed since the
121 * last call to localeconv_l() with this locale. */
122 int numeric_locale_changed;
123 /** Flag indicating whether this locale is actually using a locale for
124 * LC_NUMERIC (1), or if it should use the C default instead (0). */
125 int using_numeric_locale;
126 /** Flag indicating whether this locale is actually using a locale for
127 * LC_TIME (1), or if it should use the C default instead (0). */
128 int using_time_locale;
129 /** Flag indicating whether this locale is actually using a locale for
130 * LC_MESSAGES (1), or if it should use the C default instead (0). */
131 int using_messages_locale;
132 /** The structure to be returned from localeconv_l() for this locale. */
133 struct lconv lconv;
134 /** Persistent state used by mblen() calls. */
135 __mbstate_t mblen;
136 /** Persistent state used by mbrlen() calls. */
137 __mbstate_t mbrlen;
138 /** Persistent state used by mbrtoc16() calls. */
139 __mbstate_t mbrtoc16;
140 /** Persistent state used by mbrtoc32() calls. */
141 __mbstate_t mbrtoc32;
142 /** Persistent state used by mbrtowc() calls. */
143 __mbstate_t mbrtowc;
144 /** Persistent state used by mbsnrtowcs() calls. */
145 __mbstate_t mbsnrtowcs;
146 /** Persistent state used by mbsrtowcs() calls. */
147 __mbstate_t mbsrtowcs;
148 /** Persistent state used by mbtowc() calls. */
149 __mbstate_t mbtowc;
150 /** Persistent state used by c16rtomb() calls. */
151 __mbstate_t c16rtomb;
152 /** Persistent state used by c32rtomb() calls. */
153 __mbstate_t c32rtomb;
154 /** Persistent state used by wcrtomb() calls. */
155 __mbstate_t wcrtomb;
156 /** Persistent state used by wcsnrtombs() calls. */
157 __mbstate_t wcsnrtombs;
158 /** Persistent state used by wcsrtombs() calls. */
159 __mbstate_t wcsrtombs;
160 /** Persistent state used by wctomb() calls. */
161 __mbstate_t wctomb;
162 /** Buffer used by nl_langinfo_l() */
163 char *csym;
164 };
165
166 /**
167 * Increments the reference count of a reference-counted structure.
168 */
169 __attribute__((unused)) static void*
xlocale_retain(void * val)170 xlocale_retain(void *val)
171 {
172 struct xlocale_refcounted *obj = val;
173 atomic_add_long(&(obj->retain_count), 1);
174 return (val);
175 }
176 /**
177 * Decrements the reference count of a reference-counted structure, freeing it
178 * if this is the last reference, calling its destructor if it has one.
179 */
180 __attribute__((unused)) static void
xlocale_release(void * val)181 xlocale_release(void *val)
182 {
183 struct xlocale_refcounted *obj = val;
184 long count;
185
186 count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1;
187 if (count < 0 && obj->destructor != NULL)
188 obj->destructor(obj);
189 }
190
191 /**
192 * Load functions. Each takes the name of a locale and a pointer to the data
193 * to be initialised as arguments. Two special values are allowed for the
194 */
195 extern void* __collate_load(const char*, locale_t);
196 extern void* __ctype_load(const char*, locale_t);
197 extern void* __messages_load(const char*, locale_t);
198 extern void* __monetary_load(const char*, locale_t);
199 extern void* __numeric_load(const char*, locale_t);
200 extern void* __time_load(const char*, locale_t);
201
202 extern struct _xlocale __xlocale_global_locale;
203 extern struct _xlocale __xlocale_C_locale;
204
205 /**
206 * Caches the rune table in TLS for fast access.
207 */
208 void __set_thread_rune_locale(locale_t loc);
209 /**
210 * Flag indicating whether a per-thread locale has been set. If no per-thread
211 * locale has ever been set, then we always use the global locale.
212 */
213 extern int __has_thread_locale;
214 /**
215 * The per-thread locale. Avoids the need to use pthread lookup functions when
216 * getting the per-thread locale.
217 */
218 extern __thread locale_t __thread_locale;
219
220 /**
221 * Returns the current locale for this thread, or the global locale if none is
222 * set. The caller does not have to free the locale. The return value from
223 * this call is not guaranteed to remain valid after the locale changes. As
224 * such, this should only be called within libc functions.
225 */
__get_locale(void)226 static inline locale_t __get_locale(void)
227 {
228
229 if (!__has_thread_locale) {
230 return (&__xlocale_global_locale);
231 }
232 return (__thread_locale ? __thread_locale : &__xlocale_global_locale);
233 }
234
235 /**
236 * Two magic values are allowed for locale_t objects. NULL and -1. This
237 * function maps those to the real locales that they represent.
238 */
get_real_locale(locale_t locale)239 static inline locale_t get_real_locale(locale_t locale)
240 {
241 switch ((intptr_t)locale) {
242 case 0: return (&__xlocale_C_locale);
243 case -1: return (&__xlocale_global_locale);
244 default: return (locale);
245 }
246 }
247
248 /**
249 * Replace a placeholder locale with the real global or thread-local locale_t.
250 */
251 #define FIX_LOCALE(l) (l = get_real_locale(l))
252
253 #endif
254