]> git.ozlabs.org Git - ccan/blob - ccan/ptrint/ptrint.h
992e4b18bead034afcd25a7a9b617c8f17bc1548
[ccan] / ccan / ptrint / ptrint.h
1 /* CC0 (Public domain) - see LICENSE file for details */
2 #ifndef CCAN_PTRINT_H
3 #define CCAN_PTRINT_H
4
5 #include "config.h"
6
7 #include <stddef.h>
8
9 #include <ccan/build_assert/build_assert.h>
10
11 /*
12  * This is a deliberately incomplete type, because it should never be
13  * dereferenced - instead it marks pointer values which are actually
14  * encoding integers
15  */
16 typedef struct ptrint ptrint_t;
17
18 static inline ptrdiff_t ptr2int(const ptrint_t *p)
19 {
20         /*
21          * ptrdiff_t is the right size by definition, but to avoid
22          * surprises we want a warning if the user can't fit at least
23          * a regular int in there
24          */
25         BUILD_ASSERT(sizeof(int) <= sizeof(ptrdiff_t));
26         return (const char *)p - (const char *)NULL;
27 }
28
29 static inline ptrint_t *int2ptr(ptrdiff_t i)
30 {
31         return (ptrint_t *)((char *)NULL + i);
32 }
33
34 #endif /* CCAN_PTRINT_H */