ptrint: Module for encoding integers into void * pointers
For callbacks which need a void * context pointer in the general case,
there are often simpler cases where an integer would suffice. These are
often handled by casting the integer into a pointer, rather than having
to allocate that integer somewhere.
This adds a module with some helpers for this. It has some advantages over
direct casts:
* It uses pointer arithmetic against NULL instead of casts which should
make it more portable, even to weird platforms with odd representations
for NULL. I don't know the C standard well enough to know if it's
totally portable though.
* In particular it means that the truth value of the pointer
representation matches that of the encoded integer.
* The conversion functions are inlines providing more type safety than
raw casts.
* It uses a ptrint_t * type which can be used to mark such pointer
encoded integers. ptrint_t is a deliberately incomplete type so such
pointers can never be dereferenced.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Showing
ccan/ptrint/LICENSE
0 → 120000
ccan/ptrint/_info
0 → 100644
ccan/ptrint/ptrint.h
0 → 100644
ccan/ptrint/test/run.c
0 → 100644
Please register or sign in to comment