timeout_ext 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/COPYING +510 -0
- data/MANIFEST +51 -0
- data/README +15 -0
- data/Rakefile +28 -0
- data/ext/timeout_ext/.gitignore +3 -0
- data/ext/timeout_ext/ccan-bits.c +7 -0
- data/ext/timeout_ext/ccan/array_size/LICENSE +28 -0
- data/ext/timeout_ext/ccan/array_size/_info +46 -0
- data/ext/timeout_ext/ccan/array_size/array_size.h +26 -0
- data/ext/timeout_ext/ccan/build_assert/LICENSE +28 -0
- data/ext/timeout_ext/ccan/build_assert/_info +49 -0
- data/ext/timeout_ext/ccan/build_assert/build_assert.h +40 -0
- data/ext/timeout_ext/ccan/check_type/LICENSE +28 -0
- data/ext/timeout_ext/ccan/check_type/_info +33 -0
- data/ext/timeout_ext/ccan/check_type/check_type.h +64 -0
- data/ext/timeout_ext/ccan/compiler/LICENSE +28 -0
- data/ext/timeout_ext/ccan/compiler/_info +64 -0
- data/ext/timeout_ext/ccan/compiler/compiler.h +231 -0
- data/ext/timeout_ext/ccan/container_of/LICENSE +28 -0
- data/ext/timeout_ext/ccan/container_of/_info +65 -0
- data/ext/timeout_ext/ccan/container_of/container_of.h +145 -0
- data/ext/timeout_ext/ccan/ilog/LICENSE +28 -0
- data/ext/timeout_ext/ccan/ilog/_info +50 -0
- data/ext/timeout_ext/ccan/ilog/ilog.c +141 -0
- data/ext/timeout_ext/ccan/ilog/ilog.h +151 -0
- data/ext/timeout_ext/ccan/list/LICENSE +17 -0
- data/ext/timeout_ext/ccan/list/_info +72 -0
- data/ext/timeout_ext/ccan/list/list.h +842 -0
- data/ext/timeout_ext/ccan/str/LICENSE +28 -0
- data/ext/timeout_ext/ccan/str/_info +52 -0
- data/ext/timeout_ext/ccan/str/str.h +228 -0
- data/ext/timeout_ext/ccan/str/str_debug.h +30 -0
- data/ext/timeout_ext/ccan/time/LICENSE +17 -0
- data/ext/timeout_ext/ccan/time/_info +57 -0
- data/ext/timeout_ext/ccan/time/time.c +138 -0
- data/ext/timeout_ext/ccan/time/time.h +753 -0
- data/ext/timeout_ext/ccan/timer/LICENSE +510 -0
- data/ext/timeout_ext/ccan/timer/_info +79 -0
- data/ext/timeout_ext/ccan/timer/design.txt +76 -0
- data/ext/timeout_ext/ccan/timer/timer.c +524 -0
- data/ext/timeout_ext/ccan/timer/timer.h +211 -0
- data/ext/timeout_ext/depend +17 -0
- data/ext/timeout_ext/extconf.rb +50 -0
- data/ext/timeout_ext/licenses/BSD-MIT +17 -0
- data/ext/timeout_ext/licenses/CC0 +28 -0
- data/ext/timeout_ext/licenses/LGPL-2.1 +510 -0
- data/ext/timeout_ext/missing/stdbool/stdbool.h +20 -0
- data/ext/timeout_ext/timeout_ext.c +114 -0
- data/test/test_timeout_ext.rb +44 -0
- data/timeout_ext.gemspec +30 -0
- metadata +126 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
Statement of Purpose
|
2
|
+
|
3
|
+
The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work").
|
4
|
+
|
5
|
+
Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others.
|
6
|
+
|
7
|
+
For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights.
|
8
|
+
|
9
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following:
|
10
|
+
|
11
|
+
the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work;
|
12
|
+
moral rights retained by the original author(s) and/or performer(s);
|
13
|
+
publicity and privacy rights pertaining to a person's image or likeness depicted in a Work;
|
14
|
+
rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below;
|
15
|
+
rights protecting the extraction, dissemination, use and reuse of data in a Work;
|
16
|
+
database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and
|
17
|
+
other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof.
|
18
|
+
|
19
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose.
|
20
|
+
|
21
|
+
3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose.
|
22
|
+
|
23
|
+
4. Limitations and Disclaimers.
|
24
|
+
|
25
|
+
No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document.
|
26
|
+
Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law.
|
27
|
+
Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work.
|
28
|
+
Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work.
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#include "config.h"
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <string.h>
|
4
|
+
|
5
|
+
/**
|
6
|
+
* str - string helper routines
|
7
|
+
*
|
8
|
+
* This is a grab bag of functions for string operations, designed to enhance
|
9
|
+
* the standard string.h.
|
10
|
+
*
|
11
|
+
* Note that if you define CCAN_STR_DEBUG, you will get extra compile
|
12
|
+
* checks on common misuses of the following functions (they will now
|
13
|
+
* be out-of-line, so there is a runtime penalty!).
|
14
|
+
*
|
15
|
+
* strstr, strchr, strrchr:
|
16
|
+
* Return const char * if first argument is const (gcc only).
|
17
|
+
*
|
18
|
+
* isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph,
|
19
|
+
* islower, isprint, ispunct, isspace, isupper, isxdigit:
|
20
|
+
* Static and runtime check that input is EOF or an *unsigned*
|
21
|
+
* char, as per C standard (really!).
|
22
|
+
*
|
23
|
+
* Example:
|
24
|
+
* #include <stdio.h>
|
25
|
+
* #include <ccan/str/str.h>
|
26
|
+
*
|
27
|
+
* int main(int argc, char *argv[])
|
28
|
+
* {
|
29
|
+
* if (argc > 1 && streq(argv[1], "--verbose"))
|
30
|
+
* printf("verbose set\n");
|
31
|
+
* if (argc > 1 && strstarts(argv[1], "--"))
|
32
|
+
* printf("Some option set\n");
|
33
|
+
* if (argc > 1 && strends(argv[1], "cow-powers"))
|
34
|
+
* printf("Magic option set\n");
|
35
|
+
* return 0;
|
36
|
+
* }
|
37
|
+
*
|
38
|
+
* License: CC0 (Public domain)
|
39
|
+
* Author: Rusty Russell <rusty@rustcorp.com.au>
|
40
|
+
*/
|
41
|
+
int main(int argc, char *argv[])
|
42
|
+
{
|
43
|
+
if (argc != 2)
|
44
|
+
return 1;
|
45
|
+
|
46
|
+
if (strcmp(argv[1], "depends") == 0) {
|
47
|
+
printf("ccan/build_assert\n");
|
48
|
+
return 0;
|
49
|
+
}
|
50
|
+
|
51
|
+
return 1;
|
52
|
+
}
|
@@ -0,0 +1,228 @@
|
|
1
|
+
/* CC0 (Public domain) - see LICENSE file for details */
|
2
|
+
#ifndef CCAN_STR_H
|
3
|
+
#define CCAN_STR_H
|
4
|
+
#include "config.h"
|
5
|
+
#include <string.h>
|
6
|
+
#include <stdbool.h>
|
7
|
+
#include <limits.h>
|
8
|
+
#include <ctype.h>
|
9
|
+
|
10
|
+
/**
|
11
|
+
* streq - Are two strings equal?
|
12
|
+
* @a: first string
|
13
|
+
* @b: first string
|
14
|
+
*
|
15
|
+
* This macro is arguably more readable than "!strcmp(a, b)".
|
16
|
+
*
|
17
|
+
* Example:
|
18
|
+
* if (streq(somestring, ""))
|
19
|
+
* printf("String is empty!\n");
|
20
|
+
*/
|
21
|
+
#define streq(a,b) (strcmp((a),(b)) == 0)
|
22
|
+
|
23
|
+
/**
|
24
|
+
* strstarts - Does this string start with this prefix?
|
25
|
+
* @str: string to test
|
26
|
+
* @prefix: prefix to look for at start of str
|
27
|
+
*
|
28
|
+
* Example:
|
29
|
+
* if (strstarts(somestring, "foo"))
|
30
|
+
* printf("String %s begins with 'foo'!\n", somestring);
|
31
|
+
*/
|
32
|
+
#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
|
33
|
+
|
34
|
+
/**
|
35
|
+
* strends - Does this string end with this postfix?
|
36
|
+
* @str: string to test
|
37
|
+
* @postfix: postfix to look for at end of str
|
38
|
+
*
|
39
|
+
* Example:
|
40
|
+
* if (strends(somestring, "foo"))
|
41
|
+
* printf("String %s end with 'foo'!\n", somestring);
|
42
|
+
*/
|
43
|
+
static inline bool strends(const char *str, const char *postfix)
|
44
|
+
{
|
45
|
+
if (strlen(str) < strlen(postfix))
|
46
|
+
return false;
|
47
|
+
|
48
|
+
return streq(str + strlen(str) - strlen(postfix), postfix);
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* stringify - Turn expression into a string literal
|
53
|
+
* @expr: any C expression
|
54
|
+
*
|
55
|
+
* Example:
|
56
|
+
* #define PRINT_COND_IF_FALSE(cond) \
|
57
|
+
* ((cond) || printf("%s is false!", stringify(cond)))
|
58
|
+
*/
|
59
|
+
#define stringify(expr) stringify_1(expr)
|
60
|
+
/* Double-indirection required to stringify expansions */
|
61
|
+
#define stringify_1(expr) #expr
|
62
|
+
|
63
|
+
/**
|
64
|
+
* strcount - Count number of (non-overlapping) occurrences of a substring.
|
65
|
+
* @haystack: a C string
|
66
|
+
* @needle: a substring
|
67
|
+
*
|
68
|
+
* Example:
|
69
|
+
* assert(strcount("aaa aaa", "a") == 6);
|
70
|
+
* assert(strcount("aaa aaa", "ab") == 0);
|
71
|
+
* assert(strcount("aaa aaa", "aa") == 2);
|
72
|
+
*/
|
73
|
+
size_t strcount(const char *haystack, const char *needle);
|
74
|
+
|
75
|
+
/**
|
76
|
+
* STR_MAX_CHARS - Maximum possible size of numeric string for this type.
|
77
|
+
* @type_or_expr: a pointer or integer type or expression.
|
78
|
+
*
|
79
|
+
* This provides enough space for a nul-terminated string which represents the
|
80
|
+
* largest possible value for the type or expression.
|
81
|
+
*
|
82
|
+
* Note: The implementation adds extra space so hex values or negative
|
83
|
+
* values will fit (eg. sprintf(... "%p"). )
|
84
|
+
*
|
85
|
+
* Example:
|
86
|
+
* char str[STR_MAX_CHARS(int)];
|
87
|
+
*
|
88
|
+
* sprintf(str, "%i", 7);
|
89
|
+
*/
|
90
|
+
#define STR_MAX_CHARS(type_or_expr) \
|
91
|
+
((sizeof(type_or_expr) * CHAR_BIT + 8) / 9 * 3 + 2 \
|
92
|
+
+ STR_MAX_CHARS_TCHECK_(type_or_expr))
|
93
|
+
|
94
|
+
#if HAVE_TYPEOF
|
95
|
+
/* Only a simple type can have 0 assigned, so test that. */
|
96
|
+
#define STR_MAX_CHARS_TCHECK_(type_or_expr) \
|
97
|
+
(sizeof(({ typeof(type_or_expr) x = 0; x; }))*0)
|
98
|
+
#else
|
99
|
+
#define STR_MAX_CHARS_TCHECK_(type_or_expr) 0
|
100
|
+
#endif
|
101
|
+
|
102
|
+
/**
|
103
|
+
* cisalnum - isalnum() which takes a char (and doesn't accept EOF)
|
104
|
+
* @c: a character
|
105
|
+
*
|
106
|
+
* Surprisingly, the standard ctype.h isalnum() takes an int, which
|
107
|
+
* must have the value of EOF (-1) or an unsigned char. This variant
|
108
|
+
* takes a real char, and doesn't accept EOF.
|
109
|
+
*/
|
110
|
+
static inline bool cisalnum(char c)
|
111
|
+
{
|
112
|
+
return isalnum((unsigned char)c);
|
113
|
+
}
|
114
|
+
static inline bool cisalpha(char c)
|
115
|
+
{
|
116
|
+
return isalpha((unsigned char)c);
|
117
|
+
}
|
118
|
+
static inline bool cisascii(char c)
|
119
|
+
{
|
120
|
+
return isascii((unsigned char)c);
|
121
|
+
}
|
122
|
+
#if HAVE_ISBLANK
|
123
|
+
static inline bool cisblank(char c)
|
124
|
+
{
|
125
|
+
return isblank((unsigned char)c);
|
126
|
+
}
|
127
|
+
#endif
|
128
|
+
static inline bool ciscntrl(char c)
|
129
|
+
{
|
130
|
+
return iscntrl((unsigned char)c);
|
131
|
+
}
|
132
|
+
static inline bool cisdigit(char c)
|
133
|
+
{
|
134
|
+
return isdigit((unsigned char)c);
|
135
|
+
}
|
136
|
+
static inline bool cisgraph(char c)
|
137
|
+
{
|
138
|
+
return isgraph((unsigned char)c);
|
139
|
+
}
|
140
|
+
static inline bool cislower(char c)
|
141
|
+
{
|
142
|
+
return islower((unsigned char)c);
|
143
|
+
}
|
144
|
+
static inline bool cisprint(char c)
|
145
|
+
{
|
146
|
+
return isprint((unsigned char)c);
|
147
|
+
}
|
148
|
+
static inline bool cispunct(char c)
|
149
|
+
{
|
150
|
+
return ispunct((unsigned char)c);
|
151
|
+
}
|
152
|
+
static inline bool cisspace(char c)
|
153
|
+
{
|
154
|
+
return isspace((unsigned char)c);
|
155
|
+
}
|
156
|
+
static inline bool cisupper(char c)
|
157
|
+
{
|
158
|
+
return isupper((unsigned char)c);
|
159
|
+
}
|
160
|
+
static inline bool cisxdigit(char c)
|
161
|
+
{
|
162
|
+
return isxdigit((unsigned char)c);
|
163
|
+
}
|
164
|
+
|
165
|
+
#include <ccan/str/str_debug.h>
|
166
|
+
|
167
|
+
/* These checks force things out of line, hence they are under DEBUG. */
|
168
|
+
#ifdef CCAN_STR_DEBUG
|
169
|
+
#include <ccan/build_assert/build_assert.h>
|
170
|
+
|
171
|
+
/* These are commonly misused: they take -1 or an *unsigned* char value. */
|
172
|
+
#undef isalnum
|
173
|
+
#undef isalpha
|
174
|
+
#undef isascii
|
175
|
+
#undef isblank
|
176
|
+
#undef iscntrl
|
177
|
+
#undef isdigit
|
178
|
+
#undef isgraph
|
179
|
+
#undef islower
|
180
|
+
#undef isprint
|
181
|
+
#undef ispunct
|
182
|
+
#undef isspace
|
183
|
+
#undef isupper
|
184
|
+
#undef isxdigit
|
185
|
+
|
186
|
+
/* You can use a char if char is unsigned. */
|
187
|
+
#if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
|
188
|
+
#define str_check_arg_(i) \
|
189
|
+
((i) + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(i), \
|
190
|
+
char) \
|
191
|
+
|| (char)255 > 0))
|
192
|
+
#else
|
193
|
+
#define str_check_arg_(i) (i)
|
194
|
+
#endif
|
195
|
+
|
196
|
+
#define isalnum(i) str_isalnum(str_check_arg_(i))
|
197
|
+
#define isalpha(i) str_isalpha(str_check_arg_(i))
|
198
|
+
#define isascii(i) str_isascii(str_check_arg_(i))
|
199
|
+
#if HAVE_ISBLANK
|
200
|
+
#define isblank(i) str_isblank(str_check_arg_(i))
|
201
|
+
#endif
|
202
|
+
#define iscntrl(i) str_iscntrl(str_check_arg_(i))
|
203
|
+
#define isdigit(i) str_isdigit(str_check_arg_(i))
|
204
|
+
#define isgraph(i) str_isgraph(str_check_arg_(i))
|
205
|
+
#define islower(i) str_islower(str_check_arg_(i))
|
206
|
+
#define isprint(i) str_isprint(str_check_arg_(i))
|
207
|
+
#define ispunct(i) str_ispunct(str_check_arg_(i))
|
208
|
+
#define isspace(i) str_isspace(str_check_arg_(i))
|
209
|
+
#define isupper(i) str_isupper(str_check_arg_(i))
|
210
|
+
#define isxdigit(i) str_isxdigit(str_check_arg_(i))
|
211
|
+
|
212
|
+
#if HAVE_TYPEOF
|
213
|
+
/* With GNU magic, we can make const-respecting standard string functions. */
|
214
|
+
#undef strstr
|
215
|
+
#undef strchr
|
216
|
+
#undef strrchr
|
217
|
+
|
218
|
+
/* + 0 is needed to decay array into pointer. */
|
219
|
+
#define strstr(haystack, needle) \
|
220
|
+
((typeof((haystack) + 0))str_strstr((haystack), (needle)))
|
221
|
+
#define strchr(haystack, c) \
|
222
|
+
((typeof((haystack) + 0))str_strchr((haystack), (c)))
|
223
|
+
#define strrchr(haystack, c) \
|
224
|
+
((typeof((haystack) + 0))str_strrchr((haystack), (c)))
|
225
|
+
#endif
|
226
|
+
#endif /* CCAN_STR_DEBUG */
|
227
|
+
|
228
|
+
#endif /* CCAN_STR_H */
|
@@ -0,0 +1,30 @@
|
|
1
|
+
/* CC0 (Public domain) - see LICENSE file for details */
|
2
|
+
#ifndef CCAN_STR_DEBUG_H
|
3
|
+
#define CCAN_STR_DEBUG_H
|
4
|
+
|
5
|
+
/* #define CCAN_STR_DEBUG 1 */
|
6
|
+
|
7
|
+
#ifdef CCAN_STR_DEBUG
|
8
|
+
/* Because we mug the real ones with macros, we need our own wrappers. */
|
9
|
+
int str_isalnum(int i);
|
10
|
+
int str_isalpha(int i);
|
11
|
+
int str_isascii(int i);
|
12
|
+
#if HAVE_ISBLANK
|
13
|
+
int str_isblank(int i);
|
14
|
+
#endif
|
15
|
+
int str_iscntrl(int i);
|
16
|
+
int str_isdigit(int i);
|
17
|
+
int str_isgraph(int i);
|
18
|
+
int str_islower(int i);
|
19
|
+
int str_isprint(int i);
|
20
|
+
int str_ispunct(int i);
|
21
|
+
int str_isspace(int i);
|
22
|
+
int str_isupper(int i);
|
23
|
+
int str_isxdigit(int i);
|
24
|
+
|
25
|
+
char *str_strstr(const char *haystack, const char *needle);
|
26
|
+
char *str_strchr(const char *s, int c);
|
27
|
+
char *str_strrchr(const char *s, int c);
|
28
|
+
#endif /* CCAN_STR_DEBUG */
|
29
|
+
|
30
|
+
#endif /* CCAN_STR_DEBUG_H */
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
2
|
+
of this software and associated documentation files (the "Software"), to deal
|
3
|
+
in the Software without restriction, including without limitation the rights
|
4
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
5
|
+
copies of the Software, and to permit persons to whom the Software is
|
6
|
+
furnished to do so, subject to the following conditions:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in
|
9
|
+
all copies or substantial portions of the Software.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
17
|
+
THE SOFTWARE.
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#include "config.h"
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <string.h>
|
4
|
+
|
5
|
+
/**
|
6
|
+
* time - routines for dealing with time
|
7
|
+
*
|
8
|
+
* This code provides convenient functions for working with time, in the
|
9
|
+
* form of 'struct timerel' for durations and 'struct timeabs' for timestamps
|
10
|
+
* which are light wrappers around struct timespec.
|
11
|
+
*
|
12
|
+
* Author: Rusty Russell <rusty@rustcorp.com.au>
|
13
|
+
* License: BSD-MIT
|
14
|
+
*
|
15
|
+
* Example:
|
16
|
+
* #include <ccan/time/time.h>
|
17
|
+
* #include <stdlib.h>
|
18
|
+
* #include <stdio.h>
|
19
|
+
* #include <err.h>
|
20
|
+
*
|
21
|
+
* int main(int argc, char *argv[])
|
22
|
+
* {
|
23
|
+
* struct timeabs t;
|
24
|
+
*
|
25
|
+
* if (argc != 2)
|
26
|
+
* errx(1, "Usage: %s <diff in millisec>", argv[0]);
|
27
|
+
*
|
28
|
+
* t = time_now();
|
29
|
+
* if (argv[1][0] == '-')
|
30
|
+
* t = timeabs_sub(t, time_from_msec(atol(argv[1]+1)));
|
31
|
+
* else
|
32
|
+
* t = timeabs_add(t, time_from_msec(atol(argv[1])));
|
33
|
+
*
|
34
|
+
* printf("%lu.%09u\n",
|
35
|
+
* (unsigned long)t.ts.tv_sec, (unsigned)t.ts.tv_nsec);
|
36
|
+
* return 0;
|
37
|
+
* }
|
38
|
+
*/
|
39
|
+
int main(int argc, char *argv[])
|
40
|
+
{
|
41
|
+
/* Expect exactly one argument */
|
42
|
+
if (argc != 2)
|
43
|
+
return 1;
|
44
|
+
|
45
|
+
if (strcmp(argv[1], "depends") == 0) {
|
46
|
+
return 0;
|
47
|
+
}
|
48
|
+
|
49
|
+
#if HAVE_CLOCK_GETTIME_IN_LIBRT
|
50
|
+
if (strcmp(argv[1], "libs") == 0) {
|
51
|
+
printf("rt\n");
|
52
|
+
return 0;
|
53
|
+
}
|
54
|
+
#endif
|
55
|
+
|
56
|
+
return 1;
|
57
|
+
}
|
@@ -0,0 +1,138 @@
|
|
1
|
+
/* Licensed under BSD-MIT - see LICENSE file for details */
|
2
|
+
#include <ccan/time/time.h>
|
3
|
+
#include <stdlib.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
|
6
|
+
#if !HAVE_CLOCK_GETTIME
|
7
|
+
#include <sys/time.h>
|
8
|
+
|
9
|
+
struct timeabs time_now(void)
|
10
|
+
{
|
11
|
+
struct timeval now;
|
12
|
+
struct timeabs ret;
|
13
|
+
gettimeofday(&now, NULL);
|
14
|
+
ret.ts.tv_sec = now.tv_sec;
|
15
|
+
ret.ts.tv_nsec = now.tv_usec * 1000;
|
16
|
+
return TIMEABS_CHECK(ret);
|
17
|
+
}
|
18
|
+
#else
|
19
|
+
#include <time.h>
|
20
|
+
struct timeabs time_now(void)
|
21
|
+
{
|
22
|
+
struct timeabs ret;
|
23
|
+
clock_gettime(CLOCK_REALTIME, &ret.ts);
|
24
|
+
return TIMEABS_CHECK(ret);
|
25
|
+
}
|
26
|
+
#endif /* HAVE_CLOCK_GETTIME */
|
27
|
+
|
28
|
+
struct timemono time_mono(void)
|
29
|
+
{
|
30
|
+
struct timemono ret;
|
31
|
+
#if TIME_HAVE_MONOTONIC
|
32
|
+
clock_gettime(CLOCK_MONOTONIC, &ret.ts);
|
33
|
+
#else /* Best we can do */
|
34
|
+
ret.ts = time_now().ts;
|
35
|
+
#endif /* !HAVE_TIME_MONOTONIC */
|
36
|
+
return TIMEMONO_CHECK(ret);
|
37
|
+
}
|
38
|
+
|
39
|
+
struct timerel time_divide(struct timerel t, unsigned long div)
|
40
|
+
{
|
41
|
+
struct timerel res;
|
42
|
+
uint64_t rem, ns;
|
43
|
+
|
44
|
+
/* Dividing seconds is simple. */
|
45
|
+
res.ts.tv_sec = TIMEREL_CHECK(t).ts.tv_sec / div;
|
46
|
+
rem = t.ts.tv_sec % div;
|
47
|
+
|
48
|
+
/* If we can't fit remainder * 1,000,000,000 in 64 bits? */
|
49
|
+
#if 0 /* ilog is great, but we use fp for multiply anyway. */
|
50
|
+
bits = ilog64(rem);
|
51
|
+
if (bits + 30 >= 64) {
|
52
|
+
/* Reduce accuracy slightly */
|
53
|
+
rem >>= (bits - (64 - 30));
|
54
|
+
div >>= (bits - (64 - 30));
|
55
|
+
}
|
56
|
+
#endif
|
57
|
+
if (rem & ~(((uint64_t)1 << 30) - 1)) {
|
58
|
+
/* FIXME: fp is cheating! */
|
59
|
+
double nsec = rem * 1000000000.0 + t.ts.tv_nsec;
|
60
|
+
res.ts.tv_nsec = nsec / div;
|
61
|
+
} else {
|
62
|
+
ns = rem * 1000000000 + t.ts.tv_nsec;
|
63
|
+
res.ts.tv_nsec = ns / div;
|
64
|
+
}
|
65
|
+
return TIMEREL_CHECK(res);
|
66
|
+
}
|
67
|
+
|
68
|
+
struct timerel time_multiply(struct timerel t, unsigned long mult)
|
69
|
+
{
|
70
|
+
struct timerel res;
|
71
|
+
|
72
|
+
/* Are we going to overflow if we multiply nsec? */
|
73
|
+
if (mult & ~((1UL << 30) - 1)) {
|
74
|
+
/* FIXME: fp is cheating! */
|
75
|
+
double nsec = (double)t.ts.tv_nsec * mult;
|
76
|
+
|
77
|
+
res.ts.tv_sec = nsec / 1000000000.0;
|
78
|
+
res.ts.tv_nsec = nsec - (res.ts.tv_sec * 1000000000.0);
|
79
|
+
} else {
|
80
|
+
uint64_t nsec = t.ts.tv_nsec * mult;
|
81
|
+
|
82
|
+
res.ts.tv_nsec = nsec % 1000000000;
|
83
|
+
res.ts.tv_sec = nsec / 1000000000;
|
84
|
+
}
|
85
|
+
res.ts.tv_sec += TIMEREL_CHECK(t).ts.tv_sec * mult;
|
86
|
+
return TIMEREL_CHECK(res);
|
87
|
+
}
|
88
|
+
|
89
|
+
struct timespec time_check_(struct timespec t, const char *abortstr)
|
90
|
+
{
|
91
|
+
if (t.tv_sec < 0 || t.tv_nsec >= 1000000000) {
|
92
|
+
if (abortstr) {
|
93
|
+
fprintf(stderr, "%s: malformed time %li.%09li\n",
|
94
|
+
abortstr,
|
95
|
+
(long)t.tv_sec, (long)t.tv_nsec);
|
96
|
+
abort();
|
97
|
+
} else {
|
98
|
+
struct timespec old = t;
|
99
|
+
|
100
|
+
if (t.tv_nsec >= 1000000000) {
|
101
|
+
t.tv_sec += t.tv_nsec / 1000000000;
|
102
|
+
t.tv_nsec %= 1000000000;
|
103
|
+
}
|
104
|
+
if (t.tv_sec < 0)
|
105
|
+
t.tv_sec = 0;
|
106
|
+
|
107
|
+
fprintf(stderr, "WARNING: malformed time"
|
108
|
+
" %li seconds %li ns converted to %li.%09li.\n",
|
109
|
+
(long)old.tv_sec, (long)old.tv_nsec,
|
110
|
+
(long)t.tv_sec, (long)t.tv_nsec);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
return t;
|
114
|
+
}
|
115
|
+
|
116
|
+
struct timerel timerel_check(struct timerel t, const char *abortstr)
|
117
|
+
{
|
118
|
+
struct timerel ret;
|
119
|
+
|
120
|
+
ret.ts = time_check_(t.ts, abortstr);
|
121
|
+
return ret;
|
122
|
+
}
|
123
|
+
|
124
|
+
struct timeabs timeabs_check(struct timeabs t, const char *abortstr)
|
125
|
+
{
|
126
|
+
struct timeabs ret;
|
127
|
+
|
128
|
+
ret.ts = time_check_(t.ts, abortstr);
|
129
|
+
return ret;
|
130
|
+
}
|
131
|
+
|
132
|
+
struct timemono timemono_check(struct timemono t, const char *abortstr)
|
133
|
+
{
|
134
|
+
struct timemono ret;
|
135
|
+
|
136
|
+
ret.ts = time_check_(t.ts, abortstr);
|
137
|
+
return ret;
|
138
|
+
}
|