]>
git.proxmox.com Git - rustc.git/blob - src/jemalloc/src/nstime.c
1 #include "jemalloc/internal/jemalloc_internal.h"
3 #define BILLION UINT64_C(1000000000)
6 nstime_init(nstime_t
*time
, uint64_t ns
)
13 nstime_init2(nstime_t
*time
, uint64_t sec
, uint64_t nsec
)
16 time
->ns
= sec
* BILLION
+ nsec
;
20 nstime_ns(const nstime_t
*time
)
27 nstime_sec(const nstime_t
*time
)
30 return (time
->ns
/ BILLION
);
34 nstime_nsec(const nstime_t
*time
)
37 return (time
->ns
% BILLION
);
41 nstime_copy(nstime_t
*time
, const nstime_t
*source
)
48 nstime_compare(const nstime_t
*a
, const nstime_t
*b
)
51 return ((a
->ns
> b
->ns
) - (a
->ns
< b
->ns
));
55 nstime_add(nstime_t
*time
, const nstime_t
*addend
)
58 assert(UINT64_MAX
- time
->ns
>= addend
->ns
);
60 time
->ns
+= addend
->ns
;
64 nstime_subtract(nstime_t
*time
, const nstime_t
*subtrahend
)
67 assert(nstime_compare(time
, subtrahend
) >= 0);
69 time
->ns
-= subtrahend
->ns
;
73 nstime_imultiply(nstime_t
*time
, uint64_t multiplier
)
76 assert((((time
->ns
| multiplier
) & (UINT64_MAX
<< (sizeof(uint64_t) <<
77 2))) == 0) || ((time
->ns
* multiplier
) / multiplier
== time
->ns
));
79 time
->ns
*= multiplier
;
83 nstime_idivide(nstime_t
*time
, uint64_t divisor
)
92 nstime_divide(const nstime_t
*time
, const nstime_t
*divisor
)
95 assert(divisor
->ns
!= 0);
97 return (time
->ns
/ divisor
->ns
);
101 # define NSTIME_MONOTONIC true
103 nstime_get(nstime_t
*time
)
106 uint64_t ticks_100ns
;
108 GetSystemTimeAsFileTime(&ft
);
109 ticks_100ns
= (((uint64_t)ft
.dwHighDateTime
) << 32) | ft
.dwLowDateTime
;
111 nstime_init(time
, ticks_100ns
* 100);
113 #elif JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE
114 # define NSTIME_MONOTONIC true
116 nstime_get(nstime_t
*time
)
120 clock_gettime(CLOCK_MONOTONIC_COARSE
, &ts
);
121 nstime_init2(time
, ts
.tv_sec
, ts
.tv_nsec
);
123 #elif JEMALLOC_HAVE_CLOCK_MONOTONIC
124 # define NSTIME_MONOTONIC true
126 nstime_get(nstime_t
*time
)
130 clock_gettime(CLOCK_MONOTONIC
, &ts
);
131 nstime_init2(time
, ts
.tv_sec
, ts
.tv_nsec
);
133 #elif JEMALLOC_HAVE_MACH_ABSOLUTE_TIME
134 # define NSTIME_MONOTONIC true
136 nstime_get(nstime_t
*time
)
139 nstime_init(time
, mach_absolute_time());
142 # define NSTIME_MONOTONIC false
144 nstime_get(nstime_t
*time
)
148 gettimeofday(&tv
, NULL
);
149 nstime_init2(time
, tv
.tv_sec
, tv
.tv_usec
* 1000);
154 #undef nstime_monotonic
155 #define nstime_monotonic JEMALLOC_N(n_nstime_monotonic)
158 nstime_monotonic(void)
161 return (NSTIME_MONOTONIC
);
162 #undef NSTIME_MONOTONIC
165 #undef nstime_monotonic
166 #define nstime_monotonic JEMALLOC_N(nstime_monotonic)
167 nstime_monotonic_t
*nstime_monotonic
= JEMALLOC_N(n_nstime_monotonic
);
172 #define nstime_update JEMALLOC_N(n_nstime_update)
175 nstime_update(nstime_t
*time
)
179 nstime_copy(&old_time
, time
);
182 /* Handle non-monotonic clocks. */
183 if (unlikely(nstime_compare(&old_time
, time
) > 0)) {
184 nstime_copy(time
, &old_time
);
192 #define nstime_update JEMALLOC_N(nstime_update)
193 nstime_update_t
*nstime_update
= JEMALLOC_N(n_nstime_update
);