iso14651_t1_common: <U\([0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]\)> → <U000\1>
[glibc.git] / inet / deadline.c
blobc54005c3c1783753c3380b835e15c8e6b99dc6ba
1 /* Computing deadlines for timeouts.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <net-internal.h>
21 #include <assert.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <time.h>
27 struct deadline_current_time
28 __deadline_current_time (void)
30 struct deadline_current_time result;
31 if (__clock_gettime (CLOCK_MONOTONIC, &result.current) != 0)
33 struct timeval current_tv;
34 if (__gettimeofday (&current_tv, NULL) == 0)
35 __libc_fatal ("Fatal error: gettimeofday system call failed\n");
36 result.current.tv_sec = current_tv.tv_sec;
37 result.current.tv_nsec = current_tv.tv_usec * 1000;
39 assert (result.current.tv_sec >= 0);
40 return result;
43 /* A special deadline value for which __deadline_is_infinite is
44 true. */
45 static inline struct deadline
46 infinite_deadline (void)
48 return (struct deadline) { { -1, -1 } };
51 struct deadline
52 __deadline_from_timeval (struct deadline_current_time current,
53 struct timeval tv)
55 assert (__is_timeval_valid_timeout (tv));
57 /* Compute second-based deadline. Perform the addition in
58 uintmax_t, which is unsigned, to simply overflow detection. */
59 uintmax_t sec = current.current.tv_sec;
60 sec += tv.tv_sec;
61 if (sec < (uintmax_t) tv.tv_sec)
62 return infinite_deadline ();
64 /* Compute nanosecond deadline. */
65 int nsec = current.current.tv_nsec + tv.tv_usec * 1000;
66 if (nsec >= 1000 * 1000 * 1000)
68 /* Carry nanosecond overflow to seconds. */
69 nsec -= 1000 * 1000 * 1000;
70 if (sec + 1 < sec)
71 return infinite_deadline ();
72 ++sec;
74 /* This uses a GCC extension, otherwise these casts for detecting
75 overflow would not be defined. */
76 if ((time_t) sec < 0 || sec != (uintmax_t) (time_t) sec)
77 return infinite_deadline ();
79 return (struct deadline) { { sec, nsec } };
82 int
83 __deadline_to_ms (struct deadline_current_time current,
84 struct deadline deadline)
86 if (__deadline_is_infinite (deadline))
87 return INT_MAX;
89 if (current.current.tv_sec > deadline.absolute.tv_sec
90 || (current.current.tv_sec == deadline.absolute.tv_sec
91 && current.current.tv_nsec >= deadline.absolute.tv_nsec))
92 return 0;
93 time_t sec = deadline.absolute.tv_sec - current.current.tv_sec;
94 if (sec >= INT_MAX)
95 /* This value will overflow below. */
96 return INT_MAX;
97 int nsec = deadline.absolute.tv_nsec - current.current.tv_nsec;
98 if (nsec < 0)
100 /* Borrow from the seconds field. */
101 assert (sec > 0);
102 --sec;
103 nsec += 1000 * 1000 * 1000;
106 /* Prepare for rounding up to milliseconds. */
107 nsec += 999999;
108 if (nsec > 1000 * 1000 * 1000)
110 assert (sec < INT_MAX);
111 ++sec;
112 nsec -= 1000 * 1000 * 1000;
115 unsigned int msec = nsec / (1000 * 1000);
116 if (sec > INT_MAX / 1000)
117 return INT_MAX;
118 msec += sec * 1000;
119 if (msec > INT_MAX)
120 return INT_MAX;
121 return msec;