2 * Copyright (c) 2005, 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _XLOCALE_PRIVATE_H_
25 #define _XLOCALE_PRIVATE_H_
27 #include <sys/cdefs.h>
28 #define __DARWIN_XLOCALE_PRIVATE
30 #undef __DARWIN_XLOCALE_PRIVATE
33 #include <libkern/OSAtomic.h>
35 #include <pthread/tsd_private.h>
38 #include "setlocale.h"
41 #include "lmessages.h"
42 #include "lmonetary.h"
44 #include "timelocal.h"
45 #include <TargetConditionals.h>
48 #define MB_CUR_MAX (__current_locale()->__lc_ctype->__mb_cur_max)
50 #define MB_CUR_MAX_L(x) ((x)->__lc_ctype->__mb_cur_max)
52 typedef void (*__free_extra_t
)(void *);
54 #define XPERMANENT ((__free_extra_t)-1)
55 #define XMAGIC 0x786c6f63616c6530LL /* 'xlocale0' */
57 #define __STRUCT_COMMON \
59 __free_extra_t __free_extra;
61 struct __xlocale_st_collate
{
63 char __encoding
[ENCODING_LEN
+ 1];
64 struct __collate_st_info __info
;
65 struct __collate_st_subst
*__substitute_table
[COLL_WEIGHTS_MAX
];
66 struct __collate_st_chain_pri
*__chain_pri_table
;
67 struct __collate_st_large_char_pri
*__large_char_pri_table
;
68 struct __collate_st_char_pri __char_pri_table
[UCHAR_MAX
+ 1];
70 struct __xlocale_st_runelocale
{
72 char __ctype_encoding
[ENCODING_LEN
+ 1];
75 size_t (*__mbrtowc
)(wchar_t * __restrict
, const char * __restrict
,
76 size_t, __darwin_mbstate_t
* __restrict
, struct _xlocale
*);
77 int (*__mbsinit
)(const __darwin_mbstate_t
*, struct _xlocale
*);
78 size_t (*__mbsnrtowcs
)(wchar_t * __restrict
, const char ** __restrict
,
79 size_t, size_t, __darwin_mbstate_t
* __restrict
, struct _xlocale
*);
80 size_t (*__wcrtomb
)(char * __restrict
, wchar_t,
81 __darwin_mbstate_t
* __restrict
, struct _xlocale
*);
82 size_t (*__wcsnrtombs
)(char * __restrict
, const wchar_t ** __restrict
,
83 size_t, size_t, __darwin_mbstate_t
* __restrict
, struct _xlocale
*);
85 _RuneLocale _CurrentRuneLocale
;
87 struct __xlocale_st_ldpart
{
92 * the next four structures must have the first three fields of the same
93 * as the _xlocale_st_ldpart structure above.
95 struct __xlocale_st_messages
{
97 char *_messages_locale_buf
;
98 struct lc_messages_T _messages_locale
;
100 struct __xlocale_st_monetary
{
102 char *_monetary_locale_buf
;
103 struct lc_monetary_T _monetary_locale
;
105 struct __xlocale_st_numeric
{
107 char *_numeric_locale_buf
;
108 struct lc_numeric_T _numeric_locale
;
110 struct __xlocale_st_time
{
112 char *_time_locale_buf
;
113 struct lc_time_T _time_locale
;
116 /* the extended locale structure */
117 /* values for __numeric_fp_cvt */
118 #define LC_NUMERIC_FP_UNINITIALIZED 0
119 #define LC_NUMERIC_FP_SAME_LOCALE 1
120 #define LC_NUMERIC_FP_USE_LOCALE 2
123 /* The item(s) before __magic are not copied when duplicating locale_t's */
124 __STRUCT_COMMON
/* only used for locale_t's in __lc_numeric_loc */
125 /* 10 independent mbstate_t buffers! */
126 __darwin_mbstate_t __mbs_mblen
;
127 __darwin_mbstate_t __mbs_mbrlen
;
128 __darwin_mbstate_t __mbs_mbrtowc
;
129 __darwin_mbstate_t __mbs_mbsnrtowcs
;
130 __darwin_mbstate_t __mbs_mbsrtowcs
;
131 __darwin_mbstate_t __mbs_mbtowc
;
132 __darwin_mbstate_t __mbs_wcrtomb
;
133 __darwin_mbstate_t __mbs_wcsnrtombs
;
134 __darwin_mbstate_t __mbs_wcsrtombs
;
135 __darwin_mbstate_t __mbs_wctomb
;
136 os_unfair_lock __lock
;
137 /* magic (Here up to the end is copied when duplicating locale_t's) */
140 unsigned char __collate_load_error
;
141 unsigned char __collate_substitute_nontrivial
;
142 unsigned char _messages_using_locale
;
143 unsigned char _monetary_using_locale
;
144 unsigned char _numeric_using_locale
;
145 unsigned char _time_using_locale
;
146 unsigned char __mlocale_changed
;
147 unsigned char __nlocale_changed
;
148 unsigned char __numeric_fp_cvt
;
150 struct __xlocale_st_collate
*__lc_collate
;
152 struct __xlocale_st_runelocale
*__lc_ctype
;
154 struct __xlocale_st_messages
*__lc_messages
;
156 struct __xlocale_st_monetary
*__lc_monetary
;
158 struct __xlocale_st_numeric
*__lc_numeric
;
159 struct _xlocale
*__lc_numeric_loc
;
161 struct __xlocale_st_time
*__lc_time
;
163 struct lconv __lc_localeconv
;
166 #define DEFAULT_CURRENT_LOCALE(x) \
168 (x) = __current_locale(); \
169 } else if ((x) == LC_GLOBAL_LOCALE) { \
170 (x) = &__global_locale; \
173 #define NORMALIZE_LOCALE(x) if ((x) == NULL) { \
175 } else if ((x) == LC_GLOBAL_LOCALE) { \
176 (x) = &__global_locale; \
179 #define XL_LOCK(x) os_unfair_lock_lock(&(x)->__lock);
180 #define XL_RELEASE(x) if ((x) && (x)->__free_extra != XPERMANENT && OSAtomicDecrement32Barrier(&(x)->__refcount) == 0) { \
181 if ((x)->__free_extra) \
182 (*(x)->__free_extra)((x)); \
186 #define XL_RETAIN(x) if ((x) && (x)->__free_extra != XPERMANENT) { OSAtomicIncrement32Barrier(&(x)->__refcount); }
187 #define XL_UNLOCK(x) os_unfair_lock_unlock(&(x)->__lock);
189 __attribute__((visibility("hidden")))
190 extern struct __xlocale_st_runelocale _DefaultRuneXLocale
;
192 __attribute__((visibility("hidden")))
193 extern struct _xlocale __global_locale
;
195 __attribute__((visibility("hidden")))
196 extern pthread_key_t __locale_key
;
200 void __ldpart_free_extra(struct __xlocale_st_ldpart
*);
201 locale_t
__numeric_ctype(locale_t
);
202 void __xlocale_init(void);
204 static inline __attribute__((always_inline
)) locale_t
205 __current_locale(void)
207 #if TARGET_IPHONE_SIMULATOR
208 /* <rdar://problem/14136256> Crash in _objc_inform for duplicate class name during simulator launch
209 * TODO: Remove after the simulator's libSystem is initialized properly.
211 if (__locale_key
== (pthread_key_t
)-1) {
212 return &__global_locale
;
215 void *__thread_locale
;
216 if (_pthread_has_direct_tsd()) {
217 __thread_locale
= _pthread_getspecific_direct(__locale_key
);
219 __thread_locale
= pthread_getspecific(__locale_key
);
221 return (__thread_locale
? (locale_t
)__thread_locale
: &__global_locale
);
224 static inline __attribute__((always_inline
)) locale_t
225 __locale_ptr(locale_t __loc
)
227 NORMALIZE_LOCALE(__loc
);
233 #endif /* _XLOCALE_PRIVATE_H_ */