1227753Stheraven/*-
2227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
3227753Stheraven * All rights reserved.
4227753Stheraven *
5227753Stheraven * This software was developed by David Chisnall under sponsorship from
6227753Stheraven * the FreeBSD Foundation.
7227753Stheraven *
8227753Stheraven * Redistribution and use in source and binary forms, with or without
9227753Stheraven * modification, are permitted provided that the following conditions
10227753Stheraven * are met:
11232498Stheraven * 1. Redistributions of source code must retain the above copyright
12232498Stheraven *    notice, this list of conditions and the following disclaimer.
13232498Stheraven * 2. Redistributions in binary form must reproduce the above copyright
14232498Stheraven *    notice, this list of conditions and the following disclaimer in the
15232498Stheraven *    documentation and/or other materials provided with the distribution.
16227753Stheraven *
17227753Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18227753Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19227753Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20232498Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21227753Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22227753Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23227753Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24227753Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25227753Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26227753Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27227753Stheraven * SUCH DAMAGE.
28227753Stheraven *
29227753Stheraven * $FreeBSD$
30227753Stheraven */
31227753Stheraven
32227753Stheraven#ifndef _XLOCALE_PRIVATE__H_
33227753Stheraven#define _XLOCALE_PRIVATE__H_
34227753Stheraven
35227753Stheraven#include <xlocale.h>
36227753Stheraven#include <locale.h>
37227753Stheraven#include <stdlib.h>
38227753Stheraven#include <stdint.h>
39227753Stheraven#include <sys/types.h>
40227753Stheraven#include <machine/atomic.h>
41227753Stheraven#include "setlocale.h"
42227753Stheraven
43227753Stheravenenum {
44227753Stheraven	XLC_COLLATE = 0,
45227753Stheraven	XLC_CTYPE,
46227753Stheraven	XLC_MONETARY,
47227753Stheraven	XLC_NUMERIC,
48227753Stheraven	XLC_TIME,
49227753Stheraven	XLC_MESSAGES,
50227753Stheraven	XLC_LAST
51227753Stheraven};
52227753Stheraven
53227753Stheraven
54227753Stheraven/**
55227753Stheraven * Header used for objects that are reference counted.  Objects may optionally
56227753Stheraven * have a destructor associated, which is responsible for destroying the
57227753Stheraven * structure.  Global / static versions of the structure should have no
58227753Stheraven * destructor set - they can then have their reference counts manipulated as
59227753Stheraven * normal, but will not do anything with them.
60227753Stheraven *
61227753Stheraven * The header stores a retain count - objects are assumed to have a reference
62227753Stheraven * count of 1 when they are created, but the retain count is 0.  When the
63227753Stheraven * retain count is less than 0, they are freed.
64227753Stheraven */
65227753Stheravenstruct xlocale_refcounted {
66227753Stheraven	/** Number of references to this component.  */
67227753Stheraven	long retain_count;
68227753Stheraven	/** Function used to destroy this component, if one is required*/
69227753Stheraven	void(*destructor)(void*);
70227753Stheraven};
71227753Stheraven/**
72227753Stheraven * Header for a locale component.  All locale components must begin with this
73227753Stheraven * header.
74227753Stheraven */
75227753Stheravenstruct xlocale_component {
76227753Stheraven	struct xlocale_refcounted header;
77227753Stheraven	/** Name of the locale used for this component. */
78227753Stheraven	char locale[ENCODING_LEN+1];
79227753Stheraven};
80227753Stheraven
81227753Stheraven/**
82227753Stheraven * xlocale structure, stores per-thread locale information.
83227753Stheraven */
84227753Stheravenstruct _xlocale {
85227753Stheraven	struct xlocale_refcounted header;
86227753Stheraven	/** Components for the locale.  */
87227753Stheraven	struct xlocale_component *components[XLC_LAST];
88232498Stheraven	/** Flag indicating if components[XLC_MONETARY] has changed since the
89232498Stheraven	 * last call to localeconv_l() with this locale. */
90227753Stheraven	int monetary_locale_changed;
91227753Stheraven	/** Flag indicating whether this locale is actually using a locale for
92227753Stheraven	 * LC_MONETARY (1), or if it should use the C default instead (0). */
93227753Stheraven	int using_monetary_locale;
94232498Stheraven	/** Flag indicating if components[XLC_NUMERIC] has changed since the
95232498Stheraven	 * last call to localeconv_l() with this locale. */
96227753Stheraven	int numeric_locale_changed;
97227753Stheraven	/** Flag indicating whether this locale is actually using a locale for
98227753Stheraven	 * LC_NUMERIC (1), or if it should use the C default instead (0). */
99227753Stheraven	int using_numeric_locale;
100227753Stheraven	/** Flag indicating whether this locale is actually using a locale for
101227753Stheraven	 * LC_TIME (1), or if it should use the C default instead (0). */
102227753Stheraven	int using_time_locale;
103227753Stheraven	/** Flag indicating whether this locale is actually using a locale for
104227753Stheraven	 * LC_MESSAGES (1), or if it should use the C default instead (0). */
105227753Stheraven	int using_messages_locale;
106227753Stheraven	/** The structure to be returned from localeconv_l() for this locale. */
107227753Stheraven	struct lconv lconv;
108227753Stheraven	/** Persistent state used by mblen() calls. */
109227753Stheraven	__mbstate_t mblen;
110227753Stheraven	/** Persistent state used by mbrlen() calls. */
111227753Stheraven	__mbstate_t mbrlen;
112250883Sed	/** Persistent state used by mbrtoc16() calls. */
113250883Sed	__mbstate_t mbrtoc16;
114250883Sed	/** Persistent state used by mbrtoc32() calls. */
115250883Sed	__mbstate_t mbrtoc32;
116227753Stheraven	/** Persistent state used by mbrtowc() calls. */
117227753Stheraven	__mbstate_t mbrtowc;
118227753Stheraven	/** Persistent state used by mbsnrtowcs() calls. */
119227753Stheraven	__mbstate_t mbsnrtowcs;
120227753Stheraven	/** Persistent state used by mbsrtowcs() calls. */
121227753Stheraven	__mbstate_t mbsrtowcs;
122227753Stheraven	/** Persistent state used by mbtowc() calls. */
123227753Stheraven	__mbstate_t mbtowc;
124250883Sed	/** Persistent state used by c16rtomb() calls. */
125250883Sed	__mbstate_t c16rtomb;
126250883Sed	/** Persistent state used by c32rtomb() calls. */
127250883Sed	__mbstate_t c32rtomb;
128227753Stheraven	/** Persistent state used by wcrtomb() calls. */
129227753Stheraven	__mbstate_t wcrtomb;
130227753Stheraven	/** Persistent state used by wcsnrtombs() calls. */
131227753Stheraven	__mbstate_t wcsnrtombs;
132227753Stheraven	/** Persistent state used by wcsrtombs() calls. */
133227753Stheraven	__mbstate_t wcsrtombs;
134227753Stheraven	/** Persistent state used by wctomb() calls. */
135227753Stheraven	__mbstate_t wctomb;
136227753Stheraven	/** Buffer used by nl_langinfo_l() */
137227753Stheraven	char *csym;
138227753Stheraven};
139227753Stheraven
140227753Stheraven/**
141227753Stheraven * Increments the reference count of a reference-counted structure.
142227753Stheraven */
143227753Stheraven__attribute__((unused)) static void*
144227753Stheravenxlocale_retain(void *val)
145227753Stheraven{
146227753Stheraven	struct xlocale_refcounted *obj = val;
147227753Stheraven	atomic_add_long(&(obj->retain_count), 1);
148227753Stheraven	return (val);
149227753Stheraven}
150227753Stheraven/**
151227753Stheraven * Decrements the reference count of a reference-counted structure, freeing it
152227753Stheraven * if this is the last reference, calling its destructor if it has one.
153227753Stheraven */
154227753Stheraven__attribute__((unused)) static void
155227753Stheravenxlocale_release(void *val)
156227753Stheraven{
157227753Stheraven	struct xlocale_refcounted *obj = val;
158297790Spfg	long count;
159297790Spfg
160297790Spfg	count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1;
161297790Spfg	if (count < 0 && obj->destructor != NULL)
162297790Spfg		obj->destructor(obj);
163227753Stheraven}
164227753Stheraven
165227753Stheraven/**
166227753Stheraven * Load functions.  Each takes the name of a locale and a pointer to the data
167227753Stheraven * to be initialised as arguments.  Two special values are allowed for the
168227753Stheraven */
169227753Stheravenextern void* __collate_load(const char*, locale_t);
170227753Stheravenextern void* __ctype_load(const char*, locale_t);
171227753Stheravenextern void* __messages_load(const char*, locale_t);
172227753Stheravenextern void* __monetary_load(const char*, locale_t);
173227753Stheravenextern void* __numeric_load(const char*, locale_t);
174227753Stheravenextern void* __time_load(const char*, locale_t);
175227753Stheraven
176227753Stheravenextern struct _xlocale __xlocale_global_locale;
177227753Stheravenextern struct _xlocale __xlocale_C_locale;
178227753Stheraven
179227753Stheraven/**
180232498Stheraven * Caches the rune table in TLS for fast access.
181232498Stheraven */
182232498Stheravenvoid __set_thread_rune_locale(locale_t loc);
183232498Stheraven/**
184232498Stheraven * Flag indicating whether a per-thread locale has been set.  If no per-thread
185232498Stheraven * locale has ever been set, then we always use the global locale.
186232498Stheraven */
187232498Stheravenextern int __has_thread_locale;
188232498Stheraven#ifndef __NO_TLS
189232498Stheraven/**
190232498Stheraven * The per-thread locale.  Avoids the need to use pthread lookup functions when
191232498Stheraven * getting the per-thread locale.
192232498Stheraven */
193232498Stheravenextern _Thread_local locale_t __thread_locale;
194232498Stheraven
195232498Stheraven/**
196227753Stheraven * Returns the current locale for this thread, or the global locale if none is
197227753Stheraven * set.  The caller does not have to free the locale.  The return value from
198227753Stheraven * this call is not guaranteed to remain valid after the locale changes.  As
199227753Stheraven * such, this should only be called within libc functions.
200227753Stheraven */
201233173Stheravenstatic inline locale_t __get_locale(void)
202232498Stheraven{
203232498Stheraven
204232498Stheraven	if (!__has_thread_locale) {
205232498Stheraven		return (&__xlocale_global_locale);
206232498Stheraven	}
207232498Stheraven	return (__thread_locale ? __thread_locale : &__xlocale_global_locale);
208232498Stheraven}
209232498Stheraven#else
210227753Stheravenlocale_t __get_locale(void);
211232498Stheraven#endif
212227753Stheraven
213227753Stheraven/**
214227753Stheraven * Two magic values are allowed for locale_t objects.  NULL and -1.  This
215227753Stheraven * function maps those to the real locales that they represent.
216227753Stheraven */
217227753Stheravenstatic inline locale_t get_real_locale(locale_t locale)
218227753Stheraven{
219227753Stheraven	switch ((intptr_t)locale) {
220227753Stheraven		case 0: return (&__xlocale_C_locale);
221227753Stheraven		case -1: return (&__xlocale_global_locale);
222227753Stheraven		default: return (locale);
223227753Stheraven	}
224227753Stheraven}
225227753Stheraven
226227753Stheraven/**
227227753Stheraven * Replace a placeholder locale with the real global or thread-local locale_t.
228227753Stheraven */
229227753Stheraven#define FIX_LOCALE(l) (l = get_real_locale(l))
230227753Stheraven
231227753Stheraven#endif
232