VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/include/internal/time.h@ 108403

最後變更 在這個檔案從108403是 108206,由 vboxsync 提交於 5 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.8 KB
 
1/*
2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#ifndef OSSL_INTERNAL_TIME_H
11# define OSSL_INTERNAL_TIME_H
12# pragma once
13
14# include <openssl/e_os2.h> /* uint64_t */
15# include "internal/e_os.h" /* for struct timeval */
16# include "internal/safe_math.h"
17
18/*
19 * Internal type defining a time.
20 * This should be treated as an opaque structure.
21 *
22 * The time datum is Unix's 1970 and at nanosecond precision, this gives
23 * a range of 584 years roughly.
24 */
25typedef struct {
26 uint64_t t; /* Ticks since the epoch */
27} OSSL_TIME;
28
29/* The precision of times allows this many values per second */
30# define OSSL_TIME_SECOND ((uint64_t)1000000000)
31
32/* One millisecond. */
33# define OSSL_TIME_MS (OSSL_TIME_SECOND / 1000)
34
35/* One microsecond. */
36# define OSSL_TIME_US (OSSL_TIME_MS / 1000)
37
38/* One nanosecond. */
39# define OSSL_TIME_NS (OSSL_TIME_US / 1000)
40
41#define ossl_seconds2time(s) ossl_ticks2time((s) * OSSL_TIME_SECOND)
42#define ossl_time2seconds(t) (ossl_time2ticks(t) / OSSL_TIME_SECOND)
43#define ossl_ms2time(ms) ossl_ticks2time((ms) * OSSL_TIME_MS)
44#define ossl_time2ms(t) (ossl_time2ticks(t) / OSSL_TIME_MS)
45#define ossl_us2time(us) ossl_ticks2time((us) * OSSL_TIME_US)
46#define ossl_time2us(t) (ossl_time2ticks(t) / OSSL_TIME_US)
47
48/*
49 * Arithmetic operations on times.
50 * These operations are saturating, in that an overflow or underflow returns
51 * the largest or smallest value respectively.
52 */
53OSSL_SAFE_MATH_UNSIGNED(time, uint64_t)
54
55/* Convert a tick count into a time */
56static ossl_unused ossl_inline
57OSSL_TIME ossl_ticks2time(uint64_t ticks)
58{
59 OSSL_TIME r;
60
61 r.t = ticks;
62 return r;
63}
64
65/* Convert a time to a tick count */
66static ossl_unused ossl_inline
67uint64_t ossl_time2ticks(OSSL_TIME t)
68{
69 return t.t;
70}
71
72/* Get current time */
73OSSL_TIME ossl_time_now(void);
74
75/* The beginning and end of the time range */
76static ossl_unused ossl_inline
77OSSL_TIME ossl_time_zero(void)
78{
79 return ossl_ticks2time(0);
80}
81
82static ossl_unused ossl_inline
83OSSL_TIME ossl_time_infinite(void)
84{
85 return ossl_ticks2time(~(uint64_t)0);
86}
87
88
89/* Convert time to timeval */
90static ossl_unused ossl_inline
91struct timeval ossl_time_to_timeval(OSSL_TIME t)
92{
93 struct timeval tv;
94 int err = 0;
95
96 /*
97 * Round up any nano secs which struct timeval doesn't support. Ensures that
98 * we never return a zero time if the input time is non zero
99 */
100 t.t = safe_add_time(t.t, OSSL_TIME_US - 1, &err);
101 if (err)
102 t = ossl_time_infinite();
103
104#ifdef _WIN32
105 tv.tv_sec = (long int)(t.t / OSSL_TIME_SECOND);
106#else
107 tv.tv_sec = (time_t)(t.t / OSSL_TIME_SECOND);
108#endif
109 tv.tv_usec = (t.t % OSSL_TIME_SECOND) / OSSL_TIME_US;
110 return tv;
111}
112
113/* Convert timeval to time */
114static ossl_unused ossl_inline
115OSSL_TIME ossl_time_from_timeval(struct timeval tv)
116{
117 OSSL_TIME t;
118
119#ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
120 if (tv.tv_sec < 0)
121 return ossl_time_zero();
122#endif
123 t.t = tv.tv_sec * OSSL_TIME_SECOND + tv.tv_usec * OSSL_TIME_US;
124 return t;
125}
126
127/* Convert OSSL_TIME to time_t */
128static ossl_unused ossl_inline
129time_t ossl_time_to_time_t(OSSL_TIME t)
130{
131 return (time_t)(t.t / OSSL_TIME_SECOND);
132}
133
134/* Convert time_t to OSSL_TIME */
135static ossl_unused ossl_inline
136OSSL_TIME ossl_time_from_time_t(time_t t)
137{
138 OSSL_TIME ot;
139
140 ot.t = t;
141 ot.t *= OSSL_TIME_SECOND;
142 return ot;
143}
144
145/* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
146static ossl_unused ossl_inline
147int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
148{
149 if (a.t > b.t)
150 return 1;
151 if (a.t < b.t)
152 return -1;
153 return 0;
154}
155
156/* Returns true if an OSSL_TIME is ossl_time_zero(). */
157static ossl_unused ossl_inline
158int ossl_time_is_zero(OSSL_TIME t)
159{
160 return ossl_time_compare(t, ossl_time_zero()) == 0;
161}
162
163/* Returns true if an OSSL_TIME is ossl_time_infinite(). */
164static ossl_unused ossl_inline
165int ossl_time_is_infinite(OSSL_TIME t)
166{
167 return ossl_time_compare(t, ossl_time_infinite()) == 0;
168}
169
170static ossl_unused ossl_inline
171OSSL_TIME ossl_time_add(OSSL_TIME a, OSSL_TIME b)
172{
173 OSSL_TIME r;
174 int err = 0;
175
176 r.t = safe_add_time(a.t, b.t, &err);
177 return err ? ossl_time_infinite() : r;
178}
179
180static ossl_unused ossl_inline
181OSSL_TIME ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
182{
183 OSSL_TIME r;
184 int err = 0;
185
186 r.t = safe_sub_time(a.t, b.t, &err);
187 return err ? ossl_time_zero() : r;
188}
189
190/* Returns |a - b|. */
191static ossl_unused ossl_inline
192OSSL_TIME ossl_time_abs_difference(OSSL_TIME a, OSSL_TIME b)
193{
194 return a.t > b.t ? ossl_time_subtract(a, b)
195 : ossl_time_subtract(b, a);
196}
197
198static ossl_unused ossl_inline
199OSSL_TIME ossl_time_multiply(OSSL_TIME a, uint64_t b)
200{
201 OSSL_TIME r;
202 int err = 0;
203
204 r.t = safe_mul_time(a.t, b, &err);
205 return err ? ossl_time_infinite() : r;
206}
207
208static ossl_unused ossl_inline
209OSSL_TIME ossl_time_divide(OSSL_TIME a, uint64_t b)
210{
211 OSSL_TIME r;
212 int err = 0;
213
214 r.t = safe_div_time(a.t, b, &err);
215 return err ? ossl_time_zero() : r;
216}
217
218static ossl_unused ossl_inline
219OSSL_TIME ossl_time_muldiv(OSSL_TIME a, uint64_t b, uint64_t c)
220{
221 OSSL_TIME r;
222 int err = 0;
223
224 r.t = safe_muldiv_time(a.t, b, c, &err);
225 return err ? ossl_time_zero() : r;
226}
227
228/* Return higher of the two given time values. */
229static ossl_unused ossl_inline
230OSSL_TIME ossl_time_max(OSSL_TIME a, OSSL_TIME b)
231{
232 return a.t > b.t ? a : b;
233}
234
235/* Return the lower of the two given time values. */
236static ossl_unused ossl_inline
237OSSL_TIME ossl_time_min(OSSL_TIME a, OSSL_TIME b)
238{
239 return a.t < b.t ? a : b;
240}
241
242#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette