Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions stl/src/stdhndlr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

// set_new_handler

#include <new.h>
#include <new>

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
Expand Down
69 changes: 36 additions & 33 deletions stl/src/xtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,57 @@

#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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been hard work causing all the merge conflicts on my own since @BillyONeal left the team.

constexpr long long _Epoch = 0x19DB1DED53E8000LL;

FILETIME ft;
__crtGetSystemTimePreciseAsFileTime(&ft);
return ((static_cast<long long>(ft.dwHighDateTime)) << 32) + static_cast<long long>(ft.dwLowDateTime) - _Epoch;
}

// 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<long>(now % _Nsec100_per_sec) * 100;
Expand Down