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