From 5447f8514998fcba38b301875aeb76d556a52420 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 17 Oct 2023 19:45:03 -0700 Subject: [PATCH 1/3] xtime.cpp: The `_Epoch` and `_Nsec100_per_sec` constants can be function-local. --- stl/src/xtime.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stl/src/xtime.cpp b/stl/src/xtime.cpp index 2832b9202b4..4ef3e160efe 100644 --- a/stl/src/xtime.cpp +++ b/stl/src/xtime.cpp @@ -42,12 +42,11 @@ static _timespec64 _timespec64_diff(const _timespec64* xt, const _timespec64* no return diff; } -constexpr long long _Epoch = 0x19DB1DED53E8000LL; -constexpr long _Nsec100_per_sec = _Nsec_per_sec / 100; - _EXTERN_C _CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() { // 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; @@ -55,6 +54,8 @@ _CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() { // get system time in 100-n // Used by several src files, but not dllexported. void _Timespec64_get_sys(_timespec64* xt) { // 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; From 238e2cddffb1807885ec77076d6b1f8e6ba207a0 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 17 Oct 2023 19:50:34 -0700 Subject: [PATCH 2/3] xtime.cpp: Use an unnamed namespace, following our modern convention. The `constexpr` variables and `static` functions had internal linkage, so this is unobservable to other TUs. We can drop `static` now. --- stl/src/xtime.cpp | 62 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/stl/src/xtime.cpp b/stl/src/xtime.cpp index 4ef3e160efe..09ebf88cb7d 100644 --- a/stl/src/xtime.cpp +++ b/stl/src/xtime.cpp @@ -8,39 +8,41 @@ #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; +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; + } } - 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; + // 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; } - - 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; -} +} // namespace _EXTERN_C From a49c87a0f454c0f1217ec325a278eaaec51be7a2 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 17 Oct 2023 20:11:52 -0700 Subject: [PATCH 3/3] stdhndlr.cpp: Use an unnamed namespace, following our modern convention. UCRT `` declares `_set_new_handler`, which is better than trying to declare it ourselves. We can then drop the `new_hand` typedef which was being used for that declaration. The `_New_handler` variable was `static`, so we can clearly move it into an unnamed namespace and drop the `static`. We do need to qualify the `_STD new_handler` type now. The `_New_handler_interface` helper function was never dllexported nor referred to by other stl/src TUs. We can move it out of `namespace std` and into the unnamed namespace. The resulting code has our desired distinction between TU-local and dllexported machinery. --- stl/src/stdhndlr.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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