xlocale_private.h revision 233173
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: head/lib/libc/locale/xlocale_private.h 233173 2012-03-19 11:47:37Z theraven $
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;
112227753Stheraven	/** Persistent state used by mbrtowc() calls. */
113227753Stheraven	__mbstate_t mbrtowc;
114227753Stheraven	/** Persistent state used by mbsnrtowcs() calls. */
115227753Stheraven	__mbstate_t mbsnrtowcs;
116227753Stheraven	/** Persistent state used by mbsrtowcs() calls. */
117227753Stheraven	__mbstate_t mbsrtowcs;
118227753Stheraven	/** Persistent state used by mbtowc() calls. */
119227753Stheraven	__mbstate_t mbtowc;
120227753Stheraven	/** Persistent state used by wcrtomb() calls. */
121227753Stheraven	__mbstate_t wcrtomb;
122227753Stheraven	/** Persistent state used by wcsnrtombs() calls. */
123227753Stheraven	__mbstate_t wcsnrtombs;
124227753Stheraven	/** Persistent state used by wcsrtombs() calls. */
125227753Stheraven	__mbstate_t wcsrtombs;
126227753Stheraven	/** Persistent state used by wctomb() calls. */
127227753Stheraven	__mbstate_t wctomb;
128227753Stheraven	/** Buffer used by nl_langinfo_l() */
129227753Stheraven	char *csym;
130227753Stheraven};
131227753Stheraven
132227753Stheraven/**
133227753Stheraven * Increments the reference count of a reference-counted structure.
134227753Stheraven */
135227753Stheraven__attribute__((unused)) static void*
136227753Stheravenxlocale_retain(void *val)
137227753Stheraven{
138227753Stheraven	struct xlocale_refcounted *obj = val;
139227753Stheraven	atomic_add_long(&(obj->retain_count), 1);
140227753Stheraven	return (val);
141227753Stheraven}
142227753Stheraven/**
143227753Stheraven * Decrements the reference count of a reference-counted structure, freeing it
144227753Stheraven * if this is the last reference, calling its destructor if it has one.
145227753Stheraven */
146227753Stheraven__attribute__((unused)) static void
147227753Stheravenxlocale_release(void *val)
148227753Stheraven{
149227753Stheraven	struct xlocale_refcounted *obj = val;
150227753Stheraven	long count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1;
151227753Stheraven	if (count < 0) {
152227753Stheraven		if (0 != obj->destructor) {
153227753Stheraven			obj->destructor(obj);
154227753Stheraven		}
155227753Stheraven	}
156227753Stheraven}
157227753Stheraven
158227753Stheraven/**
159227753Stheraven * Load functions.  Each takes the name of a locale and a pointer to the data
160227753Stheraven * to be initialised as arguments.  Two special values are allowed for the
161227753Stheraven */
162227753Stheravenextern void* __collate_load(const char*, locale_t);
163227753Stheravenextern void* __ctype_load(const char*, locale_t);
164227753Stheravenextern void* __messages_load(const char*, locale_t);
165227753Stheravenextern void* __monetary_load(const char*, locale_t);
166227753Stheravenextern void* __numeric_load(const char*, locale_t);
167227753Stheravenextern void* __time_load(const char*, locale_t);
168227753Stheraven
169227753Stheravenextern struct _xlocale __xlocale_global_locale;
170227753Stheravenextern struct _xlocale __xlocale_C_locale;
171227753Stheraven
172227753Stheraven/**
173232498Stheraven * Caches the rune table in TLS for fast access.
174232498Stheraven */
175232498Stheravenvoid __set_thread_rune_locale(locale_t loc);
176232498Stheraven/**
177232498Stheraven * Flag indicating whether a per-thread locale has been set.  If no per-thread
178232498Stheraven * locale has ever been set, then we always use the global locale.
179232498Stheraven */
180232498Stheravenextern int __has_thread_locale;
181232498Stheraven#ifndef __NO_TLS
182232498Stheraven/**
183232498Stheraven * The per-thread locale.  Avoids the need to use pthread lookup functions when
184232498Stheraven * getting the per-thread locale.
185232498Stheraven */
186232498Stheravenextern _Thread_local locale_t __thread_locale;
187232498Stheraven
188232498Stheraven/**
189227753Stheraven * Returns the current locale for this thread, or the global locale if none is
190227753Stheraven * set.  The caller does not have to free the locale.  The return value from
191227753Stheraven * this call is not guaranteed to remain valid after the locale changes.  As
192227753Stheraven * such, this should only be called within libc functions.
193227753Stheraven */
194233173Stheravenstatic inline locale_t __get_locale(void)
195232498Stheraven{
196232498Stheraven
197232498Stheraven	if (!__has_thread_locale) {
198232498Stheraven		return (&__xlocale_global_locale);
199232498Stheraven	}
200232498Stheraven	return (__thread_locale ? __thread_locale : &__xlocale_global_locale);
201232498Stheraven}
202232498Stheraven#else
203227753Stheravenlocale_t __get_locale(void);
204232498Stheraven#endif
205227753Stheraven
206227753Stheraven/**
207227753Stheraven * Two magic values are allowed for locale_t objects.  NULL and -1.  This
208227753Stheraven * function maps those to the real locales that they represent.
209227753Stheraven */
210227753Stheravenstatic inline locale_t get_real_locale(locale_t locale)
211227753Stheraven{
212227753Stheraven	switch ((intptr_t)locale) {
213227753Stheraven		case 0: return (&__xlocale_C_locale);
214227753Stheraven		case -1: return (&__xlocale_global_locale);
215227753Stheraven		default: return (locale);
216227753Stheraven	}
217227753Stheraven}
218227753Stheraven
219227753Stheraven/**
220227753Stheraven * Replace a placeholder locale with the real global or thread-local locale_t.
221227753Stheraven */
222227753Stheraven#define FIX_LOCALE(l) (l = get_real_locale(l))
223227753Stheraven
224227753Stheraven#endif
225