]> git.ozlabs.org Git - ccan/commitdiff
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)
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>

No differences found