2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_INTERNAL_TIME_H
11 # define OSSL_INTERNAL_TIME_H
14 # include <openssl/e_os2.h> /* uint64_t */
15 # include "internal/e_os.h" /* for struct timeval */
16 # include "internal/safe_math.h"
19 * Internal type defining a time.
20 * This should be treated as an opaque structure.
22 * The time datum is Unix's 1970 and at nanosecond precision, this gives
23 * a range of 584 years roughly.
26 uint64_t t
; /* Ticks since the epoch */
29 /* The precision of times allows this many values per second */
30 # define OSSL_TIME_SECOND ((uint64_t)1000000000)
32 /* One millisecond. */
33 # define OSSL_TIME_MS (OSSL_TIME_SECOND / 1000)
35 /* One microsecond. */
36 # define OSSL_TIME_US (OSSL_TIME_MS / 1000)
38 /* Convert a tick count into a time */
39 static ossl_unused ossl_inline OSSL_TIME
ossl_ticks2time(uint64_t ticks
)
47 /* Convert a time to a tick count */
48 static ossl_unused ossl_inline
uint64_t ossl_time2ticks(OSSL_TIME t
)
53 /* Get current time */
54 OSSL_TIME
ossl_time_now(void);
56 /* The beginning and end of the time range */
57 static ossl_unused ossl_inline OSSL_TIME
ossl_time_zero(void)
59 return ossl_ticks2time(0);
62 static ossl_unused ossl_inline OSSL_TIME
ossl_time_infinite(void)
64 return ossl_ticks2time(~(uint64_t)0);
68 /* Convert time to timeval */
69 static ossl_unused ossl_inline
70 void ossl_time_time_to_timeval(OSSL_TIME t
, struct timeval
*out
)
73 out
->tv_sec
= (long int)(t
.t
/ OSSL_TIME_SECOND
);
75 out
->tv_sec
= (time_t)(t
.t
/ OSSL_TIME_SECOND
);
77 out
->tv_usec
= (t
.t
% OSSL_TIME_SECOND
) / (OSSL_TIME_SECOND
/ 1000000);
80 /* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
81 static ossl_unused ossl_inline
82 int ossl_time_compare(OSSL_TIME a
, OSSL_TIME b
)
92 * Arithmetic operations on times.
93 * These operations are saturating, in that an overflow or underflow returns
94 * the largest or smallest value respectively.
96 OSSL_SAFE_MATH_UNSIGNED(time
, uint64_t)
98 static ossl_unused ossl_inline
99 OSSL_TIME
ossl_time_add(OSSL_TIME a
, OSSL_TIME b
)
104 r
.t
= safe_add_time(a
.t
, b
.t
, &err
);
105 return err
? ossl_time_infinite() : r
;
108 static ossl_unused ossl_inline
109 OSSL_TIME
ossl_time_subtract(OSSL_TIME a
, OSSL_TIME b
)
114 r
.t
= safe_sub_time(a
.t
, b
.t
, &err
);
115 return err
? ossl_time_zero() : r
;
118 /* Returns |a - b|. */
119 static ossl_unused ossl_inline
120 OSSL_TIME
ossl_time_abs_difference(OSSL_TIME a
, OSSL_TIME b
)
122 return a
.t
> b
.t
? ossl_time_subtract(a
, b
)
123 : ossl_time_subtract(b
, a
);
126 static ossl_unused ossl_inline
127 OSSL_TIME
ossl_time_multiply(OSSL_TIME a
, uint64_t b
)
132 r
.t
= safe_mul_time(a
.t
, b
, &err
);
133 return err
? ossl_time_infinite() : r
;
136 static ossl_unused ossl_inline
137 OSSL_TIME
ossl_time_divide(OSSL_TIME a
, uint64_t b
)
142 r
.t
= safe_div_time(a
.t
, b
, &err
);
143 return err
? ossl_time_zero() : r
;
146 /* Return higher of the two given time values. */
147 static ossl_unused ossl_inline
148 OSSL_TIME
ossl_time_max(OSSL_TIME a
, OSSL_TIME b
)
150 return a
.t
> b
.t
? a
: b
;
153 /* Return the lower of the two given time values. */
154 static ossl_unused ossl_inline
155 OSSL_TIME
ossl_time_min(OSSL_TIME a
, OSSL_TIME b
)
157 return a
.t
< b
.t
? a
: b
;