diff options
author | Nick Mathewson <nickm@torproject.org> | 2017-12-13 08:54:29 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-12-13 08:54:29 -0500 |
commit | dd6dec2665af9964d8f940c27f3f0815a649424a (patch) | |
tree | b584ee84311bba9a26f3e4c6dbb1f7b52442d433 /src/common/compat_time.c | |
parent | 4c877ae87483d6e63c9e0309eb8abc009f9b9b87 (diff) | |
download | tor-dd6dec2665af9964d8f940c27f3f0815a649424a.tar.gz tor-dd6dec2665af9964d8f940c27f3f0815a649424a.zip |
Add a function to add msec to a monotime.
We'll use this for the channel padding logic.
Diffstat (limited to 'src/common/compat_time.c')
-rw-r--r-- | src/common/compat_time.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/common/compat_time.c b/src/common/compat_time.c index fa8dbdfac0..396a7b7540 100644 --- a/src/common/compat_time.c +++ b/src/common/compat_time.c @@ -357,6 +357,14 @@ monotime_is_zero(const monotime_t *val) return val->abstime_ == 0; } +void +monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec) +{ + const uint64_t nsec = msec * ONE_MILLION; + const uint64_t ticks = (nsec * mach_time_info.denom) / mach_time_info.numer; + out->abstime_ = val->abstime_ + ticks; +} + /* end of "__APPLE__" */ #elif defined(HAVE_CLOCK_GETTIME) @@ -453,6 +461,19 @@ monotime_is_zero(const monotime_t *val) return val->ts_.tv_sec == 0 && val->ts_.tv_nsec == 0; } +void +monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec) +{ + const uint32_t sec = msec / 1000; + const uint32_t msec_remainder = msec % 1000; + out->ts_.tv_sec = val->ts_.tv_sec + sec; + out->ts_.tv_nsec = val->ts_.tv_nsec + (msec_remainder * ONE_MILLION); + if (out->ts_.tv_nsec > ONE_BILLION) { + out->ts_.tv_nsec -= ONE_BILLION; + out->ts_.tv_sec += 1; + } +} + /* end of "HAVE_CLOCK_GETTIME" */ #elif defined (_WIN32) @@ -605,6 +626,20 @@ monotime_coarse_is_zero(const monotime_coarse_t *val) return val->tick_count_ == 0; } +void +monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec) +{ + const uint64_t nsec = msec * ONE_MILLION; + const uint64_t ticks = (nsec * nsec_per_tick_denom) / nsec_per_tick_numer; + out->pcount_ = val->pcount_ + ticks; +} + +void +monotime_coarse_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec) +{ + out->tick_count_ = val->tick_count_ + msec; +} + /* end of "_WIN32" */ #elif defined(MONOTIME_USING_GETTIMEOFDAY) @@ -658,6 +693,19 @@ monotime_is_zero(const monotime_t *val) return val->tv_.tv_sec == 0 && val->tv_.tv_usec == 0; } +void +monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec) +{ + const uint32_t sec = msec / 1000; + const uint32_t msec_remainder = msec % 1000; + out->tv_.tv_sec = val->tv_.tv_sec + sec; + out->tv_.tv_usec = val->tv_.tv_nsec + (msec_remainder * 1000); + if (out->tv_.tv_usec > ONE_MILLION) { + out->tv_.tv_usec -= ONE_MILLION; + out->tv_.tv_sec += 1; + } +} + /* end of "MONOTIME_USING_GETTIMEOFDAY" */ #else #error "No way to implement monotonic timers." |