1 // Copyright (C) 1997-2023 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 // Initialize the underlying locale model.
210 _S_create_c_locale(_S_c_locale
, _S_c_name
);
214 locale::facet::_S_get_c_locale()
217 if (__gthread_active_p())
218 __gthread_once(&_S_once
, _S_initialize_once
);
223 _S_initialize_once();
229 locale::facet::_S_get_c_name() throw()
230 { return _S_c_name
; }
240 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
242 _M_facets
[__i
]->_M_remove_reference();
246 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
248 _M_caches
[__i
]->_M_remove_reference();
252 for (size_t __i
= 0; __i
< _S_categories_size
; ++__i
)
253 delete [] _M_names
[__i
];
257 // Clone existing _Impl object.
259 _Impl(const _Impl
& __imp
, size_t __refs
)
260 : _M_refcount(__refs
), _M_facets(0), _M_facets_size(__imp
._M_facets_size
),
261 _M_caches(0), _M_names(0)
265 _M_facets
= new const facet
*[_M_facets_size
];
266 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
268 _M_facets
[__i
] = __imp
._M_facets
[__i
];
270 _M_facets
[__i
]->_M_add_reference();
272 _M_caches
= new const facet
*[_M_facets_size
];
273 for (size_t __j
= 0; __j
< _M_facets_size
; ++__j
)
275 _M_caches
[__j
] = __imp
._M_caches
[__j
];
277 _M_caches
[__j
]->_M_add_reference();
279 _M_names
= new char*[_S_categories_size
];
280 for (size_t __k
= 0; __k
< _S_categories_size
; ++__k
)
283 // Name the categories.
284 for (size_t __l
= 0; (__l
< _S_categories_size
285 && __imp
._M_names
[__l
]); ++__l
)
287 const size_t __len
= std::strlen(__imp
._M_names
[__l
]) + 1;
288 _M_names
[__l
] = new char[__len
];
289 std::memcpy(_M_names
[__l
], __imp
._M_names
[__l
], __len
);
295 __throw_exception_again
;
301 _M_replace_category(const _Impl
* __imp
,
302 const locale::id
* const* __idpp
)
304 for (; *__idpp
; ++__idpp
)
305 _M_replace_facet(__imp
, *__idpp
);
310 _M_replace_facet(const _Impl
* __imp
, const locale::id
* __idp
)
312 size_t __index
= __idp
->_M_id();
313 if ((__index
> (__imp
->_M_facets_size
- 1))
314 || !__imp
->_M_facets
[__index
])
315 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
316 _M_install_facet(__idp
, __imp
->_M_facets
[__index
]);
321 _M_install_facet(const locale::id
* __idp
, const facet
* __fp
)
325 size_t __index
= __idp
->_M_id();
327 // Check size of facet vector to ensure adequate room.
328 if (__index
> _M_facets_size
- 1)
330 const size_t __new_size
= __index
+ 4;
333 const facet
** __oldf
= _M_facets
;
334 const facet
** __newf
;
335 __newf
= new const facet
*[__new_size
];
336 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
337 __newf
[__i
] = _M_facets
[__i
];
338 for (size_t __l
= _M_facets_size
; __l
< __new_size
; ++__l
)
342 const facet
** __oldc
= _M_caches
;
343 const facet
** __newc
;
346 __newc
= new const facet
*[__new_size
];
351 __throw_exception_again
;
353 for (size_t __j
= 0; __j
< _M_facets_size
; ++__j
)
354 __newc
[__j
] = _M_caches
[__j
];
355 for (size_t __k
= _M_facets_size
; __k
< __new_size
; ++__k
)
358 _M_facets_size
= __new_size
;
365 __fp
->_M_add_reference();
366 const facet
*& __fpr
= _M_facets
[__index
];
369 #if _GLIBCXX_USE_DUAL_ABI
370 // If this is a twinned facet replace its twin with a shim.
371 for (const id
* const* p
= _S_twinned_facets
; *p
!= 0; p
+= 2)
373 if (p
[0]->_M_id() == __index
)
375 // replacing the old ABI facet, also replace new ABI twin
376 const facet
*& __fpr2
= _M_facets
[p
[1]->_M_id()];
379 const facet
* __fp2
= __fp
->_M_sso_shim(p
[1]);
380 __fp2
->_M_add_reference();
381 __fpr2
->_M_remove_reference();
386 else if (p
[1]->_M_id() == __index
)
388 // replacing the new ABI facet, also replace old ABI twin
389 const facet
*& __fpr2
= _M_facets
[p
[0]->_M_id()];
392 const facet
* __fp2
= __fp
->_M_cow_shim(p
[0]);
393 __fp2
->_M_add_reference();
394 __fpr2
->_M_remove_reference();
401 // Replacing an existing facet. Order matters.
402 __fpr
->_M_remove_reference();
407 // Installing a newly created facet into an empty
408 // _M_facets container, say a newly-constructed,
409 // swanky-fresh _Impl.
410 _M_facets
[__index
] = __fp
;
413 // Ideally, it would be nice to only remove the caches that
414 // are now incorrect. However, some of the caches depend on
415 // multiple facets, and we only know about one facet
416 // here. It's no great loss: the first use of the new facet
417 // will create a new, correctly cached facet anyway.
418 for (size_t __i
= 0; __i
< _M_facets_size
; ++__i
)
420 const facet
* __cpr
= _M_caches
[__i
];
423 __cpr
->_M_remove_reference();
432 _M_install_cache(const facet
* __cache
, size_t __index
)
434 __gnu_cxx::__scoped_lock
sentry(get_locale_cache_mutex());
435 #if _GLIBCXX_USE_DUAL_ABI
436 // If this cache is for one of the facets that is instantiated twice,
437 // for old and new std::string ABI, install it in both slots.
438 size_t __index2
= -1;
439 for (const id
* const* p
= _S_twinned_facets
; *p
!= 0; p
+= 2)
441 if (p
[0]->_M_id() == __index
)
443 __index2
= p
[1]->_M_id();
446 else if (p
[1]->_M_id() == __index
)
449 __index
= p
[0]->_M_id();
454 if (_M_caches
[__index
] != 0)
456 // Some other thread got in first.
461 __cache
->_M_add_reference();
462 _M_caches
[__index
] = __cache
;
463 #if _GLIBCXX_USE_DUAL_ABI
464 if (__index2
!= size_t(-1))
466 __cache
->_M_add_reference();
467 _M_caches
[__index2
] = __cache
;
474 // Definitions for static const data members of locale::id
475 _Atomic_word
locale::id::_S_refcount
; // init'd to 0 by linker
477 // XXX GLIBCXX_ABI Deprecated
478 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
481 find_ldbl_sync_facet(const locale::id
* __idp
)
483 # define _GLIBCXX_SYNC_ID(facet, mangled) \
484 if (__idp == &::mangled) \
487 _GLIBCXX_SYNC_ID (num_get
<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
488 _GLIBCXX_SYNC_ID (num_put
<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
489 _GLIBCXX_SYNC_ID (money_get
<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
490 _GLIBCXX_SYNC_ID (money_put
<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE
);
491 # ifdef _GLIBCXX_USE_WCHAR_T
492 _GLIBCXX_SYNC_ID (num_get
<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
493 _GLIBCXX_SYNC_ID (num_put
<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
494 _GLIBCXX_SYNC_ID (money_get
<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
495 _GLIBCXX_SYNC_ID (money_put
<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE
);
503 locale::id::_M_id() const throw()
507 // XXX GLIBCXX_ABI Deprecated
508 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
509 if (locale::id
* f
= find_ldbl_sync_facet(this))
511 const size_t sync_id
= f
->_M_id();
512 _M_index
= 1 + sync_id
;
518 if (!__gnu_cxx::__is_single_threaded())
520 if (__atomic_always_lock_free(sizeof(_M_index
), &_M_index
))
522 const _Atomic_word next
523 = 1 + __gnu_cxx::__exchange_and_add(&_S_refcount
, 1);
525 __atomic_compare_exchange_n(&_M_index
, &expected
, next
,
527 /* success = */ __ATOMIC_ACQ_REL
,
528 /* failure = */ __ATOMIC_ACQUIRE
);
532 static __gnu_cxx::__mutex m
;
533 __gnu_cxx::__scoped_lock
l(m
);
535 _M_index
= ++_S_refcount
;
540 _M_index
= ++_S_refcount
; // single-threaded case
545 _GLIBCXX_END_NAMESPACE_VERSION