1 // Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
23 #define _GLIBCXX_USE_CXX11_ABI 1
27 #include <cwctype> // For towupper, etc.
29 #include <ext/concurrence.h>
34 get_locale_cache_mutex()
36 static __gnu_cxx::__mutex locale_cache_mutex
;
37 return locale_cache_mutex
;
39 } // anonymous namespace
41 // XXX GLIBCXX_ABI Deprecated
42 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
43 # define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
44 _GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
45 _GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
46 _GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
47 _GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
48 # ifdef _GLIBCXX_USE_WCHAR_T
49 _GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
50 _GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
51 _GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
52 _GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
56 namespace std
_GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 // Definitions for static const data members of locale.
61 const locale::category
locale::none
;
62 const locale::category
locale::ctype
;
63 const locale::category
locale::numeric
;
64 const locale::category
locale::collate
;
65 const locale::category
locale::time
;
66 const locale::category
locale::monetary
;
67 const locale::category
locale::messages
;
68 const locale::category
locale::all
;
70 // These are no longer exported.
71 locale::_Impl
* locale::_S_classic
;
72 locale::_Impl
* locale::_S_global
;
75 __gthread_once_t
locale::_S_once
= __GTHREAD_ONCE_INIT
;
78 locale::locale(const locale
& __other
) throw()
79 : _M_impl(__other
._M_impl
)
81 if (_M_impl
!= _S_classic
)
82 _M_impl
->_M_add_reference();
85 // This is used to initialize global and classic locales, and
86 // assumes that the _Impl objects are constructed correctly.
87 // The lack of a reference increment is intentional.
88 locale::locale(_Impl
* __ip
) throw() : _M_impl(__ip
)
91 locale::~locale() throw()
93 if (_M_impl
!= _S_classic
)
94 _M_impl
->_M_remove_reference();
98 locale::operator==(const locale
& __rhs
) const throw()
100 // Deal first with the common cases, fast to process: refcopies,
101 // unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
102 // categories same name, i.e., _M_names[0]). Otherwise fall back
103 // to the general locale::name().
105 if (_M_impl
== __rhs
._M_impl
)
107 else if (!_M_impl
->_M_names
[0] || !__rhs
._M_impl
->_M_names
[0]
108 || std::strcmp(_M_impl
->_M_names
[0],
109 __rhs
._M_impl
->_M_names
[0]) != 0)
111 else if (!_M_impl
->_M_names
[1] && !__rhs
._M_impl
->_M_names
[1])
114 __ret
= this->name() == __rhs
.name();
119 locale::operator=(const locale
& __other
) throw()
121 if (__other
._M_impl
!= _S_classic
)
122 __other
._M_impl
->_M_add_reference();
123 if (_M_impl
!= _S_classic
)
124 _M_impl
->_M_remove_reference();
125 _M_impl
= __other
._M_impl
;
129 _GLIBCXX_DEFAULT_ABI_TAG
134 if (!_M_impl
->_M_names
[0])
136 else if (_M_impl
->_M_check_same_name())
137 __ret
= _M_impl
->_M_names
[0];
141 __ret
+= _S_categories
[0];
143 __ret
+= _M_impl
->_M_names
[0];
144 for (size_t __i
= 1; __i
< _S_categories_size
; ++__i
)
147 __ret
+= _S_categories
[__i
];
149 __ret
+= _M_impl
->_M_names
[__i
];
156 locale::_S_normalize_category(category __cat
)
159 if (__cat
== none
|| ((__cat
& all
) && !(__cat
& ~all
)))
163 // NB: May be a C-style "LC_ALL" category; convert.
181 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
190 __throw_runtime_error(__N("locale::_S_normalize_category "
191 "category not found"));
198 __c_locale
locale::facet::_S_c_locale
;
200 const char locale::facet::_S_c_name
[2] = "C";
203 __gthread_once_t
locale::facet::_S_once
= __GTHREAD_ONCE_INIT
;
207 locale::facet::_S_initialize_once()
209 // Need to check this because we could get called once from
210 // _S_get_c_locale() when the program is single-threaded, and then again
211 // (via __gthread_once) when it's multi-threaded.
215 // Initialize the underlying locale model.
216 _S_create_c_locale(_S_c_locale
, _S_c_name
);
220 locale::facet::_S_get_c_locale()
223 if (__gthread_active_p())
224 __gthread_once(&_S_once
, _S_initialize_once
);
226 if (__builtin_expect (!_S_c_locale
, 0))
227 _S_initialize_once();
232 locale::facet::_S_get_c_name() throw()
233 { return _S_c_name
; }
243 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
245 _M_facets
[__i
]->_M_remove_reference();
249 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
251 _M_caches
[__i
]->_M_remove_reference();
255 for (size_t __i
= 0; __i
< _S_categories_size
; ++__i
)
256 delete [] _M_names
[__i
];
260 // Clone existing _Impl object.
262 _Impl(const _Impl
& __imp
, size_t __refs
)
263 : _M_refcount(__refs
), _M_facets(0), _M_facets_size(__imp
._M_facets_size
),
264 _M_caches(0), _M_names(0)
268 _M_facets
= new const facet
*[_M_facets_size
];
269 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
271 _M_facets
[__i
] = __imp
._M_facets
[__i
];
273 _M_facets
[__i
]->_M_add_reference();
275 _M_caches
= new const facet
*[_M_facets_size
];
276 for (size_t __j
= 0; __j
< _M_facets_size
; ++__j
)
278 _M_caches
[__j
] = __imp
._M_caches
[__j
];
280 _M_caches
[__j
]->_M_add_reference();
282 _M_names
= new char*[_S_categories_size
];
283 for (size_t __k
= 0; __k
< _S_categories_size
; ++__k
)
286 // Name the categories.
287 for (size_t __l
= 0; (__l
< _S_categories_size
288 && __imp
._M_names
[__l
]); ++__l
)
290 const size_t __len
= std::strlen(__imp
._M_names
[__l
]) + 1;
291 _M_names
[__l
] = new char[__len
];
292 std::memcpy(_M_names
[__l
], __imp
._M_names
[__l
], __len
);
298 __throw_exception_again
;
304 _M_replace_category(const _Impl
* __imp
,
305 const locale::id
* const* __idpp
)
307 for (; *__idpp
; ++__idpp
)
308 _M_replace_facet(__imp
, *__idpp
);
313 _M_replace_facet(const _Impl
* __imp
, const locale::id
* __idp
)
315 size_t __index
= __idp
->_M_id();
316 if ((__index
> (__imp
->_M_facets_size
- 1))
317 || !__imp
->_M_facets
[__index
])
318 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
319 _M_install_facet(__idp
, __imp
->_M_facets
[__index
]);
324 _M_install_facet(const locale::id
* __idp
, const facet
* __fp
)
326 if (__builtin_expect(__fp
!= 0, 1))
328 size_t __index
= __idp
->_M_id();
330 // Check size of facet vector to ensure adequate room.
331 if (__index
> _M_facets_size
- 1)
333 const size_t __new_size
= __index
+ 4;
336 const facet
** __oldf
= _M_facets
;
337 const facet
** __newf
;
338 __newf
= new const facet
*[__new_size
];
339 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
340 __newf
[__i
] = _M_facets
[__i
];
341 for (size_t __l
= _M_facets_size
; __l
< __new_size
; ++__l
)
345 const facet
** __oldc
= _M_caches
;
346 const facet
** __newc
;
349 __newc
= new const facet
*[__new_size
];
354 __throw_exception_again
;
356 for (size_t __j
= 0; __j
< _M_facets_size
; ++__j
)
357 __newc
[__j
] = _M_caches
[__j
];
358 for (size_t __k
= _M_facets_size
; __k
< __new_size
; ++__k
)
361 _M_facets_size
= __new_size
;
368 __fp
->_M_add_reference();
369 const facet
*& __fpr
= _M_facets
[__index
];
372 #if _GLIBCXX_USE_DUAL_ABI
373 // If this is a twinned facet replace its twin with a shim.
374 for (const id
* const* p
= _S_twinned_facets
; *p
!= 0; p
+= 2)
376 if (p
[0]->_M_id() == __index
)
378 // replacing the old ABI facet, also replace new ABI twin
379 const facet
*& __fpr2
= _M_facets
[p
[1]->_M_id()];
382 const facet
* __fp2
= __fp
->_M_sso_shim(p
[1]);
383 __fp2
->_M_add_reference();
384 __fpr2
->_M_remove_reference();
389 else if (p
[1]->_M_id() == __index
)
391 // replacing the new ABI facet, also replace old ABI twin
392 const facet
*& __fpr2
= _M_facets
[p
[0]->_M_id()];
395 const facet
* __fp2
= __fp
->_M_cow_shim(p
[0]);
396 __fp2
->_M_add_reference();
397 __fpr2
->_M_remove_reference();
404 // Replacing an existing facet. Order matters.
405 __fpr
->_M_remove_reference();
410 // Installing a newly created facet into an empty
411 // _M_facets container, say a newly-constructed,
412 // swanky-fresh _Impl.
413 _M_facets
[__index
] = __fp
;
416 // Ideally, it would be nice to only remove the caches that
417 // are now incorrect. However, some of the caches depend on
418 // multiple facets, and we only know about one facet
419 // here. It's no great loss: the first use of the new facet
420 // will create a new, correctly cached facet anyway.
421 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
423 const facet
* __cpr
= _M_caches
[__i
];
426 __cpr
->_M_remove_reference();
435 _M_install_cache(const facet
* __cache
, size_t __index
)
437 __gnu_cxx::__scoped_lock
sentry(get_locale_cache_mutex());
438 #if _GLIBCXX_USE_DUAL_ABI
439 // If this cache is for one of the facets that is instantiated twice,
440 // for old and new std::string ABI, install it in both slots.
441 size_t __index2
= -1;
442 for (const id
* const* p
= _S_twinned_facets
; *p
!= 0; p
+= 2)
444 if (p
[0]->_M_id() == __index
)
446 __index2
= p
[1]->_M_id();
449 else if (p
[1]->_M_id() == __index
)
452 __index
= p
[0]->_M_id();
457 if (_M_caches
[__index
] != 0)
459 // Some other thread got in first.
464 __cache
->_M_add_reference();
465 _M_caches
[__index
] = __cache
;
466 #if _GLIBCXX_USE_DUAL_ABI
467 if (__index2
!= size_t(-1))
469 __cache
->_M_add_reference();
470 _M_caches
[__index2
] = __cache
;
477 // Definitions for static const data members of locale::id
478 _Atomic_word
locale::id::_S_refcount
; // init'd to 0 by linker
480 // XXX GLIBCXX_ABI Deprecated
481 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
484 find_ldbl_sync_facet(const locale::id
* __idp
)
486 # define _GLIBCXX_SYNC_ID(facet, mangled) \
487 if (__idp == &::mangled) \
490 _GLIBCXX_SYNC_ID (num_get
<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
491 _GLIBCXX_SYNC_ID (num_put
<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
492 _GLIBCXX_SYNC_ID (money_get
<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
493 _GLIBCXX_SYNC_ID (money_put
<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
494 # ifdef _GLIBCXX_USE_WCHAR_T
495 _GLIBCXX_SYNC_ID (num_get
<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
496 _GLIBCXX_SYNC_ID (num_put
<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
497 _GLIBCXX_SYNC_ID (money_get
<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
498 _GLIBCXX_SYNC_ID (money_put
<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
506 locale::id::_M_id() const throw()
510 // XXX GLIBCXX_ABI Deprecated
511 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
512 if (locale::id
* f
= find_ldbl_sync_facet(this))
514 const size_t sync_id
= f
->_M_id();
515 _M_index
= 1 + sync_id
;
521 if (!__gnu_cxx::__is_single_threaded())
523 if (__atomic_always_lock_free(sizeof(_M_index
), &_M_index
))
525 const _Atomic_word next
526 = 1 + __gnu_cxx::__exchange_and_add(&_S_refcount
, 1);
528 __atomic_compare_exchange_n(&_M_index
, &expected
, next
,
530 /* success = */ __ATOMIC_ACQ_REL
,
531 /* failure = */ __ATOMIC_ACQUIRE
);
535 static __gnu_cxx::__mutex m
;
536 __gnu_cxx::__scoped_lock
l(m
);
538 _M_index
= ++_S_refcount
;
543 _M_index
= ++_S_refcount
; // single-threaded case
548 _GLIBCXX_END_NAMESPACE_VERSION