]> git.ozlabs.org Git - ccan/commit
ptrint: Module for encoding integers into void * pointers
authorDavid Gibson <david@gibson.dropbear.id.au>
Tue, 26 May 2015 12:56:50 +0000 (22:56 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Wed, 27 May 2015 10:06:04 +0000 (20:06 +1000)
commit53079b9c300f1bdf1cd5695a13f3ccbe2de86ee9
treebdc5a4c11d971c658ba8baf04b61bec44eb18a8b
parent9d4ae5f1313a25c99cd5b909ee1038e4424b46cb
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>
ccan/ptrint/LICENSE [new symlink]
ccan/ptrint/_info [new file with mode: 0644]
ccan/ptrint/ptrint.h [new file with mode: 0644]
ccan/ptrint/test/run.c [new file with mode: 0644]