ptrint.h 816 B

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