timeout_ext 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/COPYING +510 -0
  4. data/MANIFEST +51 -0
  5. data/README +15 -0
  6. data/Rakefile +28 -0
  7. data/ext/timeout_ext/.gitignore +3 -0
  8. data/ext/timeout_ext/ccan-bits.c +7 -0
  9. data/ext/timeout_ext/ccan/array_size/LICENSE +28 -0
  10. data/ext/timeout_ext/ccan/array_size/_info +46 -0
  11. data/ext/timeout_ext/ccan/array_size/array_size.h +26 -0
  12. data/ext/timeout_ext/ccan/build_assert/LICENSE +28 -0
  13. data/ext/timeout_ext/ccan/build_assert/_info +49 -0
  14. data/ext/timeout_ext/ccan/build_assert/build_assert.h +40 -0
  15. data/ext/timeout_ext/ccan/check_type/LICENSE +28 -0
  16. data/ext/timeout_ext/ccan/check_type/_info +33 -0
  17. data/ext/timeout_ext/ccan/check_type/check_type.h +64 -0
  18. data/ext/timeout_ext/ccan/compiler/LICENSE +28 -0
  19. data/ext/timeout_ext/ccan/compiler/_info +64 -0
  20. data/ext/timeout_ext/ccan/compiler/compiler.h +231 -0
  21. data/ext/timeout_ext/ccan/container_of/LICENSE +28 -0
  22. data/ext/timeout_ext/ccan/container_of/_info +65 -0
  23. data/ext/timeout_ext/ccan/container_of/container_of.h +145 -0
  24. data/ext/timeout_ext/ccan/ilog/LICENSE +28 -0
  25. data/ext/timeout_ext/ccan/ilog/_info +50 -0
  26. data/ext/timeout_ext/ccan/ilog/ilog.c +141 -0
  27. data/ext/timeout_ext/ccan/ilog/ilog.h +151 -0
  28. data/ext/timeout_ext/ccan/list/LICENSE +17 -0
  29. data/ext/timeout_ext/ccan/list/_info +72 -0
  30. data/ext/timeout_ext/ccan/list/list.h +842 -0
  31. data/ext/timeout_ext/ccan/str/LICENSE +28 -0
  32. data/ext/timeout_ext/ccan/str/_info +52 -0
  33. data/ext/timeout_ext/ccan/str/str.h +228 -0
  34. data/ext/timeout_ext/ccan/str/str_debug.h +30 -0
  35. data/ext/timeout_ext/ccan/time/LICENSE +17 -0
  36. data/ext/timeout_ext/ccan/time/_info +57 -0
  37. data/ext/timeout_ext/ccan/time/time.c +138 -0
  38. data/ext/timeout_ext/ccan/time/time.h +753 -0
  39. data/ext/timeout_ext/ccan/timer/LICENSE +510 -0
  40. data/ext/timeout_ext/ccan/timer/_info +79 -0
  41. data/ext/timeout_ext/ccan/timer/design.txt +76 -0
  42. data/ext/timeout_ext/ccan/timer/timer.c +524 -0
  43. data/ext/timeout_ext/ccan/timer/timer.h +211 -0
  44. data/ext/timeout_ext/depend +17 -0
  45. data/ext/timeout_ext/extconf.rb +50 -0
  46. data/ext/timeout_ext/licenses/BSD-MIT +17 -0
  47. data/ext/timeout_ext/licenses/CC0 +28 -0
  48. data/ext/timeout_ext/licenses/LGPL-2.1 +510 -0
  49. data/ext/timeout_ext/missing/stdbool/stdbool.h +20 -0
  50. data/ext/timeout_ext/timeout_ext.c +114 -0
  51. data/test/test_timeout_ext.rb +44 -0
  52. data/timeout_ext.gemspec +30 -0
  53. 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,64 @@
1
+ #include "config.h"
2
+ #include <string.h>
3
+ #include <stdio.h>
4
+
5
+ /**
6
+ * compiler - macros for common compiler extensions
7
+ *
8
+ * Abstracts away some compiler hints. Currently these include:
9
+ * - COLD
10
+ * For functions not called in fast paths (aka. cold functions)
11
+ * - PRINTF_FMT
12
+ * For functions which take printf-style parameters.
13
+ * - CONST_FUNCTION
14
+ * For functions which return the same value for same parameters.
15
+ * - NEEDED
16
+ * For functions and variables which must be emitted even if unused.
17
+ * - UNNEEDED
18
+ * For functions and variables which need not be emitted if unused.
19
+ * - UNUSED
20
+ * For parameters which are not used.
21
+ * - IS_COMPILE_CONSTANT()
22
+ * For using different tradeoffs for compiletime vs runtime evaluation.
23
+ *
24
+ * License: CC0 (Public domain)
25
+ * Author: Rusty Russell <rusty@rustcorp.com.au>
26
+ *
27
+ * Example:
28
+ * #include <ccan/compiler/compiler.h>
29
+ * #include <stdio.h>
30
+ * #include <stdarg.h>
31
+ *
32
+ * // Example of a (slow-path) logging function.
33
+ * static int log_threshold = 2;
34
+ * static void COLD PRINTF_FMT(2,3)
35
+ * logger(int level, const char *fmt, ...)
36
+ * {
37
+ * va_list ap;
38
+ * va_start(ap, fmt);
39
+ * if (level >= log_threshold)
40
+ * vfprintf(stderr, fmt, ap);
41
+ * va_end(ap);
42
+ * }
43
+ *
44
+ * int main(int argc, char *argv[] UNNEEDED)
45
+ * {
46
+ * if (argc != 1) {
47
+ * logger(3, "Don't want %i arguments!\n", argc-1);
48
+ * return 1;
49
+ * }
50
+ * return 0;
51
+ * }
52
+ */
53
+ int main(int argc, char *argv[])
54
+ {
55
+ /* Expect exactly one argument */
56
+ if (argc != 2)
57
+ return 1;
58
+
59
+ if (strcmp(argv[1], "depends") == 0) {
60
+ return 0;
61
+ }
62
+
63
+ return 1;
64
+ }
@@ -0,0 +1,231 @@
1
+ /* CC0 (Public domain) - see LICENSE file for details */
2
+ #ifndef CCAN_COMPILER_H
3
+ #define CCAN_COMPILER_H
4
+ #include "config.h"
5
+
6
+ #ifndef COLD
7
+ #if HAVE_ATTRIBUTE_COLD
8
+ /**
9
+ * COLD - a function is unlikely to be called.
10
+ *
11
+ * Used to mark an unlikely code path and optimize appropriately.
12
+ * It is usually used on logging or error routines.
13
+ *
14
+ * Example:
15
+ * static void COLD moan(const char *reason)
16
+ * {
17
+ * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
18
+ * }
19
+ */
20
+ #define COLD __attribute__((__cold__))
21
+ #else
22
+ #define COLD
23
+ #endif
24
+ #endif
25
+
26
+ #ifndef NORETURN
27
+ #if HAVE_ATTRIBUTE_NORETURN
28
+ /**
29
+ * NORETURN - a function does not return
30
+ *
31
+ * Used to mark a function which exits; useful for suppressing warnings.
32
+ *
33
+ * Example:
34
+ * static void NORETURN fail(const char *reason)
35
+ * {
36
+ * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
37
+ * exit(1);
38
+ * }
39
+ */
40
+ #define NORETURN __attribute__((__noreturn__))
41
+ #else
42
+ #define NORETURN
43
+ #endif
44
+ #endif
45
+
46
+ #ifndef PRINTF_FMT
47
+ #if HAVE_ATTRIBUTE_PRINTF
48
+ /**
49
+ * PRINTF_FMT - a function takes printf-style arguments
50
+ * @nfmt: the 1-based number of the function's format argument.
51
+ * @narg: the 1-based number of the function's first variable argument.
52
+ *
53
+ * This allows the compiler to check your parameters as it does for printf().
54
+ *
55
+ * Example:
56
+ * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
57
+ */
58
+ #define PRINTF_FMT(nfmt, narg) \
59
+ __attribute__((format(__printf__, nfmt, narg)))
60
+ #else
61
+ #define PRINTF_FMT(nfmt, narg)
62
+ #endif
63
+ #endif
64
+
65
+ #ifndef CONST_FUNCTION
66
+ #if HAVE_ATTRIBUTE_CONST
67
+ /**
68
+ * CONST_FUNCTION - a function's return depends only on its argument
69
+ *
70
+ * This allows the compiler to assume that the function will return the exact
71
+ * same value for the exact same arguments. This implies that the function
72
+ * must not use global variables, or dereference pointer arguments.
73
+ */
74
+ #define CONST_FUNCTION __attribute__((__const__))
75
+ #else
76
+ #define CONST_FUNCTION
77
+ #endif
78
+
79
+ #ifndef PURE_FUNCTION
80
+ #if HAVE_ATTRIBUTE_PURE
81
+ /**
82
+ * PURE_FUNCTION - a function is pure
83
+ *
84
+ * A pure function is one that has no side effects other than it's return value
85
+ * and uses no inputs other than it's arguments and global variables.
86
+ */
87
+ #define PURE_FUNCTION __attribute__((__pure__))
88
+ #else
89
+ #define PURE_FUNCTION
90
+ #endif
91
+ #endif
92
+ #endif
93
+
94
+ #if HAVE_ATTRIBUTE_UNUSED
95
+ #ifndef UNNEEDED
96
+ /**
97
+ * UNNEEDED - a variable/function may not be needed
98
+ *
99
+ * This suppresses warnings about unused variables or functions, but tells
100
+ * the compiler that if it is unused it need not emit it into the source code.
101
+ *
102
+ * Example:
103
+ * // With some preprocessor options, this is unnecessary.
104
+ * static UNNEEDED int counter;
105
+ *
106
+ * // With some preprocessor options, this is unnecessary.
107
+ * static UNNEEDED void add_to_counter(int add)
108
+ * {
109
+ * counter += add;
110
+ * }
111
+ */
112
+ #define UNNEEDED __attribute__((__unused__))
113
+ #endif
114
+
115
+ #ifndef NEEDED
116
+ #if HAVE_ATTRIBUTE_USED
117
+ /**
118
+ * NEEDED - a variable/function is needed
119
+ *
120
+ * This suppresses warnings about unused variables or functions, but tells
121
+ * the compiler that it must exist even if it (seems) unused.
122
+ *
123
+ * Example:
124
+ * // Even if this is unused, these are vital for debugging.
125
+ * static NEEDED int counter;
126
+ * static NEEDED void dump_counter(void)
127
+ * {
128
+ * printf("Counter is %i\n", counter);
129
+ * }
130
+ */
131
+ #define NEEDED __attribute__((__used__))
132
+ #else
133
+ /* Before used, unused functions and vars were always emitted. */
134
+ #define NEEDED __attribute__((__unused__))
135
+ #endif
136
+ #endif
137
+
138
+ #ifndef UNUSED
139
+ /**
140
+ * UNUSED - a parameter is unused
141
+ *
142
+ * Some compilers (eg. gcc with -W or -Wunused) warn about unused
143
+ * function parameters. This suppresses such warnings and indicates
144
+ * to the reader that it's deliberate.
145
+ *
146
+ * Example:
147
+ * // This is used as a callback, so needs to have this prototype.
148
+ * static int some_callback(void *unused UNUSED)
149
+ * {
150
+ * return 0;
151
+ * }
152
+ */
153
+ #define UNUSED __attribute__((__unused__))
154
+ #endif
155
+ #else
156
+ #ifndef UNNEEDED
157
+ #define UNNEEDED
158
+ #endif
159
+ #ifndef NEEDED
160
+ #define NEEDED
161
+ #endif
162
+ #ifndef UNUSED
163
+ #define UNUSED
164
+ #endif
165
+ #endif
166
+
167
+ #ifndef IS_COMPILE_CONSTANT
168
+ #if HAVE_BUILTIN_CONSTANT_P
169
+ /**
170
+ * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
171
+ * @expr: the expression to evaluate
172
+ *
173
+ * When an expression manipulation is complicated, it is usually better to
174
+ * implement it in a function. However, if the expression being manipulated is
175
+ * known at compile time, it is better to have the compiler see the entire
176
+ * expression so it can simply substitute the result.
177
+ *
178
+ * This can be done using the IS_COMPILE_CONSTANT() macro.
179
+ *
180
+ * Example:
181
+ * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
182
+ *
183
+ * // Out-of-line version.
184
+ * const char *greek_name(enum greek greek);
185
+ *
186
+ * // Inline version.
187
+ * static inline const char *_greek_name(enum greek greek)
188
+ * {
189
+ * switch (greek) {
190
+ * case ALPHA: return "alpha";
191
+ * case BETA: return "beta";
192
+ * case GAMMA: return "gamma";
193
+ * case DELTA: return "delta";
194
+ * case EPSILON: return "epsilon";
195
+ * default: return "**INVALID**";
196
+ * }
197
+ * }
198
+ *
199
+ * // Use inline if compiler knows answer. Otherwise call function
200
+ * // to avoid copies of the same code everywhere.
201
+ * #define greek_name(g) \
202
+ * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
203
+ */
204
+ #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
205
+ #else
206
+ /* If we don't know, assume it's not. */
207
+ #define IS_COMPILE_CONSTANT(expr) 0
208
+ #endif
209
+ #endif
210
+
211
+ #ifndef WARN_UNUSED_RESULT
212
+ #if HAVE_WARN_UNUSED_RESULT
213
+ /**
214
+ * WARN_UNUSED_RESULT - warn if a function return value is unused.
215
+ *
216
+ * Used to mark a function where it is extremely unlikely that the caller
217
+ * can ignore the result, eg realloc().
218
+ *
219
+ * Example:
220
+ * // buf param may be freed by this; need return value!
221
+ * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
222
+ * {
223
+ * return realloc(buf, (*size) *= 2);
224
+ * }
225
+ */
226
+ #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
227
+ #else
228
+ #define WARN_UNUSED_RESULT
229
+ #endif
230
+ #endif
231
+ #endif /* CCAN_COMPILER_H */
@@ -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,65 @@
1
+ #include "config.h"
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+
5
+ /**
6
+ * container_of - routine for upcasting
7
+ *
8
+ * It is often convenient to create code where the caller registers a pointer
9
+ * to a generic structure and a callback. The callback might know that the
10
+ * pointer points to within a larger structure, and container_of gives a
11
+ * convenient and fairly type-safe way of returning to the enclosing structure.
12
+ *
13
+ * This idiom is an alternative to providing a void * pointer for every
14
+ * callback.
15
+ *
16
+ * Example:
17
+ * #include <stdio.h>
18
+ * #include <ccan/container_of/container_of.h>
19
+ *
20
+ * struct timer {
21
+ * void *members;
22
+ * };
23
+ *
24
+ * struct info {
25
+ * int my_stuff;
26
+ * struct timer timer;
27
+ * };
28
+ *
29
+ * static void my_timer_callback(struct timer *timer)
30
+ * {
31
+ * struct info *info = container_of(timer, struct info, timer);
32
+ * printf("my_stuff is %u\n", info->my_stuff);
33
+ * }
34
+ *
35
+ * static void register_timer(struct timer *timer)
36
+ * {
37
+ * (void)timer;
38
+ * (void)my_timer_callback;
39
+ * //...
40
+ * }
41
+ *
42
+ * int main(void)
43
+ * {
44
+ * struct info info = { .my_stuff = 1 };
45
+ *
46
+ * register_timer(&info.timer);
47
+ * // ...
48
+ * return 0;
49
+ * }
50
+ *
51
+ * License: CC0 (Public domain)
52
+ * Author: Rusty Russell <rusty@rustcorp.com.au>
53
+ */
54
+ int main(int argc, char *argv[])
55
+ {
56
+ if (argc != 2)
57
+ return 1;
58
+
59
+ if (strcmp(argv[1], "depends") == 0) {
60
+ printf("ccan/check_type\n");
61
+ return 0;
62
+ }
63
+
64
+ return 1;
65
+ }
@@ -0,0 +1,145 @@
1
+ /* CC0 (Public domain) - see LICENSE file for details */
2
+ #ifndef CCAN_CONTAINER_OF_H
3
+ #define CCAN_CONTAINER_OF_H
4
+ #include <stddef.h>
5
+
6
+ #include "config.h"
7
+ #include <ccan/check_type/check_type.h>
8
+
9
+ /**
10
+ * container_of - get pointer to enclosing structure
11
+ * @member_ptr: pointer to the structure member
12
+ * @containing_type: the type this member is within
13
+ * @member: the name of this member within the structure.
14
+ *
15
+ * Given a pointer to a member of a structure, this macro does pointer
16
+ * subtraction to return the pointer to the enclosing type.
17
+ *
18
+ * Example:
19
+ * struct foo {
20
+ * int fielda, fieldb;
21
+ * // ...
22
+ * };
23
+ * struct info {
24
+ * int some_other_field;
25
+ * struct foo my_foo;
26
+ * };
27
+ *
28
+ * static struct info *foo_to_info(struct foo *foo)
29
+ * {
30
+ * return container_of(foo, struct info, my_foo);
31
+ * }
32
+ */
33
+ #define container_of(member_ptr, containing_type, member) \
34
+ ((containing_type *) \
35
+ ((char *)(member_ptr) \
36
+ - container_off(containing_type, member)) \
37
+ + check_types_match(*(member_ptr), ((containing_type *)0)->member))
38
+
39
+
40
+ /**
41
+ * container_of_or_null - get pointer to enclosing structure, or NULL
42
+ * @member_ptr: pointer to the structure member
43
+ * @containing_type: the type this member is within
44
+ * @member: the name of this member within the structure.
45
+ *
46
+ * Given a pointer to a member of a structure, this macro does pointer
47
+ * subtraction to return the pointer to the enclosing type, unless it
48
+ * is given NULL, in which case it also returns NULL.
49
+ *
50
+ * Example:
51
+ * struct foo {
52
+ * int fielda, fieldb;
53
+ * // ...
54
+ * };
55
+ * struct info {
56
+ * int some_other_field;
57
+ * struct foo my_foo;
58
+ * };
59
+ *
60
+ * static struct info *foo_to_info_allowing_null(struct foo *foo)
61
+ * {
62
+ * return container_of_or_null(foo, struct info, my_foo);
63
+ * }
64
+ */
65
+ static inline char *container_of_or_null_(void *member_ptr, size_t offset)
66
+ {
67
+ return member_ptr ? (char *)member_ptr - offset : NULL;
68
+ }
69
+ #define container_of_or_null(member_ptr, containing_type, member) \
70
+ ((containing_type *) \
71
+ container_of_or_null_(member_ptr, \
72
+ container_off(containing_type, member)) \
73
+ + check_types_match(*(member_ptr), ((containing_type *)0)->member))
74
+
75
+ /**
76
+ * container_off - get offset to enclosing structure
77
+ * @containing_type: the type this member is within
78
+ * @member: the name of this member within the structure.
79
+ *
80
+ * Given a pointer to a member of a structure, this macro does
81
+ * typechecking and figures out the offset to the enclosing type.
82
+ *
83
+ * Example:
84
+ * struct foo {
85
+ * int fielda, fieldb;
86
+ * // ...
87
+ * };
88
+ * struct info {
89
+ * int some_other_field;
90
+ * struct foo my_foo;
91
+ * };
92
+ *
93
+ * static struct info *foo_to_info(struct foo *foo)
94
+ * {
95
+ * size_t off = container_off(struct info, my_foo);
96
+ * return (void *)((char *)foo - off);
97
+ * }
98
+ */
99
+ #define container_off(containing_type, member) \
100
+ offsetof(containing_type, member)
101
+
102
+ /**
103
+ * container_of_var - get pointer to enclosing structure using a variable
104
+ * @member_ptr: pointer to the structure member
105
+ * @container_var: a pointer of same type as this member's container
106
+ * @member: the name of this member within the structure.
107
+ *
108
+ * Given a pointer to a member of a structure, this macro does pointer
109
+ * subtraction to return the pointer to the enclosing type.
110
+ *
111
+ * Example:
112
+ * static struct info *foo_to_i(struct foo *foo)
113
+ * {
114
+ * struct info *i = container_of_var(foo, i, my_foo);
115
+ * return i;
116
+ * }
117
+ */
118
+ #if HAVE_TYPEOF
119
+ #define container_of_var(member_ptr, container_var, member) \
120
+ container_of(member_ptr, typeof(*container_var), member)
121
+ #else
122
+ #define container_of_var(member_ptr, container_var, member) \
123
+ ((void *)((char *)(member_ptr) - \
124
+ container_off_var(container_var, member)))
125
+ #endif
126
+
127
+ /**
128
+ * container_off_var - get offset of a field in enclosing structure
129
+ * @container_var: a pointer to a container structure
130
+ * @member: the name of a member within the structure.
131
+ *
132
+ * Given (any) pointer to a structure and a its member name, this
133
+ * macro does pointer subtraction to return offset of member in a
134
+ * structure memory layout.
135
+ *
136
+ */
137
+ #if HAVE_TYPEOF
138
+ #define container_off_var(var, member) \
139
+ container_off(typeof(*var), member)
140
+ #else
141
+ #define container_off_var(var, member) \
142
+ ((const char *)&(var)->member - (const char *)(var))
143
+ #endif
144
+
145
+ #endif /* CCAN_CONTAINER_OF_H */