sys/util.h: helper macro to perform pointer difference

Some code casts pointers to ints in order to obtain their difference.
The compiler complains on 64-bit targets as an int is not wide enough
to hold a pointer.

Let's introduce the PTR_DIFF() helper macro to applies the proper cast
to pointers before performing a difference on them, and still return the
result as an int which should be large enough in practice.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2019-07-09 17:08:21 -04:00 committed by Andrew Boie
commit 755cc644cc

View file

@ -25,6 +25,9 @@
#define POINTER_TO_INT(x) ((intptr_t) (x))
#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
/* Helper to get the difference between two pointers as an int */
#define PTR_DIFF(y, x) ((int)((intptr_t)(y) - (intptr_t)(x)))
#if !(defined (__CHAR_BIT__) && defined (__SIZEOF_LONG__))
# error Missing required predefined macros for BITS_PER_LONG calculation
#endif