3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #include "stlport_prefix.h"
27 #include "locale_impl.h"
28 #include "acquire_release.h"
31 _STLP_MOVE_TO_PRIV_NAMESPACE
33 // those wrappers are needed to avoid extern "C"
35 static void* _Loc_ctype_create(const char * s, _Locale_name_hint* hint)
36 { return _Locale_ctype_create(s, hint); }
37 static void* _Loc_numeric_create(const char * s, _Locale_name_hint* hint)
38 { return _Locale_numeric_create(s, hint); }
39 static void* _Loc_time_create(const char * s, _Locale_name_hint* hint)
40 { return _Locale_time_create(s, hint); }
41 static void* _Loc_collate_create(const char * s, _Locale_name_hint* hint)
42 { return _Locale_collate_create(s, hint); }
43 static void* _Loc_monetary_create(const char * s, _Locale_name_hint* hint)
44 { return _Locale_monetary_create(s, hint); }
45 static void* _Loc_messages_create(const char * s, _Locale_name_hint* hint)
46 { return _Locale_messages_create(s, hint); }
48 static char const* _Loc_ctype_name(const void* l, char* s)
49 { return _Locale_ctype_name(l, s); }
50 static char const* _Loc_numeric_name(const void* l, char* s)
51 { return _Locale_numeric_name(l, s); }
52 static char const* _Loc_time_name(const void* l, char* s)
53 { return _Locale_time_name(l,s); }
54 static char const* _Loc_collate_name( const void* l, char* s)
55 { return _Locale_collate_name(l,s); }
56 static char const* _Loc_monetary_name(const void* l, char* s)
57 { return _Locale_monetary_name(l,s); }
58 static char const* _Loc_messages_name(const void* l, char* s)
59 { return _Locale_messages_name(l,s); }
61 static const char* _Loc_ctype_default(char* p)
62 { return _Locale_ctype_default(p); }
63 static const char* _Loc_numeric_default(char * p)
64 { return _Locale_numeric_default(p); }
65 static const char* _Loc_time_default(char* p)
66 { return _Locale_time_default(p); }
67 static const char* _Loc_collate_default(char* p)
68 { return _Locale_collate_default(p); }
69 static const char* _Loc_monetary_default(char* p)
70 { return _Locale_monetary_default(p); }
71 static const char* _Loc_messages_default(char* p)
72 { return _Locale_messages_default(p); }
74 static void _Loc_ctype_destroy(void* p) {_Locale_ctype_destroy(p); }
75 static void _Loc_numeric_destroy(void* p) {_Locale_numeric_destroy(p); }
76 static void _Loc_time_destroy(void* p) {_Locale_time_destroy(p);}
77 static void _Loc_collate_destroy(void* p) {_Locale_collate_destroy(p);}
78 static void _Loc_monetary_destroy(void* p) {_Locale_monetary_destroy(p);}
79 static void _Loc_messages_destroy(void* p) {_Locale_messages_destroy(p);}
81 typedef void* (*loc_create_func_t)(const char *, _Locale_name_hint*);
82 typedef char const* (*loc_name_func_t)(const void* l, char* s);
83 typedef void (*loc_destroy_func_t)(void* l);
84 typedef const char* (*loc_default_name_func_t)(char* s);
85 typedef char const* (*loc_extract_name_func_t)(const char*, char*, _Locale_name_hint*);
87 //----------------------------------------------------------------------
88 // Acquire and release low-level category objects. The whole point of
89 // this is so that we don't allocate (say) four different _Locale_ctype
90 // objects for a single locale.
92 // Global hash tables for category objects.
93 typedef hash_map<string, pair<void*, size_t>, hash<string>, equal_to<string> > Category_Map;
95 // Look up a category by name
96 static Category_Map** ctype_hash() {
97 static Category_Map *_S_ctype_hash = 0;
98 return &_S_ctype_hash;
100 static Category_Map** numeric_hash() {
101 static Category_Map *_S_numeric_hash = 0;
102 return &_S_numeric_hash;
104 static Category_Map** time_hash() {
105 static Category_Map *_S_time_hash = 0;
106 return &_S_time_hash;
108 static Category_Map** collate_hash() {
109 static Category_Map *_S_collate_hash = 0;
110 return &_S_collate_hash;
112 static Category_Map** monetary_hash() {
113 static Category_Map *_S_monetary_hash = 0;
114 return &_S_monetary_hash;
116 static Category_Map** messages_hash() {
117 static Category_Map *_S_messages_hash;
118 return &_S_messages_hash;
121 // We have a single lock for all of the hash tables. We may wish to
122 // replace it with six different locks.
124 static _STLP_STATIC_MUTEX __category_hash_lock _STLP_MUTEX_INITIALIZER;
127 __acquire_category(const char* name, _Locale_name_hint* hint,
128 loc_extract_name_func_t extract_name,
129 loc_create_func_t create_obj, loc_default_name_func_t default_obj,
131 #if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x564)
132 typedef Category_Map::iterator Category_iterator;
133 pair<Category_iterator, bool> result;
135 pair<Category_Map::iterator, bool> result;
138 // Find what name to look for. Be careful if user requests the default.
140 char buf[_Locale_MAX_SIMPLE_NAME];
141 if (name == 0 || name[0] == 0) {
142 cname = default_obj(buf);
143 if (cname == 0 || cname[0] == 0)
147 cname = extract_name(name, buf, hint);
153 Category_Map::value_type __e(cname, pair<void*,size_t>((void*)0,size_t(0)));
155 _STLP_auto_lock sentry(__category_hash_lock);
158 *M = new Category_Map();
160 #if defined(__SC__) //*TY 06/01/2000 - added workaround for SCpp
161 if(!*M) delete *M; //*TY 06/01/2000 - it forgets to generate dtor for Category_Map class. This fake code forces to generate one.
162 #endif //*TY 06/01/2000 -
164 // Look for an existing entry with that name.
165 result = (*M)->insert_noresize(__e);
167 // There was no entry in the map already. Create the category.
169 (*result.first).second.first = create_obj(cname, hint);
171 // Increment the reference count.
172 ++((*result.first).second.second);
174 return (*result.first).second.first;
178 __release_category(void* cat,
179 loc_destroy_func_t destroy_fun,
180 loc_name_func_t get_name,
182 Category_Map *pM = *M;
185 // Find the name of the category object.
186 char buf[_Locale_MAX_SIMPLE_NAME + 1];
187 char const* name = get_name(cat, buf);
190 _STLP_auto_lock sentry(__category_hash_lock);
191 Category_Map::iterator it = pM->find(name);
192 if (it != pM->end()) {
193 // Decrement the ref count. If it goes to zero, delete this category
195 if (--((*it).second.second) == 0) {
196 void* cat1 = (*it).second.first;
199 #if defined (_STLP_LEAKS_PEDANTIC)
204 #endif /* _STLP_LEAKS_PEDANTIC */
211 _Locale_ctype* _STLP_CALL __acquire_ctype(const char* name, _Locale_name_hint* hint) {
212 return __REINTERPRET_CAST(_Locale_ctype*, __acquire_category(name, hint,
213 _Locale_extract_ctype_name, _Loc_ctype_create, _Loc_ctype_default,
216 _Locale_numeric* _STLP_CALL __acquire_numeric(const char* name, _Locale_name_hint* hint) {
217 return __REINTERPRET_CAST(_Locale_numeric*, __acquire_category(name, hint,
218 _Locale_extract_numeric_name, _Loc_numeric_create, _Loc_numeric_default,
221 _Locale_time* _STLP_CALL __acquire_time(const char* name, _Locale_name_hint* hint) {
222 return __REINTERPRET_CAST(_Locale_time*, __acquire_category(name, hint,
223 _Locale_extract_time_name, _Loc_time_create, _Loc_time_default,
226 _Locale_collate* _STLP_CALL __acquire_collate(const char* name, _Locale_name_hint* hint) {
227 return __REINTERPRET_CAST(_Locale_collate*, __acquire_category(name, hint,
228 _Locale_extract_collate_name, _Loc_collate_create, _Loc_collate_default,
231 _Locale_monetary* _STLP_CALL __acquire_monetary(const char* name, _Locale_name_hint* hint) {
232 return __REINTERPRET_CAST(_Locale_monetary*, __acquire_category(name, hint,
233 _Locale_extract_monetary_name, _Loc_monetary_create, _Loc_monetary_default,
236 _Locale_messages* _STLP_CALL __acquire_messages(const char* name, _Locale_name_hint* hint) {
237 return __REINTERPRET_CAST(_Locale_messages*, __acquire_category(name, hint,
238 _Locale_extract_messages_name, _Loc_messages_create, _Loc_messages_default,
242 void _STLP_CALL __release_ctype(_Locale_ctype* cat)
243 { __release_category(cat, _Loc_ctype_destroy, _Loc_ctype_name, ctype_hash()); }
244 void _STLP_CALL __release_numeric(_Locale_numeric* cat)
245 { __release_category(cat, _Loc_numeric_destroy, _Loc_numeric_name, numeric_hash()); }
246 void _STLP_CALL __release_time(_Locale_time* cat)
247 { __release_category(cat, _Loc_time_destroy, _Loc_time_name, time_hash()); }
248 void _STLP_CALL __release_collate(_Locale_collate* cat)
249 { __release_category(cat, _Loc_collate_destroy, _Loc_collate_name, collate_hash()); }
250 void _STLP_CALL __release_monetary(_Locale_monetary* cat)
251 { __release_category(cat, _Loc_monetary_destroy, _Loc_monetary_name, monetary_hash()); }
252 void _STLP_CALL __release_messages(_Locale_messages* cat)
253 { __release_category(cat, _Loc_messages_destroy, _Loc_messages_name, messages_hash()); }
255 _STLP_MOVE_TO_STD_NAMESPACE