diff --git a/stl/src/stdhndlr.cpp b/stl/src/stdhndlr.cpp index af55e61873e..91dfb77350a 100644 --- a/stl/src/stdhndlr.cpp +++ b/stl/src/stdhndlr.cpp @@ -3,19 +3,19 @@ // set_new_handler +#include #include -using new_hand = int(__cdecl*)(size_t); +namespace { + _STD new_handler _New_handler; -extern "C" new_hand __cdecl _set_new_handler(new_hand); + int __cdecl _New_handler_interface(size_t) { // interface to existing Microsoft _callnewh mechanism + _New_handler(); + return 1; + } +} // namespace _STD_BEGIN -static new_handler _New_handler; - -int __cdecl _New_handler_interface(size_t) { // interface to existing Microsoft _callnewh mechanism - _New_handler(); - return 1; -} _CRTIMP2 new_handler __cdecl set_new_handler(_In_opt_ new_handler pnew) noexcept { // remove current handler _BEGIN_LOCK(_LOCK_MALLOC) // lock thread to ensure atomicity diff --git a/stl/src/xtime.cpp b/stl/src/xtime.cpp index d6285c0f7a2..172a06975df 100644 --- a/stl/src/xtime.cpp +++ b/stl/src/xtime.cpp @@ -8,47 +8,48 @@ #include "awint.hpp" -constexpr long _Nsec_per_sec = 1000000000L; -constexpr long _Nsec_per_msec = 1000000L; -constexpr int _Msec_per_sec = 1000; - -static void _timespec64_normalize(_timespec64* xt) { // adjust so that 0 <= tv_nsec < 1 000 000 000 - while (xt->tv_nsec < 0) { // normalize target time - xt->tv_sec -= 1; - xt->tv_nsec += _Nsec_per_sec; - } - while (_Nsec_per_sec <= xt->tv_nsec) { // normalize target time - xt->tv_sec += 1; - xt->tv_nsec -= _Nsec_per_sec; - } -} - -// return _timespec64 object holding difference between xt and now, treating negative difference as 0 -static _timespec64 _timespec64_diff(const _timespec64* xt, const _timespec64* now) { - _timespec64 diff = *xt; - _timespec64_normalize(&diff); - if (diff.tv_nsec < now->tv_nsec) { // avoid underflow - diff.tv_sec -= now->tv_sec + 1; - diff.tv_nsec += _Nsec_per_sec - now->tv_nsec; - } else { // no underflow - diff.tv_sec -= now->tv_sec; - diff.tv_nsec -= now->tv_nsec; +namespace { + constexpr long _Nsec_per_sec = 1000000000L; + constexpr long _Nsec_per_msec = 1000000L; + constexpr int _Msec_per_sec = 1000; + + void _timespec64_normalize(_timespec64* xt) { // adjust so that 0 <= tv_nsec < 1 000 000 000 + while (xt->tv_nsec < 0) { // normalize target time + xt->tv_sec -= 1; + xt->tv_nsec += _Nsec_per_sec; + } + while (_Nsec_per_sec <= xt->tv_nsec) { // normalize target time + xt->tv_sec += 1; + xt->tv_nsec -= _Nsec_per_sec; + } } - if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec <= 0)) { // time is zero - diff.tv_sec = 0; - diff.tv_nsec = 0; + // return _timespec64 object holding difference between xt and now, treating negative difference as 0 + _timespec64 _timespec64_diff(const _timespec64* xt, const _timespec64* now) { + _timespec64 diff = *xt; + _timespec64_normalize(&diff); + if (diff.tv_nsec < now->tv_nsec) { // avoid underflow + diff.tv_sec -= now->tv_sec + 1; + diff.tv_nsec += _Nsec_per_sec - now->tv_nsec; + } else { // no underflow + diff.tv_sec -= now->tv_sec; + diff.tv_nsec -= now->tv_nsec; + } + + if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec <= 0)) { // time is zero + diff.tv_sec = 0; + diff.tv_nsec = 0; + } + return diff; } - return diff; -} - -constexpr long long _Epoch = 0x19DB1DED53E8000LL; -constexpr long _Nsec100_per_sec = _Nsec_per_sec / 100; +} // namespace _EXTERN_C _CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() noexcept { // get system time in 100-nanosecond intervals since the epoch + constexpr long long _Epoch = 0x19DB1DED53E8000LL; + FILETIME ft; __crtGetSystemTimePreciseAsFileTime(&ft); return ((static_cast(ft.dwHighDateTime)) << 32) + static_cast(ft.dwLowDateTime) - _Epoch; @@ -56,6 +57,8 @@ _CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() noexcept { // Used by several src files, but not dllexported. void _Timespec64_get_sys(_timespec64* xt) noexcept { // get system time with nanosecond resolution + constexpr long _Nsec100_per_sec = _Nsec_per_sec / 100; + unsigned long long now = _Xtime_get_ticks(); xt->tv_sec = static_cast<__time64_t>(now / _Nsec100_per_sec); xt->tv_nsec = static_cast(now % _Nsec100_per_sec) * 100;