From 755cc644cc0b47dfa335219cc305a04a14e4ec25 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Tue, 9 Jul 2019 17:08:21 -0400 Subject: [PATCH] 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 --- include/sys/util.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/sys/util.h b/include/sys/util.h index 7121da23b98..6a9718332b9 100644 --- a/include/sys/util.h +++ b/include/sys/util.h @@ -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