diff options
author | theraven <theraven@FreeBSD.org> | 2013-07-10 16:28:24 +0000 |
---|---|---|
committer | theraven <theraven@FreeBSD.org> | 2013-07-10 16:28:24 +0000 |
commit | 214e4f8fe625ad54aac3b215909e0b994a5768ec (patch) | |
tree | f757844d307e5ebbf92ab857f2376cceb7807147 /contrib/libc++/src | |
parent | c660176671bf640bee3fd09dbfa32ad187a9b942 (diff) | |
download | FreeBSD-src-214e4f8fe625ad54aac3b215909e0b994a5768ec.zip FreeBSD-src-214e4f8fe625ad54aac3b215909e0b994a5768ec.tar.gz |
Import new libcxxrt / libc++. This brings some bug fixes, including a potential race condition for static initialisers.
Diffstat (limited to 'contrib/libc++/src')
-rw-r--r-- | contrib/libc++/src/debug.cpp | 13 | ||||
-rw-r--r-- | contrib/libc++/src/hash.cpp | 3 | ||||
-rw-r--r-- | contrib/libc++/src/iostream.cpp | 8 | ||||
-rw-r--r-- | contrib/libc++/src/locale.cpp | 57 | ||||
-rw-r--r-- | contrib/libc++/src/stdexcept.cpp | 2 | ||||
-rw-r--r-- | contrib/libc++/src/string.cpp | 817 | ||||
-rw-r--r-- | contrib/libc++/src/thread.cpp | 17 |
7 files changed, 396 insertions, 521 deletions
diff --git a/contrib/libc++/src/debug.cpp b/contrib/libc++/src/debug.cpp index 06040af..04d570e 100644 --- a/contrib/libc++/src/debug.cpp +++ b/contrib/libc++/src/debug.cpp @@ -143,7 +143,7 @@ __libcpp_db::__insert_c(void* __c) if (__csz_ + 1 > static_cast<size_t>(__cend_ - __cbeg_)) { size_t nc = __next_prime(2*static_cast<size_t>(__cend_ - __cbeg_) + 1); - __c_node** cbeg = (__c_node**)calloc(nc, sizeof(void*)); + __c_node** cbeg = static_cast<__c_node**>(calloc(nc, sizeof(void*))); if (cbeg == nullptr) #ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); @@ -168,7 +168,8 @@ __libcpp_db::__insert_c(void* __c) } size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_); __c_node* p = __cbeg_[hc]; - __c_node* r = __cbeg_[hc] = (__c_node*)malloc(sizeof(__c_node)); + __c_node* r = __cbeg_[hc] = + static_cast<__c_node*>(malloc(sizeof(__c_node))); if (__cbeg_[hc] == nullptr) #ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); @@ -407,7 +408,8 @@ __c_node::__add(__i_node* i) size_t nc = 2*static_cast<size_t>(cap_ - beg_); if (nc == 0) nc = 1; - __i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*)); + __i_node** beg = + static_cast<__i_node**>(malloc(nc * sizeof(__i_node*))); if (beg == nullptr) #ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); @@ -433,7 +435,7 @@ __libcpp_db::__insert_iterator(void* __i) if (__isz_ + 1 > static_cast<size_t>(__iend_ - __ibeg_)) { size_t nc = __next_prime(2*static_cast<size_t>(__iend_ - __ibeg_) + 1); - __i_node** ibeg = (__i_node**)calloc(nc, sizeof(void*)); + __i_node** ibeg = static_cast<__i_node**>(calloc(nc, sizeof(void*))); if (ibeg == nullptr) #ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); @@ -458,7 +460,8 @@ __libcpp_db::__insert_iterator(void* __i) } size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_); __i_node* p = __ibeg_[hi]; - __i_node* r = __ibeg_[hi] = (__i_node*)malloc(sizeof(__i_node)); + __i_node* r = __ibeg_[hi] = + static_cast<__i_node*>(malloc(sizeof(__i_node))); if (r == nullptr) #ifndef _LIBCPP_NO_EXCEPTIONS throw bad_alloc(); diff --git a/contrib/libc++/src/hash.cpp b/contrib/libc++/src/hash.cpp index 65a9d51..388ab2e 100644 --- a/contrib/libc++/src/hash.cpp +++ b/contrib/libc++/src/hash.cpp @@ -12,12 +12,9 @@ #include "stdexcept" #include "type_traits" -// Don't silence a non-existent warning if clang doesn't yet have this warning. #ifdef __clang__ -#if (__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 2)) #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" #endif -#endif _LIBCPP_BEGIN_NAMESPACE_STD diff --git a/contrib/libc++/src/iostream.cpp b/contrib/libc++/src/iostream.cpp index 7fc71df..f413681 100644 --- a/contrib/libc++/src/iostream.cpp +++ b/contrib/libc++/src/iostream.cpp @@ -54,13 +54,13 @@ ios_base::Init::Init() ios_base::Init::~Init() { - ostream* cout_ptr = (ostream*)cout; - ostream* clog_ptr = (ostream*)clog; + ostream* cout_ptr = reinterpret_cast<ostream*>(cout); + ostream* clog_ptr = reinterpret_cast<ostream*>(clog); cout_ptr->flush(); clog_ptr->flush(); - wostream* wcout_ptr = (wostream*)wcout; - wostream* wclog_ptr = (wostream*)wclog; + wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout); + wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog); wcout_ptr->flush(); wclog_ptr->flush(); } diff --git a/contrib/libc++/src/locale.cpp b/contrib/libc++/src/locale.cpp index 49c1cf2..b15f077 100644 --- a/contrib/libc++/src/locale.cpp +++ b/contrib/libc++/src/locale.cpp @@ -230,8 +230,10 @@ locale::__imp::__imp(const string& name, size_t refs) // NOTE avoid the `base class should be explicitly initialized in the // copy constructor` warning emitted by GCC +#if defined(__clang__) || _GNUC_VER >= 406 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wextra" +#endif locale::__imp::__imp(const __imp& other) : facets_(max<size_t>(N, other.facets_.size())), @@ -243,7 +245,9 @@ locale::__imp::__imp(const __imp& other) facets_[i]->__add_shared(); } +#if defined(__clang__) || _GNUC_VER >= 406 #pragma GCC diagnostic pop +#endif locale::__imp::__imp(const __imp& other, const string& name, locale::category c) : facets_(N), @@ -786,7 +790,7 @@ ctype<wchar_t>::do_toupper(char_type c) const { #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c; -#elif defined(__GLIBC__) || defined(EMSCRIPTEN) +#elif defined(__GLIBC__) || defined(EMSCRIPTEN) || defined(__NetBSD__) return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c; #else return (isascii(c) && iswlower_l(c, __cloc())) ? c-L'a'+L'A' : c; @@ -799,7 +803,7 @@ ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const for (; low != high; ++low) #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low; -#elif defined(__GLIBC__) || defined(EMSCRIPTEN) +#elif defined(__GLIBC__) || defined(EMSCRIPTEN) || defined(__NetBSD__) *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low] : *low; #else @@ -813,7 +817,7 @@ ctype<wchar_t>::do_tolower(char_type c) const { #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c; -#elif defined(__GLIBC__) || defined(EMSCRIPTEN) +#elif defined(__GLIBC__) || defined(EMSCRIPTEN) || defined(__NetBSD__) return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c; #else return (isascii(c) && isupper_l(c, __cloc())) ? c-L'A'+'a' : c; @@ -826,7 +830,7 @@ ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const for (; low != high; ++low) #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low; -#elif defined(__GLIBC__) || defined(EMSCRIPTEN) +#elif defined(__GLIBC__) || defined(EMSCRIPTEN) || defined(__NetBSD__) *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low] : *low; #else @@ -893,9 +897,11 @@ ctype<char>::do_toupper(char_type c) const #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE return isascii(c) ? static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c; +#elif defined(__NetBSD__) + return static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]); #elif defined(__GLIBC__) || defined(EMSCRIPTEN) return isascii(c) ? - static_cast<char>(__classic_upper_table()[static_cast<size_t>(c)]) : c; + static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]) : c; #else return (isascii(c) && islower_l(c, __cloc())) ? c-'a'+'A' : c; #endif @@ -908,6 +914,8 @@ ctype<char>::do_toupper(char_type* low, const char_type* high) const #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low; +#elif defined(__NetBSD__) + *low = static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(*low)]); #elif defined(__GLIBC__) || defined(EMSCRIPTEN) *low = isascii(*low) ? static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low; @@ -923,7 +931,9 @@ ctype<char>::do_tolower(char_type c) const #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE return isascii(c) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c; -#elif defined(__GLIBC__) || defined(EMSCRIPTEN) +#elif defined(__NetBSD__) + return static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(c)]); +#elif defined(__GLIBC__) || defined(EMSCRIPTEN) || defined(__NetBSD__) return isascii(c) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c; #else @@ -937,6 +947,8 @@ ctype<char>::do_tolower(char_type* low, const char_type* high) const for (; low != high; ++low) #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low; +#elif defined(__NetBSD__) + *low = static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(*low)]); #elif defined(__GLIBC__) || defined(EMSCRIPTEN) *low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low; #else @@ -989,6 +1001,8 @@ ctype<char>::classic_table() _NOEXCEPT { #if defined(__APPLE__) || defined(__FreeBSD__) return _DefaultRuneLocale.__runetype; +#elif defined(__NetBSD__) + return _C_ctype_tab_ + 1; #elif defined(__GLIBC__) return __cloc()->__ctype_b; #elif __sun__ @@ -1020,9 +1034,20 @@ ctype<char>::__classic_upper_table() _NOEXCEPT { return __cloc()->__ctype_toupper; } -#endif // __GLIBC__ +#elif __NetBSD__ +const short* +ctype<char>::__classic_lower_table() _NOEXCEPT +{ + return _C_tolower_tab_ + 1; +} -#if defined(EMSCRIPTEN) +const short* +ctype<char>::__classic_upper_table() _NOEXCEPT +{ + return _C_toupper_tab_ + 1; +} + +#elif defined(EMSCRIPTEN) const int* ctype<char>::__classic_lower_table() _NOEXCEPT { @@ -1034,7 +1059,7 @@ ctype<char>::__classic_upper_table() _NOEXCEPT { return *__ctype_toupper_loc(); } -#endif // EMSCRIPTEN +#endif // __GLIBC__ || EMSCRIPTEN || __NETBSD__ // template <> class ctype_byname<char> @@ -1068,28 +1093,28 @@ ctype_byname<char>::~ctype_byname() char ctype_byname<char>::do_toupper(char_type c) const { - return static_cast<char>(toupper_l(c, __l)); + return static_cast<char>(toupper_l(static_cast<unsigned char>(c), __l)); } const char* ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const { for (; low != high; ++low) - *low = static_cast<char>(toupper_l(*low, __l)); + *low = static_cast<char>(toupper_l(static_cast<unsigned char>(*low), __l)); return low; } char ctype_byname<char>::do_tolower(char_type c) const { - return static_cast<char>(tolower_l(c, __l)); + return static_cast<char>(tolower_l(static_cast<unsigned char>(c), __l)); } const char* ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const { for (; low != high; ++low) - *low = static_cast<char>(tolower_l(*low, __l)); + *low = static_cast<char>(tolower_l(static_cast<unsigned char>(*low), __l)); return low; } @@ -1372,7 +1397,7 @@ locale::id codecvt<wchar_t, char, mbstate_t>::id; codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs) : locale::facet(refs), - __l(0) + __l(_LIBCPP_GET_C_LOCALE) { } @@ -1389,7 +1414,7 @@ codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs) codecvt<wchar_t, char, mbstate_t>::~codecvt() { - if (__l != 0) + if (__l != _LIBCPP_GET_C_LOCALE) freelocale(__l); } @@ -5315,7 +5340,7 @@ __time_put::__time_put(const string& nm) __time_put::~__time_put() { - if (__loc_) + if (__loc_ != _LIBCPP_GET_C_LOCALE) freelocale(__loc_); } diff --git a/contrib/libc++/src/stdexcept.cpp b/contrib/libc++/src/stdexcept.cpp index 0c4e832..8d25f3e 100644 --- a/contrib/libc++/src/stdexcept.cpp +++ b/contrib/libc++/src/stdexcept.cpp @@ -61,7 +61,7 @@ __libcpp_nmstr::__libcpp_nmstr(const char* msg) c[0] = c[1] = len; str_ += offset; count() = 0; - std::strcpy(const_cast<char*>(c_str()), msg); + std::memcpy(const_cast<char*>(c_str()), msg, len + 1); } inline diff --git a/contrib/libc++/src/string.cpp b/contrib/libc++/src/string.cpp index c71af4f..c6fe408 100644 --- a/contrib/libc++/src/string.cpp +++ b/contrib/libc++/src/string.cpp @@ -11,6 +11,8 @@ #include "cstdlib" #include "cwchar" #include "cerrno" +#include "limits" +#include "stdexcept" #ifdef _WIN32 #include "support/win32/support.h" #endif // _WIN32 @@ -26,662 +28,499 @@ template string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&); -int -stoi(const string& str, size_t* idx, int base) +namespace +{ + +template<typename T> +inline +void throw_helper( const string& msg ) +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + throw T( msg ); +#else + abort(); +#endif +} + +inline +void throw_from_string_out_of_range( const string& func ) +{ + throw_helper<out_of_range>(func + ": out of range"); +} + +inline +void throw_from_string_invalid_arg( const string& func ) +{ + throw_helper<invalid_argument>(func + ": no conversion"); +} + +// as_integer + +template<typename V, typename S, typename F> +inline +V +as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f) { - char* ptr; - const char* const p = str.c_str(); + typename S::value_type* ptr; + const typename S::value_type* const p = str.c_str(); typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; - long r = strtol(p, &ptr, base); + V r = f(p, &ptr, base); swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE || r < numeric_limits<int>::min() || - numeric_limits<int>::max() < r) - throw out_of_range("stoi: out of range"); + if (errno_save == ERANGE) + throw_from_string_out_of_range(func); if (ptr == p) - throw invalid_argument("stoi: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS + throw_from_string_invalid_arg(func); if (idx) *idx = static_cast<size_t>(ptr - p); + return r; +} + +template<typename V, typename S> +inline +V +as_integer(const string& func, const S& s, size_t* idx, int base); + +// string +template<> +inline +int +as_integer(const string& func, const string& s, size_t* idx, int base ) +{ + // Use long as no Stantard string to integer exists. + long r = as_integer_helper<long>( func, s, idx, base, strtol ); + if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) + throw_from_string_out_of_range(func); return static_cast<int>(r); } +template<> +inline +long +as_integer(const string& func, const string& s, size_t* idx, int base ) +{ + return as_integer_helper<long>( func, s, idx, base, strtol ); +} + +template<> +inline +unsigned long +as_integer( const string& func, const string& s, size_t* idx, int base ) +{ + return as_integer_helper<unsigned long>( func, s, idx, base, strtoul ); +} + +template<> +inline +long long +as_integer( const string& func, const string& s, size_t* idx, int base ) +{ + return as_integer_helper<long long>( func, s, idx, base, strtoll ); +} + +template<> +inline +unsigned long long +as_integer( const string& func, const string& s, size_t* idx, int base ) +{ + return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull ); +} + +// wstring +template<> +inline int -stoi(const wstring& str, size_t* idx, int base) +as_integer( const string& func, const wstring& s, size_t* idx, int base ) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - long r = wcstol(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE || r < numeric_limits<int>::min() || - numeric_limits<int>::max() < r) - throw out_of_range("stoi: out of range"); - if (ptr == p) - throw invalid_argument("stoi: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); + // Use long as no Stantard string to integer exists. + long r = as_integer_helper<long>( func, s, idx, base, wcstol ); + if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) + throw_from_string_out_of_range(func); return static_cast<int>(r); } +template<> +inline long -stol(const string& str, size_t* idx, int base) +as_integer( const string& func, const wstring& s, size_t* idx, int base ) +{ + return as_integer_helper<long>( func, s, idx, base, wcstol ); +} + +template<> +inline +unsigned long +as_integer( const string& func, const wstring& s, size_t* idx, int base ) +{ + return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul ); +} + +template<> +inline +long long +as_integer( const string& func, const wstring& s, size_t* idx, int base ) +{ + return as_integer_helper<long long>( func, s, idx, base, wcstoll ); +} + +template<> +inline +unsigned long long +as_integer( const string& func, const wstring& s, size_t* idx, int base ) +{ + return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull ); +} + +// as_float + +template<typename V, typename S, typename F> +inline +V +as_float_helper(const string& func, const S& str, size_t* idx, F f ) { - char* ptr; - const char* const p = str.c_str(); + typename S::value_type* ptr; + const typename S::value_type* const p = str.c_str(); typename remove_reference<decltype(errno)>::type errno_save = errno; errno = 0; - long r = strtol(p, &ptr, base); + V r = f(p, &ptr); swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS if (errno_save == ERANGE) - throw out_of_range("stol: out of range"); + throw_from_string_out_of_range(func); if (ptr == p) - throw invalid_argument("stol: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS + throw_from_string_invalid_arg(func); if (idx) *idx = static_cast<size_t>(ptr - p); return r; } +template<typename V, typename S> +inline +V as_float( const string& func, const S& s, size_t* idx = nullptr ); + +template<> +inline +float +as_float( const string& func, const string& s, size_t* idx ) +{ + return as_float_helper<float>( func, s, idx, strtof ); +} + +template<> +inline +double +as_float(const string& func, const string& s, size_t* idx ) +{ + return as_float_helper<double>( func, s, idx, strtod ); +} + +template<> +inline +long double +as_float( const string& func, const string& s, size_t* idx ) +{ + return as_float_helper<long double>( func, s, idx, strtold ); +} + +template<> +inline +float +as_float( const string& func, const wstring& s, size_t* idx ) +{ + return as_float_helper<float>( func, s, idx, wcstof ); +} + +template<> +inline +double +as_float( const string& func, const wstring& s, size_t* idx ) +{ + return as_float_helper<double>( func, s, idx, wcstod ); +} + +template<> +inline +long double +as_float( const string& func, const wstring& s, size_t* idx ) +{ + return as_float_helper<long double>( func, s, idx, wcstold ); +} + +} // unnamed namespace + +int +stoi(const string& str, size_t* idx, int base) +{ + return as_integer<int>( "stoi", str, idx, base ); +} + +int +stoi(const wstring& str, size_t* idx, int base) +{ + return as_integer<int>( "stoi", str, idx, base ); +} + +long +stol(const string& str, size_t* idx, int base) +{ + return as_integer<long>( "stol", str, idx, base ); +} + long stol(const wstring& str, size_t* idx, int base) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - long r = wcstol(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stol: out of range"); - if (ptr == p) - throw invalid_argument("stol: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<long>( "stol", str, idx, base ); } unsigned long stoul(const string& str, size_t* idx, int base) { - char* ptr; - const char* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - unsigned long r = strtoul(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stoul: out of range"); - if (ptr == p) - throw invalid_argument("stoul: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<unsigned long>( "stoul", str, idx, base ); } unsigned long stoul(const wstring& str, size_t* idx, int base) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - unsigned long r = wcstoul(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stoul: out of range"); - if (ptr == p) - throw invalid_argument("stoul: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<unsigned long>( "stoul", str, idx, base ); } long long stoll(const string& str, size_t* idx, int base) { - char* ptr; - const char* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - long long r = strtoll(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stoll: out of range"); - if (ptr == p) - throw invalid_argument("stoll: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<long long>( "stoll", str, idx, base ); } long long stoll(const wstring& str, size_t* idx, int base) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - long long r = wcstoll(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stoll: out of range"); - if (ptr == p) - throw invalid_argument("stoll: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<long long>( "stoll", str, idx, base ); } unsigned long long stoull(const string& str, size_t* idx, int base) { - char* ptr; - const char* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - unsigned long long r = strtoull(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stoull: out of range"); - if (ptr == p) - throw invalid_argument("stoull: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<unsigned long long>( "stoull", str, idx, base ); } unsigned long long stoull(const wstring& str, size_t* idx, int base) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - unsigned long long r = wcstoull(p, &ptr, base); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stoull: out of range"); - if (ptr == p) - throw invalid_argument("stoull: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_integer<unsigned long long>( "stoull", str, idx, base ); } float stof(const string& str, size_t* idx) { - char* ptr; - const char* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - float r = strtof(p, &ptr); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stof: out of range"); - if (ptr == p) - throw invalid_argument("stof: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_float<float>( "stof", str, idx ); } float stof(const wstring& str, size_t* idx) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - float r = wcstof(p, &ptr); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stof: out of range"); - if (ptr == p) - throw invalid_argument("stof: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_float<float>( "stof", str, idx ); } double stod(const string& str, size_t* idx) { - char* ptr; - const char* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - double r = strtod(p, &ptr); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stod: out of range"); - if (ptr == p) - throw invalid_argument("stod: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_float<double>( "stod", str, idx ); } double stod(const wstring& str, size_t* idx) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - double r = wcstod(p, &ptr); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stod: out of range"); - if (ptr == p) - throw invalid_argument("stod: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_float<double>( "stod", str, idx ); } long double stold(const string& str, size_t* idx) { - char* ptr; - const char* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - long double r = strtold(p, &ptr); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stold: out of range"); - if (ptr == p) - throw invalid_argument("stold: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_float<long double>( "stold", str, idx ); } long double stold(const wstring& str, size_t* idx) { - wchar_t* ptr; - const wchar_t* const p = str.c_str(); - typename remove_reference<decltype(errno)>::type errno_save = errno; - errno = 0; - long double r = wcstold(p, &ptr); - swap(errno, errno_save); -#ifndef _LIBCPP_NO_EXCEPTIONS - if (errno_save == ERANGE) - throw out_of_range("stold: out of range"); - if (ptr == p) - throw invalid_argument("stold: no conversion"); -#endif // _LIBCPP_NO_EXCEPTIONS - if (idx) - *idx = static_cast<size_t>(ptr - p); - return r; + return as_float<long double>( "stold", str, idx ); } -string to_string(int val) +// to_string + +namespace +{ + +// as_string + +template<typename S, typename P, typename V > +inline +S +as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) { - string s; - s.resize(s.capacity()); + typedef typename S::size_type size_type; + size_type available = s.size(); while (true) { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%d", val)); - if (n2 <= s.size()) + int status = sprintf_like(&s[0], available + 1, fmt, a); + if ( status >= 0 ) { - s.resize(n2); - break; + size_type used = static_cast<size_type>(status); + if ( used <= available ) + { + s.resize( used ); + break; + } + available = used; // Assume this is advice of how much space we need. } - s.resize(n2); + else + available = available * 2 + 1; + s.resize(available); } return s; } -string to_string(unsigned val) +template <class S, class V, bool = is_floating_point<V>::value> +struct initial_string; + +template <class V, bool b> +struct initial_string<string, V, b> { - string s; - s.resize(s.capacity()); - while (true) + string + operator()() const { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%u", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); + string s; + s.resize(s.capacity()); + return s; } - return s; -} +}; -string to_string(long val) +template <class V> +struct initial_string<wstring, V, false> { - string s; - s.resize(s.capacity()); - while (true) + wstring + operator()() const { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%ld", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); + const size_t n = (numeric_limits<unsigned long long>::digits / 3) + + ((numeric_limits<unsigned long long>::digits % 3) != 0) + + 1; + wstring s(n, wchar_t()); + s.resize(s.capacity()); + return s; } - return s; -} +}; -string to_string(unsigned long val) +template <class V> +struct initial_string<wstring, V, true> { - string s; - s.resize(s.capacity()); - while (true) + wstring + operator()() const { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lu", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); + wstring s(20, wchar_t()); + s.resize(s.capacity()); + return s; } - return s; +}; + +typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...); + +inline +wide_printf +get_swprintf() +{ +#ifndef _WIN32 + return swprintf; +#else + return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf); +#endif +} + +} // unnamed namespace + +string to_string(int val) +{ + return as_string(snprintf, initial_string<string, int>()(), "%d", val); +} + +string to_string(unsigned val) +{ + return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val); +} + +string to_string(long val) +{ + return as_string(snprintf, initial_string<string, long>()(), "%ld", val); +} + +string to_string(unsigned long val) +{ + return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val); } string to_string(long long val) { - string s; - s.resize(s.capacity()); - while (true) - { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lld", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); - } - return s; + return as_string(snprintf, initial_string<string, long long>()(), "%lld", val); } string to_string(unsigned long long val) { - string s; - s.resize(s.capacity()); - while (true) - { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%llu", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); - } - return s; + return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val); } string to_string(float val) { - string s; - s.resize(s.capacity()); - while (true) - { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); - } - return s; + return as_string(snprintf, initial_string<string, float>()(), "%f", val); } string to_string(double val) { - string s; - s.resize(s.capacity()); - while (true) - { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); - } - return s; + return as_string(snprintf, initial_string<string, double>()(), "%f", val); } string to_string(long double val) { - string s; - s.resize(s.capacity()); - while (true) - { - size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%Lf", val)); - if (n2 <= s.size()) - { - s.resize(n2); - break; - } - s.resize(n2); - } - return s; + return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val); } wstring to_wstring(int val) { - const size_t n = (numeric_limits<int>::digits / 3) - + ((numeric_limits<int>::digits % 3) != 0) - + 1; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%d", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val); } wstring to_wstring(unsigned val) { - const size_t n = (numeric_limits<unsigned>::digits / 3) - + ((numeric_limits<unsigned>::digits % 3) != 0) - + 1; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%u", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val); } wstring to_wstring(long val) { - const size_t n = (numeric_limits<long>::digits / 3) - + ((numeric_limits<long>::digits % 3) != 0) - + 1; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%ld", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val); } wstring to_wstring(unsigned long val) { - const size_t n = (numeric_limits<unsigned long>::digits / 3) - + ((numeric_limits<unsigned long>::digits % 3) != 0) - + 1; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%lu", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val); } wstring to_wstring(long long val) { - const size_t n = (numeric_limits<long long>::digits / 3) - + ((numeric_limits<long long>::digits % 3) != 0) - + 1; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%lld", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val); } wstring to_wstring(unsigned long long val) { - const size_t n = (numeric_limits<unsigned long long>::digits / 3) - + ((numeric_limits<unsigned long long>::digits % 3) != 0) - + 1; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%llu", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val); } wstring to_wstring(float val) { - const size_t n = 20; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%f", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val); } wstring to_wstring(double val) { - const size_t n = 20; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%f", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val); } wstring to_wstring(long double val) { - const size_t n = 20; - wstring s(n, wchar_t()); - s.resize(s.capacity()); - while (true) - { - int n2 = swprintf(&s[0], s.size()+1, L"%Lf", val); - if (n2 > 0) - { - s.resize(static_cast<size_t>(n2)); - break; - } - s.resize(2*s.size()); - s.resize(s.capacity()); - } - return s; + return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val); } - _LIBCPP_END_NAMESPACE_STD diff --git a/contrib/libc++/src/thread.cpp b/contrib/libc++/src/thread.cpp index c6f6748..1fd8bb0 100644 --- a/contrib/libc++/src/thread.cpp +++ b/contrib/libc++/src/thread.cpp @@ -16,11 +16,17 @@ #if !defined(_WIN32) #if !defined(__sun__) && !defined(__linux__) #include <sys/sysctl.h> -#else -#include <unistd.h> #endif // !__sun__ && !__linux__ +#include <unistd.h> #endif // !_WIN32 +#if defined(__NetBSD__) +#pragma weak pthread_create // Do not create libpthread dependency +#endif +#if defined(_WIN32) +#include <windows.h> +#endif + _LIBCPP_BEGIN_NAMESPACE_STD thread::~thread() @@ -67,7 +73,7 @@ thread::hardware_concurrency() _NOEXCEPT std::size_t s = sizeof(n); sysctl(mib, 2, &n, &s, 0, 0); return n; -#elif (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)) || defined(EMSCRIPTEN) +#elif defined(_SC_NPROCESSORS_ONLN) long result = sysconf(_SC_NPROCESSORS_ONLN); // sysconf returns -1 if the name is invalid, the option does not exist or // does not have a definite limit. @@ -76,9 +82,14 @@ thread::hardware_concurrency() _NOEXCEPT if (result < 0) return 0; return static_cast<unsigned>(result); +#elif defined(_WIN32) + SYSTEM_INFO info; + GetSystemInfo(&info); + return info.dwNumberOfProcessors; #else // defined(CTL_HW) && defined(HW_NCPU) // TODO: grovel through /proc or check cpuid on x86 and similar // instructions on other architectures. +#warning hardware_concurrency not yet implemented return 0; // Means not computable [thread.thread.static] #endif // defined(CTL_HW) && defined(HW_NCPU) } |