]> git.ozlabs.org Git - ccan/blob - ccan/container_of/_info
ccanlint: timeout, and implement -t option for quicker tests.
[ccan] / ccan / container_of / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.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  *      int main()
36  *      {
37  *              struct info info = { .my_stuff = 1 };
38  *
39  *              register_timer(&info.timer);
40  *              // ...
41  *              return 0;
42  *      }
43  *
44  * Licence: LGPL (2 or any later version)
45  */
46 int main(int argc, char *argv[])
47 {
48         if (argc != 2)
49                 return 1;
50
51         if (strcmp(argv[1], "depends") == 0) {
52                 printf("ccan/check_type\n");
53                 return 0;
54         }
55
56         return 1;
57 }