]> git.saurik.com Git - apple/libc.git/blob - locale/xlocale_private.h
Libc-498.1.5.tar.gz
[apple/libc.git] / locale / xlocale_private.h
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef _XLOCALE_PRIVATE_H_
25 #define _XLOCALE_PRIVATE_H_
26
27 #include <sys/cdefs.h>
28 #define __DARWIN_XLOCALE_PRIVATE
29 #include <xlocale.h>
30 #undef __DARWIN_XLOCALE_PRIVATE
31 #include <stdlib.h>
32 #include <locale.h>
33 #include <libkern/OSAtomic.h>
34 #include <pthread.h>
35 #include <limits.h>
36 #include "setlocale.h"
37 #include "collate.h"
38 #include "runetype.h"
39 #include "lmessages.h"
40 #include "lmonetary.h"
41 #include "lnumeric.h"
42 #include "timelocal.h"
43
44 #undef MB_CUR_MAX
45 #define MB_CUR_MAX (__current_locale()->__lc_ctype->__mb_cur_max)
46 #undef MB_CUR_MAX_L
47 #define MB_CUR_MAX_L(x) ((x)->__lc_ctype->__mb_cur_max)
48
49 extern int __is_threaded;
50
51 typedef void (*__free_extra_t)(void *);
52
53 #define XPERMANENT ((__free_extra_t)-1)
54 #define XMAGIC 0x786c6f63616c6530LL /* 'xlocale0' */
55
56 #define __STRUCT_COMMON \
57 int32_t __refcount; \
58 __free_extra_t __free_extra;
59
60 struct __xlocale_st_collate {
61 __STRUCT_COMMON
62 char __encoding[ENCODING_LEN + 1];
63 struct __collate_st_info __info;
64 struct __collate_st_subst *__substitute_table[COLL_WEIGHTS_MAX];
65 struct __collate_st_chain_pri *__chain_pri_table;
66 struct __collate_st_large_char_pri *__large_char_pri_table;
67 struct __collate_st_char_pri __char_pri_table[UCHAR_MAX + 1];
68 };
69 struct __xlocale_st_runelocale {
70 __STRUCT_COMMON
71 char __ctype_encoding[ENCODING_LEN + 1];
72 int __mb_cur_max;
73 size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
74 size_t, __darwin_mbstate_t * __restrict, struct _xlocale *);
75 int (*__mbsinit)(const __darwin_mbstate_t *, struct _xlocale *);
76 size_t (*__mbsnrtowcs)(wchar_t * __restrict, const char ** __restrict,
77 size_t, size_t, __darwin_mbstate_t * __restrict, struct _xlocale *);
78 size_t (*__wcrtomb)(char * __restrict, wchar_t,
79 __darwin_mbstate_t * __restrict, struct _xlocale *);
80 size_t (*__wcsnrtombs)(char * __restrict, const wchar_t ** __restrict,
81 size_t, size_t, __darwin_mbstate_t * __restrict, struct _xlocale *);
82 int __datasize;
83 _RuneLocale _CurrentRuneLocale;
84 };
85 struct __xlocale_st_ldpart {
86 __STRUCT_COMMON
87 char *_locale_buf;
88 };
89 /*
90 * the next four structures must have the first three fields of the same
91 * as the _xlocale_st_ldpart structure above.
92 */
93 struct __xlocale_st_messages {
94 __STRUCT_COMMON
95 char *_messages_locale_buf;
96 struct lc_messages_T _messages_locale;
97 };
98 struct __xlocale_st_monetary {
99 __STRUCT_COMMON
100 char *_monetary_locale_buf;
101 struct lc_monetary_T _monetary_locale;
102 };
103 struct __xlocale_st_numeric {
104 __STRUCT_COMMON
105 char *_numeric_locale_buf;
106 struct lc_numeric_T _numeric_locale;
107 };
108 struct __xlocale_st_time {
109 __STRUCT_COMMON
110 char *_time_locale_buf;
111 struct lc_time_T _time_locale;
112 };
113
114 struct __xlocale_st_localeconv {
115 __STRUCT_COMMON
116 struct lconv __ret;
117 };
118
119 /* the extended locale structure */
120 /* values for __numeric_fp_cvt */
121 #define LC_NUMERIC_FP_UNINITIALIZED 0
122 #define LC_NUMERIC_FP_SAME_LOCALE 1
123 #define LC_NUMERIC_FP_USE_LOCALE 2
124
125 struct _xlocale {
126 /* The item(s) before __magic are not copied when duplicating locale_t's */
127 __STRUCT_COMMON /* only used for locale_t's in __lc_numeric_loc */
128 /* 10 independent mbstate_t buffers! */
129 __darwin_mbstate_t __mbs_mblen;
130 __darwin_mbstate_t __mbs_mbrlen;
131 __darwin_mbstate_t __mbs_mbrtowc;
132 __darwin_mbstate_t __mbs_mbsnrtowcs;
133 __darwin_mbstate_t __mbs_mbsrtowcs;
134 __darwin_mbstate_t __mbs_mbtowc;
135 __darwin_mbstate_t __mbs_wcrtomb;
136 __darwin_mbstate_t __mbs_wcsnrtombs;
137 __darwin_mbstate_t __mbs_wcsrtombs;
138 __darwin_mbstate_t __mbs_wctomb;
139 /* magic (Here up to the end is copied when duplicating locale_t's) */
140 int64_t __magic;
141 /* flags */
142 unsigned char __collate_load_error;
143 unsigned char __collate_substitute_nontrivial;
144 unsigned char _messages_using_locale;
145 unsigned char _monetary_using_locale;
146 unsigned char _numeric_using_locale;
147 unsigned char _time_using_locale;
148 unsigned char __mlocale_changed;
149 unsigned char __nlocale_changed;
150 unsigned char __numeric_fp_cvt;
151 /* collate */
152 struct __xlocale_st_collate *__lc_collate;
153 /* ctype */
154 struct __xlocale_st_runelocale *__lc_ctype;
155 /* messages */
156 struct __xlocale_st_messages *__lc_messages;
157 /* monetary */
158 struct __xlocale_st_monetary *__lc_monetary;
159 /* numeric */
160 struct __xlocale_st_numeric *__lc_numeric;
161 struct _xlocale *__lc_numeric_loc;
162 /* time */
163 struct __xlocale_st_time *__lc_time;
164 /* localeconv */
165 struct __xlocale_st_localeconv *__lc_localeconv;
166 };
167
168 #define NORMALIZE_LOCALE(x) if ((x) == NULL) { \
169 (x) = _c_locale; \
170 } else if ((x) == LC_GLOBAL_LOCALE) { \
171 (x) = &__global_locale; \
172 }
173
174 #define XL_RELEASE(x) if ((x) && (x)->__free_extra != XPERMANENT && OSAtomicDecrement32Barrier(&(x)->__refcount) <= 0) { \
175 if ((x)->__free_extra) \
176 (*(x)->__free_extra)((x)); \
177 free((x)); \
178 }
179 #define XL_RETAIN(x) if ((x) && (x)->__free_extra != XPERMANENT) { OSAtomicIncrement32Barrier(&(x)->__refcount); }
180
181 __private_extern__ struct __xlocale_st_runelocale _DefaultRuneXLocale;
182 __private_extern__ struct _xlocale __global_locale;
183 __private_extern__ pthread_key_t __locale_key;
184
185 __BEGIN_DECLS
186
187 void __ldpart_free_extra(struct __xlocale_st_ldpart *);
188 locale_t __numeric_ctype(locale_t);
189 void __xlocale_init(void);
190
191 static inline __attribute__((always_inline)) locale_t
192 __current_locale(void)
193 {
194 locale_t __locale = (locale_t)pthread_getspecific(__locale_key);
195 return (__locale ? __locale : &__global_locale);
196 }
197
198 static inline __attribute__((always_inline)) locale_t
199 __locale_ptr(locale_t __loc)
200 {
201 return (__loc == LC_GLOBAL_LOCALE ? &__global_locale : __loc);
202 }
203
204 __END_DECLS
205
206 #endif /* _XLOCALE_PRIVATE_H_ */