+struct bintime {
+ time_t sec;
+ uint64_t frac;
+};
+
+static __inline void
+bintime_addx(struct bintime *_bt, uint64_t _x)
+{
+ uint64_t _u;
+
+ _u = _bt->frac;
+ _bt->frac += _x;
+ if (_u > _bt->frac)
+ _bt->sec++;
+}
+
+static __inline void
+bintime_subx(struct bintime *_bt, uint64_t _x)
+{
+ uint64_t _u;
+
+ _u = _bt->frac;
+ _bt->frac -= _x;
+ if (_u < _bt->frac)
+ _bt->sec--;
+}
+
+static __inline void
+bintime_addns(struct bintime *bt, uint64_t ns)
+{
+ bt->sec += ns/ (uint64_t)NSEC_PER_SEC;
+ ns = ns % (uint64_t)NSEC_PER_SEC;
+ if (ns) {
+ /* 18446744073 = int(2^64 / NSEC_PER_SEC) */
+ ns = ns * (uint64_t)18446744073LL;
+ bintime_addx(bt, ns);
+ }
+}
+
+static __inline void
+bintime_subns(struct bintime *bt, uint64_t ns)
+{
+ bt->sec -= ns/ (uint64_t)NSEC_PER_SEC;
+ ns = ns % (uint64_t)NSEC_PER_SEC;
+ if (ns) {
+ /* 18446744073 = int(2^64 / NSEC_PER_SEC) */
+ ns = ns * (uint64_t)18446744073LL;
+ bintime_subx(bt, ns);
+ }
+}
+
+static __inline void
+bintime_addxns(struct bintime *bt, uint64_t a, int64_t xns)
+{
+ uint64_t uxns = (xns > 0)?(uint64_t )xns:(uint64_t)-xns;
+ uint64_t ns = multi_overflow(a, uxns);
+ if (xns > 0) {
+ if (ns)
+ bintime_addns(bt, ns);
+ ns = (a * uxns) / (uint64_t)NSEC_PER_SEC;
+ bintime_addx(bt, ns);
+ }
+ else{
+ if (ns)
+ bintime_subns(bt, ns);
+ ns = (a * uxns) / (uint64_t)NSEC_PER_SEC;
+ bintime_subx(bt,ns);
+ }
+}
+
+
+static __inline void
+bintime_add(struct bintime *_bt, const struct bintime *_bt2)
+{
+ uint64_t _u;
+
+ _u = _bt->frac;
+ _bt->frac += _bt2->frac;
+ if (_u > _bt->frac)
+ _bt->sec++;
+ _bt->sec += _bt2->sec;
+}
+
+static __inline void
+bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
+{
+ uint64_t _u;
+
+ _u = _bt->frac;
+ _bt->frac -= _bt2->frac;
+ if (_u < _bt->frac)
+ _bt->sec--;
+ _bt->sec -= _bt2->sec;
+}
+
+static __inline void
+clock2bintime(const clock_sec_t *secs, const clock_usec_t *microsecs, struct bintime *_bt)
+{
+
+ _bt->sec = *secs;
+ /* 18446744073709 = int(2^64 / 1000000) */
+ _bt->frac = *microsecs * (uint64_t)18446744073709LL;
+}
+
+static __inline void
+bintime2usclock(const struct bintime *_bt, clock_sec_t *secs, clock_usec_t *microsecs)
+{
+
+ *secs = _bt->sec;
+ *microsecs = ((uint64_t)USEC_PER_SEC * (uint32_t)(_bt->frac >> 32)) >> 32;
+}
+
+static __inline void
+bintime2nsclock(const struct bintime *_bt, clock_sec_t *secs, clock_usec_t *nanosecs)
+{
+
+ *secs = _bt->sec;
+ *nanosecs = ((uint64_t)NSEC_PER_SEC * (uint32_t)(_bt->frac >> 32)) >> 32;
+}
+
+static __inline void
+bintime2absolutetime(const struct bintime *_bt, uint64_t *abs)
+{
+ uint64_t nsec;
+ nsec = (uint64_t) _bt->sec * (uint64_t)NSEC_PER_SEC + (((uint64_t)NSEC_PER_SEC * (uint32_t)(_bt->frac >> 32)) >> 32);
+ nanoseconds_to_absolutetime(nsec, abs);
+}
+
+struct latched_time {
+ uint64_t monotonic_time_usec;
+ uint64_t mach_time;
+};
+
+extern int
+kernel_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);