6 * ptrint - Encoding integers in pointer values
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.
15 * The key characteristics of these functions are:
16 * ptr2int(int2ptr(val)) == val
18 * !int2ptr(val) == !val
19 * (i.e. the transformation preserves truth value).
22 * #include <ccan/ptrint/ptrint.h>
24 * static void callback(void *opaque)
26 * int val = ptr2int(opaque);
27 * printf("Value is %d\n", val);
30 * void (*cb)(void *opaque) = callback;
32 * int main(int argc, char *argv[])
36 * (*cb)(int2ptr(val));
40 * License: CC0 (Public domain)
41 * Author: David Gibson <david@gibson.dropbear.id.au>
43 int main(int argc, char *argv[])
45 /* Expect exactly one argument */
49 if (strcmp(argv[1], "depends") == 0) {
50 printf("ccan/build_assert\n");
53 if (strcmp(argv[1], "testdepends") == 0) {
54 printf("ccan/array_size\n");