diff options
Diffstat (limited to 'hrtime.h')
| -rw-r--r-- | hrtime.h | 25 |
1 files changed, 23 insertions, 2 deletions
@@ -6,6 +6,8 @@ # include <sys/time.h> #endif +#include "internal/compilers.h" + /* * Hi-res monotonic clock. It is currently nsec resolution, which has over * 500 years of range (with an unsigned 64-bit integer). Developers @@ -36,6 +38,7 @@ #define RB_HRTIME_PER_MSEC (RB_HRTIME_PER_USEC * (rb_hrtime_t)1000) #define RB_HRTIME_PER_SEC (RB_HRTIME_PER_MSEC * (rb_hrtime_t)1000) #define RB_HRTIME_MAX UINT64_MAX +#define RB_HRTIME_MIN ((rb_hrtime_t)0) /* * Lets try to support time travelers. Lets assume anybody with a time machine @@ -60,7 +63,11 @@ rb_hrtime_mul(rb_hrtime_t a, rb_hrtime_t b) { rb_hrtime_t c; -#ifdef HAVE_BUILTIN___BUILTIN_MUL_OVERFLOW +#ifdef ckd_mul + if (ckd_mul(&c, a, b)) + return RB_HRTIME_MAX; + +#elif __has_builtin(__builtin_mul_overflow) if (__builtin_mul_overflow(a, b, &c)) return RB_HRTIME_MAX; #else @@ -80,7 +87,11 @@ rb_hrtime_add(rb_hrtime_t a, rb_hrtime_t b) { rb_hrtime_t c; -#ifdef HAVE_BUILTIN___BUILTIN_ADD_OVERFLOW +#ifdef ckd_add + if (ckd_add(&c, a, b)) + return RB_HRTIME_MAX; + +#elif __has_builtin(__builtin_add_overflow) if (__builtin_add_overflow(a, b, &c)) return RB_HRTIME_MAX; #else @@ -91,6 +102,15 @@ rb_hrtime_add(rb_hrtime_t a, rb_hrtime_t b) return c; } +static inline rb_hrtime_t +rb_hrtime_sub(rb_hrtime_t a, rb_hrtime_t b) +{ + if (a < b) { + return RB_HRTIME_MIN; + } + return a - b; +} + /* * convert a timeval struct to rb_hrtime_t, clamping at RB_HRTIME_MAX */ @@ -196,6 +216,7 @@ double2hrtime(rb_hrtime_t *hrt, double d) const double TIMESPEC_SEC_MAX_PLUS_ONE = 2.0 * (TIMESPEC_SEC_MAX_as_double / 2.0 + 1.0); if (TIMESPEC_SEC_MAX_PLUS_ONE <= d) { + *hrt = RB_HRTIME_MAX; return NULL; } else if (d <= 0) { |
