summaryrefslogtreecommitdiff
path: root/ccan/ptrint/_info
blob: 8135d1ee8ee4a90cbb94189f6f0f2fafc2dd9bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
 * ptrint - Encoding integers in pointer values
 *
 * Library (standard or ccan) functions which take user supplied
 * callbacks usually have the callback supplied with a void * context
 * pointer.  For simple cases, it's sometimes sufficient to pass a
 * simple integer cast into a void *, rather than having to allocate a
 * context structure.  This module provides some helper macros to do
 * this relatively safely and portably.
 *
 * The key characteristics of these functions are:
 *	ptr2int(int2ptr(val)) == val
 * and
 *      !int2ptr(val) == !val
 * (i.e. the transformation preserves truth value).
 *
 * Example:
 *	#include <ccan/ptrint/ptrint.h>
 *
 *	static void callback(void *opaque)
 *	{
 *		int val = ptr2int(opaque);
 *		printf("Value is %d\n", val);
 *	}
 *
 *	void (*cb)(void *opaque) = callback;
 *
 *	int main(int argc, char *argv[])
 *	{
 *		int val = 17;
 *
 *		(*cb)(int2ptr(val));
 *		exit(0);
 *	}
 *
 * License: CC0 (Public domain)
 * Author: David Gibson <david@gibson.dropbear.id.au>
 */
int main(int argc, char *argv[])
{
	/* Expect exactly one argument */
	if (argc != 2)
		return 1;

	if (strcmp(argv[1], "depends") == 0) {
		printf("ccan/build_assert\n");
		return 0;
	}
	if (strcmp(argv[1], "testdepends") == 0) {
		printf("ccan/array_size\n");
		return 0;
	}

	return 1;
}