]> git.ozlabs.org Git - ccan/blob - ccan/ptrint/_info
ptrint: Module for encoding integers into void * pointers
[ccan] / ccan / ptrint / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * ptrint - Encoding integers in pointer values
7  *
8  * Library (standard or ccan) functions which take user supplied
9  * callbacks usually have the callback supplied with a void * context
10  * pointer.  For simple cases, it's sometimes sufficient to pass a
11  * simple integer cast into a void *, rather than having to allocate a
12  * context structure.  This module provides some helper macros to do
13  * this relatively safely and portably.
14  *
15  * The key characteristics of these functions are:
16  *      ptr2int(int2ptr(val)) == val
17  * and
18  *      !int2ptr(val) == !val
19  * (i.e. the transformation preserves truth value).
20  *
21  * Example:
22  *      #include <ccan/ptrint/ptrint.h>
23  *
24  *      static void callback(void *opaque)
25  *      {
26  *              int val = ptr2int(opaque);
27  *              printf("Value is %d\n", val);
28  *      }
29  *
30  *      void (*cb)(void *opaque) = callback;
31  *
32  *      int main(int argc, char *argv[])
33  *      {
34  *              int val = 17;
35  *
36  *              (*cb)(int2ptr(val));
37  *              exit(0);
38  *      }
39  *
40  * License: CC0 (Public domain)
41  * Author: David Gibson <david@gibson.dropbear.id.au>
42  */
43 int main(int argc, char *argv[])
44 {
45         /* Expect exactly one argument */
46         if (argc != 2)
47                 return 1;
48
49         if (strcmp(argv[1], "depends") == 0) {
50                 printf("ccan/build_assert\n");
51                 return 0;
52         }
53         if (strcmp(argv[1], "testdepends") == 0) {
54                 printf("ccan/array_size\n");
55                 return 0;
56         }
57
58         return 1;
59 }