summaryrefslogtreecommitdiff
path: root/ccan/ptrint/ptrint.h
blob: 992e4b18bead034afcd25a7a9b617c8f17bc1548 (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
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_PTRINT_H
#define CCAN_PTRINT_H

#include "config.h"

#include <stddef.h>

#include <ccan/build_assert/build_assert.h>

/*
 * This is a deliberately incomplete type, because it should never be
 * dereferenced - instead it marks pointer values which are actually
 * encoding integers
 */
typedef struct ptrint ptrint_t;

static inline ptrdiff_t ptr2int(const ptrint_t *p)
{
	/*
	 * ptrdiff_t is the right size by definition, but to avoid
	 * surprises we want a warning if the user can't fit at least
	 * a regular int in there
	 */
	BUILD_ASSERT(sizeof(int) <= sizeof(ptrdiff_t));
	return (const char *)p - (const char *)NULL;
}

static inline ptrint_t *int2ptr(ptrdiff_t i)
{
	return (ptrint_t *)((char *)NULL + i);
}

#endif /* CCAN_PTRINT_H */