// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
// 2009, 2010
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// .
#include
#include
#include // For getenv, free.
#include
#include // For towupper, etc.
#include
#include
namespace
{
__gnu_cxx::__mutex&
get_locale_mutex()
{
static __gnu_cxx::__mutex locale_mutex;
return locale_mutex;
}
using namespace std;
typedef char fake_locale_Impl[sizeof(locale::_Impl)]
__attribute__ ((aligned(__alignof__(locale::_Impl))));
fake_locale_Impl c_locale_impl;
typedef char fake_locale[sizeof(locale)]
__attribute__ ((aligned(__alignof__(locale))));
fake_locale c_locale;
typedef char fake_name_vec[sizeof(char*)]
__attribute__ ((aligned(__alignof__(char*))));
fake_name_vec name_vec[6 + _GLIBCXX_NUM_CATEGORIES];
typedef char fake_names[sizeof(char[2])]
__attribute__ ((aligned(__alignof__(char[2]))));
fake_names name_c[6 + _GLIBCXX_NUM_CATEGORIES];
typedef char fake_facet_vec[sizeof(locale::facet*)]
__attribute__ ((aligned(__alignof__(locale::facet*))));
fake_facet_vec facet_vec[_GLIBCXX_NUM_FACETS];
typedef char fake_cache_vec[sizeof(locale::facet*)]
__attribute__ ((aligned(__alignof__(locale::facet*))));
fake_cache_vec cache_vec[_GLIBCXX_NUM_FACETS];
typedef char fake_ctype_c[sizeof(std::ctype)]
__attribute__ ((aligned(__alignof__(std::ctype))));
fake_ctype_c ctype_c;
typedef char fake_collate_c[sizeof(std::collate)]
__attribute__ ((aligned(__alignof__(std::collate))));
fake_collate_c collate_c;
typedef char fake_numpunct_c[sizeof(numpunct)]
__attribute__ ((aligned(__alignof__(numpunct))));
fake_numpunct_c numpunct_c;
typedef char fake_num_get_c[sizeof(num_get)]
__attribute__ ((aligned(__alignof__(num_get))));
fake_num_get_c num_get_c;
typedef char fake_num_put_c[sizeof(num_put)]
__attribute__ ((aligned(__alignof__(num_put))));
fake_num_put_c num_put_c;
typedef char fake_codecvt_c[sizeof(codecvt)]
__attribute__ ((aligned(__alignof__(codecvt))));
fake_codecvt_c codecvt_c;
typedef char fake_moneypunct_c[sizeof(moneypunct)]
__attribute__ ((aligned(__alignof__(moneypunct))));
fake_moneypunct_c moneypunct_ct;
fake_moneypunct_c moneypunct_cf;
typedef char fake_money_get_c[sizeof(money_get)]
__attribute__ ((aligned(__alignof__(money_get))));
fake_money_get_c money_get_c;
typedef char fake_money_put_c[sizeof(money_put)]
__attribute__ ((aligned(__alignof__(money_put))));
fake_money_put_c money_put_c;
typedef char fake_timepunct_c[sizeof(__timepunct)]
__attribute__ ((aligned(__alignof__(__timepunct))));
fake_timepunct_c timepunct_c;
typedef char fake_time_get_c[sizeof(time_get)]
__attribute__ ((aligned(__alignof__(time_get))));
fake_time_get_c time_get_c;
typedef char fake_time_put_c[sizeof(time_put)]
__attribute__ ((aligned(__alignof__(time_put))));
fake_time_put_c time_put_c;
typedef char fake_messages_c[sizeof(messages)]
__attribute__ ((aligned(__alignof__(messages))));
fake_messages_c messages_c;
#ifdef _GLIBCXX_USE_WCHAR_T
typedef char fake_wtype_w[sizeof(std::ctype)]
__attribute__ ((aligned(__alignof__(std::ctype))));
fake_wtype_w ctype_w;
typedef char fake_wollate_w[sizeof(std::collate)]
__attribute__ ((aligned(__alignof__(std::collate))));
fake_wollate_w collate_w;
typedef char fake_numpunct_w[sizeof(numpunct)]
__attribute__ ((aligned(__alignof__(numpunct))));
fake_numpunct_w numpunct_w;
typedef char fake_num_get_w[sizeof(num_get)]
__attribute__ ((aligned(__alignof__(num_get))));
fake_num_get_w num_get_w;
typedef char fake_num_put_w[sizeof(num_put)]
__attribute__ ((aligned(__alignof__(num_put))));
fake_num_put_w num_put_w;
typedef char fake_wodecvt_w[sizeof(codecvt)]
__attribute__ ((aligned(__alignof__(codecvt))));
fake_wodecvt_w codecvt_w;
typedef char fake_moneypunct_w[sizeof(moneypunct)]
__attribute__ ((aligned(__alignof__(moneypunct))));
fake_moneypunct_w moneypunct_wt;
fake_moneypunct_w moneypunct_wf;
typedef char fake_money_get_w[sizeof(money_get)]
__attribute__ ((aligned(__alignof__(money_get))));
fake_money_get_w money_get_w;
typedef char fake_money_put_w[sizeof(money_put)]
__attribute__ ((aligned(__alignof__(money_put))));
fake_money_put_w money_put_w;
typedef char fake_timepunct_w[sizeof(__timepunct)]
__attribute__ ((aligned(__alignof__(__timepunct))));
fake_timepunct_w timepunct_w;
typedef char fake_time_get_w[sizeof(time_get)]
__attribute__ ((aligned(__alignof__(time_get))));
fake_time_get_w time_get_w;
typedef char fake_time_put_w[sizeof(time_put)]
__attribute__ ((aligned(__alignof__(time_put))));
fake_time_put_w time_put_w;
typedef char fake_messages_w[sizeof(messages)]
__attribute__ ((aligned(__alignof__(messages))));
fake_messages_w messages_w;
#endif
// Storage for "C" locale caches.
typedef char fake_num_cache_c[sizeof(std::__numpunct_cache)]
__attribute__ ((aligned(__alignof__(std::__numpunct_cache))));
fake_num_cache_c numpunct_cache_c;
typedef char fake_money_cache_c[sizeof(std::__moneypunct_cache)]
__attribute__ ((aligned(__alignof__(std::__moneypunct_cache))));
fake_money_cache_c moneypunct_cache_ct;
fake_money_cache_c moneypunct_cache_cf;
typedef char fake_time_cache_c[sizeof(std::__timepunct_cache)]
__attribute__ ((aligned(__alignof__(std::__timepunct_cache))));
fake_time_cache_c timepunct_cache_c;
#ifdef _GLIBCXX_USE_WCHAR_T
typedef char fake_num_cache_w[sizeof(std::__numpunct_cache)]
__attribute__ ((aligned(__alignof__(std::__numpunct_cache))));
fake_num_cache_w numpunct_cache_w;
typedef char fake_money_cache_w[sizeof(std::__moneypunct_cache)]
__attribute__ ((aligned(__alignof__(std::__moneypunct_cache))));
fake_money_cache_w moneypunct_cache_wt;
fake_money_cache_w moneypunct_cache_wf;
typedef char fake_time_cache_w[sizeof(std::__timepunct_cache)]
__attribute__ ((aligned(__alignof__(std::__timepunct_cache))));
fake_time_cache_w timepunct_cache_w;
#endif
} // anonymous namespace
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
locale::locale() throw() : _M_impl(0)
{
_S_initialize();
// Checked locking to optimize the common case where _S_global
// still points to _S_classic (locale::_S_initialize_once()):
// - If they are the same, just increment the reference count and
// we are done. This effectively constructs a C locale object
// identical to the static c_locale.
// - Otherwise, _S_global can and may be destroyed due to
// locale::global() call on another thread, in which case we
// fall back to lock protected access to both _S_global and
// its reference count.
_M_impl = _S_global;
if (_M_impl == _S_classic)
_M_impl->_M_add_reference();
else
{
__gnu_cxx::__scoped_lock sentry(get_locale_mutex());
_S_global->_M_add_reference();
_M_impl = _S_global;
}
}
locale
locale::global(const locale& __other)
{
_S_initialize();
_Impl* __old;
{
__gnu_cxx::__scoped_lock sentry(get_locale_mutex());
__old = _S_global;
__other._M_impl->_M_add_reference();
_S_global = __other._M_impl;
const string __other_name = __other.name();
if (__other_name != "*")
setlocale(LC_ALL, __other_name.c_str());
}
// Reference count sanity check: one reference removed for the
// subsition of __other locale, one added by return-by-value. Net
// difference: zero. When the returned locale object's destrutor
// is called, then the reference count is decremented and possibly
// destroyed.
return locale(__old);
}
const locale&
locale::classic()
{
_S_initialize();
return *(new (&c_locale) locale(_S_classic));
}
void
locale::_S_initialize_once() throw()
{
// 2 references.
// One reference for _S_classic, one for _S_global
_S_classic = new (&c_locale_impl) _Impl(2);
_S_global = _S_classic;
}
void
locale::_S_initialize()
{
#ifdef __GTHREADS
if (__gthread_active_p())
__gthread_once(&_S_once, _S_initialize_once);
#endif
if (!_S_classic)
_S_initialize_once();
}
// Definitions for static const data members of locale::_Impl
const locale::id* const
locale::_Impl::_S_id_ctype[] =
{
&std::ctype::id,
&codecvt::id,
#ifdef _GLIBCXX_USE_WCHAR_T
&std::ctype::id,
&codecvt::id,
#endif
0
};
const locale::id* const
locale::_Impl::_S_id_numeric[] =
{
&num_get::id,
&num_put::id,
&numpunct::id,
#ifdef _GLIBCXX_USE_WCHAR_T
&num_get::id,
&num_put::id,
&numpunct::id,
#endif
0
};
const locale::id* const
locale::_Impl::_S_id_collate[] =
{
&std::collate::id,
#ifdef _GLIBCXX_USE_WCHAR_T
&std::collate::id,
#endif
0
};
const locale::id* const
locale::_Impl::_S_id_time[] =
{
&__timepunct::id,
&time_get::id,
&time_put::id,
#ifdef _GLIBCXX_USE_WCHAR_T
&__timepunct::id,
&time_get::id,
&time_put::id,
#endif
0
};
const locale::id* const
locale::_Impl::_S_id_monetary[] =
{
&money_get::id,
&money_put::id,
&moneypunct::id,
&moneypunct::id,
#ifdef _GLIBCXX_USE_WCHAR_T
&money_get::id,
&money_put::id,
&moneypunct::id,
&moneypunct::id,
#endif
0
};
const locale::id* const
locale::_Impl::_S_id_messages[] =
{
&std::messages::id,
#ifdef _GLIBCXX_USE_WCHAR_T
&std::messages::id,
#endif
0
};
const locale::id* const* const
locale::_Impl::_S_facet_categories[] =
{
// Order must match the decl order in class locale.
locale::_Impl::_S_id_ctype,
locale::_Impl::_S_id_numeric,
locale::_Impl::_S_id_collate,
locale::_Impl::_S_id_time,
locale::_Impl::_S_id_monetary,
locale::_Impl::_S_id_messages,
0
};
// Construct "C" _Impl.
locale::_Impl::
_Impl(size_t __refs) throw()
: _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS),
_M_caches(0), _M_names(0)
{
_M_facets = new (&facet_vec) const facet*[_M_facets_size];
_M_caches = new (&cache_vec) const facet*[_M_facets_size];
for (size_t __i = 0; __i < _M_facets_size; ++__i)
_M_facets[__i] = _M_caches[__i] = 0;
// Name the categories.
_M_names = new (&name_vec) char*[_S_categories_size];
_M_names[0] = new (&name_c[0]) char[2];
std::memcpy(_M_names[0], locale::facet::_S_get_c_name(), 2);
for (size_t __j = 1; __j < _S_categories_size; ++__j)
_M_names[__j] = 0;
// This is needed as presently the C++ version of "C" locales
// != data in the underlying locale model for __timepunct,
// numpunct, and moneypunct. Also, the "C" locales must be
// constructed in a way such that they are pre-allocated.
// NB: Set locale::facets(ref) count to one so that each individual
// facet is not destroyed when the locale (and thus locale::_Impl) is
// destroyed.
_M_init_facet(new (&ctype_c) std::ctype(0, false, 1));
_M_init_facet(new (&codecvt_c) codecvt(1));
typedef __numpunct_cache num_cache_c;
num_cache_c* __npc = new (&numpunct_cache_c) num_cache_c(2);
_M_init_facet(new (&numpunct_c) numpunct(__npc, 1));
_M_init_facet(new (&num_get_c) num_get(1));
_M_init_facet(new (&num_put_c) num_put(1));
_M_init_facet(new (&collate_c) std::collate(1));
typedef __moneypunct_cache money_cache_cf;
typedef __moneypunct_cache money_cache_ct;
money_cache_cf* __mpcf = new (&moneypunct_cache_cf) money_cache_cf(2);
_M_init_facet(new (&moneypunct_cf) moneypunct(__mpcf, 1));
money_cache_ct* __mpct = new (&moneypunct_cache_ct) money_cache_ct(2);
_M_init_facet(new (&moneypunct_ct) moneypunct(__mpct, 1));
_M_init_facet(new (&money_get_c) money_get(1));
_M_init_facet(new (&money_put_c) money_put(1));
typedef __timepunct_cache time_cache_c;
time_cache_c* __tpc = new (&timepunct_cache_c) time_cache_c(2);
_M_init_facet(new (&timepunct_c) __timepunct(__tpc, 1));
_M_init_facet(new (&time_get_c) time_get(1));
_M_init_facet(new (&time_put_c) time_put(1));
_M_init_facet(new (&messages_c) std::messages(1));
#ifdef _GLIBCXX_USE_WCHAR_T
_M_init_facet(new (&ctype_w) std::ctype(1));
_M_init_facet(new (&codecvt_w) codecvt(1));
typedef __numpunct_cache num_cache_w;
num_cache_w* __npw = new (&numpunct_cache_w) num_cache_w(2);
_M_init_facet(new (&numpunct_w) numpunct(__npw, 1));
_M_init_facet(new (&num_get_w) num_get(1));
_M_init_facet(new (&num_put_w) num_put(1));
_M_init_facet(new (&collate_w) std::collate(1));
typedef __moneypunct_cache money_cache_wf;
typedef __moneypunct_cache money_cache_wt;
money_cache_wf* __mpwf = new (&moneypunct_cache_wf) money_cache_wf(2);
_M_init_facet(new (&moneypunct_wf) moneypunct(__mpwf, 1));
money_cache_wt* __mpwt = new (&moneypunct_cache_wt) money_cache_wt(2);
_M_init_facet(new (&moneypunct_wt) moneypunct(__mpwt, 1));
_M_init_facet(new (&money_get_w) money_get(1));
_M_init_facet(new (&money_put_w) money_put(1));
typedef __timepunct_cache time_cache_w;
time_cache_w* __tpw = new (&timepunct_cache_w) time_cache_w(2);
_M_init_facet(new (&timepunct_w) __timepunct(__tpw, 1));
_M_init_facet(new (&time_get_w) time_get(1));
_M_init_facet(new (&time_put_w) time_put(1));
_M_init_facet(new (&messages_w) std::messages(1));
#endif
// This locale is safe to pre-cache, after all the facets have
// been created and installed.
_M_caches[numpunct::id._M_id()] = __npc;
_M_caches[moneypunct::id._M_id()] = __mpcf;
_M_caches[moneypunct::id._M_id()] = __mpct;
_M_caches[__timepunct::id._M_id()] = __tpc;
#ifdef _GLIBCXX_USE_WCHAR_T
_M_caches[numpunct::id._M_id()] = __npw;
_M_caches[moneypunct::id._M_id()] = __mpwf;
_M_caches[moneypunct::id._M_id()] = __mpwt;
_M_caches[__timepunct::id._M_id()] = __tpw;
#endif
}
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace