1 // Copyright (C) 1997-2021 Free Software Foundation, Inc. 2 // 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) 7 // any later version. 8 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. 13 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. 17 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/>. 22 23 #define _GLIBCXX_USE_CXX11_ABI 1 24 #include <clocale> 25 #include <cstring> 26 #include <cctype> 27 #include <cwctype> // For towupper, etc. 28 #include <locale> 29 #include <ext/concurrence.h> 30 31 namespace 32 { 33 __gnu_cxx::__mutex& get_locale_cache_mutex()34 get_locale_cache_mutex() 35 { 36 static __gnu_cxx::__mutex locale_cache_mutex; 37 return locale_cache_mutex; 38 } 39 } // anonymous namespace 40 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); 53 # endif 54 #endif 55 56 namespace std _GLIBCXX_VISIBILITY(default) 57 { 58 _GLIBCXX_BEGIN_NAMESPACE_VERSION 59 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; 69 70 // These are no longer exported. 71 locale::_Impl* locale::_S_classic; 72 locale::_Impl* locale::_S_global; 73 74 #ifdef __GTHREADS 75 __gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT; 76 #endif 77 locale(const locale & __other)78 locale::locale(const locale& __other) throw() 79 : _M_impl(__other._M_impl) 80 { 81 if (_M_impl != _S_classic) 82 _M_impl->_M_add_reference(); 83 } 84 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. locale(_Impl * __ip)88 locale::locale(_Impl* __ip) throw() : _M_impl(__ip) 89 { } 90 ~locale()91 locale::~locale() throw() 92 { 93 if (_M_impl != _S_classic) 94 _M_impl->_M_remove_reference(); 95 } 96 97 bool operator ==(const locale & __rhs) const98 locale::operator==(const locale& __rhs) const throw() 99 { 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(). 104 bool __ret; 105 if (_M_impl == __rhs._M_impl) 106 __ret = true; 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) 110 __ret = false; 111 else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1]) 112 __ret = true; 113 else 114 __ret = this->name() == __rhs.name(); 115 return __ret; 116 } 117 118 const locale& operator =(const locale & __other)119 locale::operator=(const locale& __other) throw() 120 { 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; 126 return *this; 127 } 128 129 _GLIBCXX_DEFAULT_ABI_TAG 130 string name() const131 locale::name() const 132 { 133 string __ret; 134 if (!_M_impl->_M_names[0]) 135 __ret = '*'; 136 else if (_M_impl->_M_check_same_name()) 137 __ret = _M_impl->_M_names[0]; 138 else 139 { 140 __ret.reserve(128); 141 __ret += _S_categories[0]; 142 __ret += '='; 143 __ret += _M_impl->_M_names[0]; 144 for (size_t __i = 1; __i < _S_categories_size; ++__i) 145 { 146 __ret += ';'; 147 __ret += _S_categories[__i]; 148 __ret += '='; 149 __ret += _M_impl->_M_names[__i]; 150 } 151 } 152 return __ret; 153 } 154 155 locale::category _S_normalize_category(category __cat)156 locale::_S_normalize_category(category __cat) 157 { 158 int __ret = 0; 159 if (__cat == none || ((__cat & all) && !(__cat & ~all))) 160 __ret = __cat; 161 else 162 { 163 // NB: May be a C-style "LC_ALL" category; convert. 164 switch (__cat) 165 { 166 case LC_COLLATE: 167 __ret = collate; 168 break; 169 case LC_CTYPE: 170 __ret = ctype; 171 break; 172 case LC_MONETARY: 173 __ret = monetary; 174 break; 175 case LC_NUMERIC: 176 __ret = numeric; 177 break; 178 case LC_TIME: 179 __ret = time; 180 break; 181 #ifdef _GLIBCXX_HAVE_LC_MESSAGES 182 case LC_MESSAGES: 183 __ret = messages; 184 break; 185 #endif 186 case LC_ALL: 187 __ret = all; 188 break; 189 default: 190 __throw_runtime_error(__N("locale::_S_normalize_category " 191 "category not found")); 192 } 193 } 194 return __ret; 195 } 196 197 // locale::facet 198 __c_locale locale::facet::_S_c_locale; 199 200 const char locale::facet::_S_c_name[2] = "C"; 201 202 #ifdef __GTHREADS 203 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT; 204 #endif 205 206 void _S_initialize_once()207 locale::facet::_S_initialize_once() 208 { 209 // Initialize the underlying locale model. 210 _S_create_c_locale(_S_c_locale, _S_c_name); 211 } 212 213 __c_locale _S_get_c_locale()214 locale::facet::_S_get_c_locale() 215 { 216 #ifdef __GTHREADS 217 if (__gthread_active_p()) 218 __gthread_once(&_S_once, _S_initialize_once); 219 else 220 #endif 221 { 222 if (!_S_c_locale) 223 _S_initialize_once(); 224 } 225 return _S_c_locale; 226 } 227 228 const char* _S_get_c_name()229 locale::facet::_S_get_c_name() throw() 230 { return _S_c_name; } 231 232 locale::facet:: ~facet()233 ~facet() { } 234 235 // locale::_Impl 236 locale::_Impl:: ~_Impl()237 ~_Impl() throw() 238 { 239 if (_M_facets) 240 for (size_t __i = 0; __i < _M_facets_size; ++__i) 241 if (_M_facets[__i]) 242 _M_facets[__i]->_M_remove_reference(); 243 delete [] _M_facets; 244 245 if (_M_caches) 246 for (size_t __i = 0; __i < _M_facets_size; ++__i) 247 if (_M_caches[__i]) 248 _M_caches[__i]->_M_remove_reference(); 249 delete [] _M_caches; 250 251 if (_M_names) 252 for (size_t __i = 0; __i < _S_categories_size; ++__i) 253 delete [] _M_names[__i]; 254 delete [] _M_names; 255 } 256 257 // Clone existing _Impl object. 258 locale::_Impl:: _Impl(const _Impl & __imp,size_t __refs)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) 262 { 263 __try 264 { 265 _M_facets = new const facet*[_M_facets_size]; 266 for (size_t __i = 0; __i < _M_facets_size; ++__i) 267 { 268 _M_facets[__i] = __imp._M_facets[__i]; 269 if (_M_facets[__i]) 270 _M_facets[__i]->_M_add_reference(); 271 } 272 _M_caches = new const facet*[_M_facets_size]; 273 for (size_t __j = 0; __j < _M_facets_size; ++__j) 274 { 275 _M_caches[__j] = __imp._M_caches[__j]; 276 if (_M_caches[__j]) 277 _M_caches[__j]->_M_add_reference(); 278 } 279 _M_names = new char*[_S_categories_size]; 280 for (size_t __k = 0; __k < _S_categories_size; ++__k) 281 _M_names[__k] = 0; 282 283 // Name the categories. 284 for (size_t __l = 0; (__l < _S_categories_size 285 && __imp._M_names[__l]); ++__l) 286 { 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); 290 } 291 } 292 __catch(...) 293 { 294 this->~_Impl(); 295 __throw_exception_again; 296 } 297 } 298 299 void 300 locale::_Impl:: _M_replace_category(const _Impl * __imp,const locale::id * const * __idpp)301 _M_replace_category(const _Impl* __imp, 302 const locale::id* const* __idpp) 303 { 304 for (; *__idpp; ++__idpp) 305 _M_replace_facet(__imp, *__idpp); 306 } 307 308 void 309 locale::_Impl:: _M_replace_facet(const _Impl * __imp,const locale::id * __idp)310 _M_replace_facet(const _Impl* __imp, const locale::id* __idp) 311 { 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]); 317 } 318 319 void 320 locale::_Impl:: _M_install_facet(const locale::id * __idp,const facet * __fp)321 _M_install_facet(const locale::id* __idp, const facet* __fp) 322 { 323 if (__fp) 324 { 325 size_t __index = __idp->_M_id(); 326 327 // Check size of facet vector to ensure adequate room. 328 if (__index > _M_facets_size - 1) 329 { 330 const size_t __new_size = __index + 4; 331 332 // New facet array. 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) 339 __newf[__l] = 0; 340 341 // New cache array. 342 const facet** __oldc = _M_caches; 343 const facet** __newc; 344 __try 345 { 346 __newc = new const facet*[__new_size]; 347 } 348 __catch(...) 349 { 350 delete [] __newf; 351 __throw_exception_again; 352 } 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) 356 __newc[__k] = 0; 357 358 _M_facets_size = __new_size; 359 _M_facets = __newf; 360 _M_caches = __newc; 361 delete [] __oldf; 362 delete [] __oldc; 363 } 364 365 __fp->_M_add_reference(); 366 const facet*& __fpr = _M_facets[__index]; 367 if (__fpr) 368 { 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) 372 { 373 if (p[0]->_M_id() == __index) 374 { 375 // replacing the old ABI facet, also replace new ABI twin 376 const facet*& __fpr2 = _M_facets[p[1]->_M_id()]; 377 if (__fpr2) 378 { 379 const facet* __fp2 = __fp->_M_sso_shim(p[1]); 380 __fp2->_M_add_reference(); 381 __fpr2->_M_remove_reference(); 382 __fpr2 = __fp2; 383 } 384 break; 385 } 386 else if (p[1]->_M_id() == __index) 387 { 388 // replacing the new ABI facet, also replace old ABI twin 389 const facet*& __fpr2 = _M_facets[p[0]->_M_id()]; 390 if (__fpr2) 391 { 392 const facet* __fp2 = __fp->_M_cow_shim(p[0]); 393 __fp2->_M_add_reference(); 394 __fpr2->_M_remove_reference(); 395 __fpr2 = __fp2; 396 } 397 break; 398 } 399 } 400 #endif 401 // Replacing an existing facet. Order matters. 402 __fpr->_M_remove_reference(); 403 __fpr = __fp; 404 } 405 else 406 { 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; 411 } 412 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) 419 { 420 const facet* __cpr = _M_caches[__i]; 421 if (__cpr) 422 { 423 __cpr->_M_remove_reference(); 424 _M_caches[__i] = 0; 425 } 426 } 427 } 428 } 429 430 void 431 locale::_Impl:: _M_install_cache(const facet * __cache,size_t __index)432 _M_install_cache(const facet* __cache, size_t __index) 433 { 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) 440 { 441 if (p[0]->_M_id() == __index) 442 { 443 __index2 = p[1]->_M_id(); 444 break; 445 } 446 else if (p[1]->_M_id() == __index) 447 { 448 __index2 = __index; 449 __index = p[0]->_M_id(); 450 break; 451 } 452 } 453 #endif 454 if (_M_caches[__index] != 0) 455 { 456 // Some other thread got in first. 457 delete __cache; 458 } 459 else 460 { 461 __cache->_M_add_reference(); 462 _M_caches[__index] = __cache; 463 #if _GLIBCXX_USE_DUAL_ABI 464 if (__index2 != size_t(-1)) 465 { 466 __cache->_M_add_reference(); 467 _M_caches[__index2] = __cache; 468 } 469 #endif 470 } 471 } 472 473 // locale::id 474 // Definitions for static const data members of locale::id 475 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker 476 477 // XXX GLIBCXX_ABI Deprecated 478 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT 479 namespace { 480 inline locale::id* find_ldbl_sync_facet(const locale::id * __idp)481 find_ldbl_sync_facet(const locale::id* __idp) 482 { 483 # define _GLIBCXX_SYNC_ID(facet, mangled) \ 484 if (__idp == &::mangled) \ 485 return &facet::id 486 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); 496 # endif 497 return 0; 498 } 499 } // namespace 500 #endif 501 502 size_t _M_id() const503 locale::id::_M_id() const throw() 504 { 505 if (!_M_index) 506 { 507 // XXX GLIBCXX_ABI Deprecated 508 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT 509 if (locale::id* f = find_ldbl_sync_facet(this)) 510 { 511 const size_t sync_id = f->_M_id(); 512 _M_index = 1 + sync_id; 513 return sync_id; 514 } 515 #endif 516 517 #ifdef __GTHREADS 518 if (!__gnu_cxx::__is_single_threaded()) 519 { 520 if (__atomic_always_lock_free(sizeof(_M_index), &_M_index)) 521 { 522 const _Atomic_word next 523 = 1 + __gnu_cxx::__exchange_and_add(&_S_refcount, 1); 524 size_t expected = 0; 525 __atomic_compare_exchange_n(&_M_index, &expected, next, 526 /* weak = */ false, 527 /* success = */ __ATOMIC_ACQ_REL, 528 /* failure = */ __ATOMIC_ACQUIRE); 529 } 530 else 531 { 532 static __gnu_cxx::__mutex m; 533 __gnu_cxx::__scoped_lock l(m); 534 if (!_M_index) 535 _M_index = ++_S_refcount; 536 } 537 } 538 else 539 #endif 540 _M_index = ++_S_refcount; // single-threaded case 541 } 542 return _M_index - 1; 543 } 544 545 _GLIBCXX_END_NAMESPACE_VERSION 546 } // namespace 547