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