LONGLONG frequency;
LARGE_INTEGER freq;
- if (!QueryPerformanceFrequency(&freq)) {
- if (raise) {
- PyErr_SetFromWindowsErr(0);
- }
- return -1;
- }
+ // Since Windows XP, the function cannot fail.
+ (void)QueryPerformanceFrequency(&freq);
frequency = freq.QuadPart;
- /* Sanity check: should never occur in practice */
- if (frequency < 1) {
- if (raise) {
- PyErr_SetString(PyExc_RuntimeError,
- "invalid QueryPerformanceFrequency");
- }
- return -1;
- }
-
- /* Check that frequency can be casted to _PyTime_t.
+ // Since Windows XP, frequency cannot be zero.
+ assert(frequency >= 1);
- Make also sure that (ticks * SEC_TO_NS) cannot overflow in
+ /* Make also sure that (ticks * SEC_TO_NS) cannot overflow in
_PyTime_MulDiv(), with ticks < frequency.
Known QueryPerformanceFrequency() values:
* 3,579,545 Hz (3.6 MHz): 279 ns resolution
None of these frequencies can overflow with 64-bit _PyTime_t, but
- check for overflow, just in case. */
- if (frequency > _PyTime_MAX
- || frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS)
- {
+ check for integer overflow just in case. */
+ if (frequency > _PyTime_MAX / SEC_TO_NS) {
if (raise) {
PyErr_SetString(PyExc_OverflowError,
"QueryPerformanceFrequency is too large");