From fe03918f6d097c862a167ba1ebb69da6bc58fb1a Mon Sep 17 00:00:00 2001 From: Jakob Rath Date: Thu, 27 Jul 2023 15:32:02 +0200 Subject: [PATCH] Add macro version of pointer tagging --- src/util/tptr.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/util/tptr.h b/src/util/tptr.h index 4e1084ce7..99abe34a7 100644 --- a/src/util/tptr.h +++ b/src/util/tptr.h @@ -21,7 +21,7 @@ Revision History: #include #include "util/machine.h" -#include +#include "util/debug.h" #define TAG_SHIFT PTR_ALIGNMENT #define ALIGNMENT_VALUE (1 << PTR_ALIGNMENT) @@ -48,10 +48,17 @@ U unbox(T* ptr) { return static_cast(reinterpret_cast(ptr) >> PTR_ALIGNMENT); } +template +unsigned get_tag(T* ptr) { + return reinterpret_cast(ptr) & TAG_MASK; +} + template -T* box(U val) { +T* box(U val, std::uintptr_t tag = 0) { static_assert( sizeof(T*) >= sizeof(U) + PTR_ALIGNMENT ); - T* ptr = reinterpret_cast(static_cast(val) << PTR_ALIGNMENT); + SASSERT_EQ(tag & PTR_MASK, 0); + T* ptr = reinterpret_cast((static_cast(val) << PTR_ALIGNMENT) | tag); SASSERT_EQ(val, unbox(ptr)); // roundtrip of conversion integer -> pointer -> integer is not actually guaranteed by the C++ standard (but seems fine in practice, as indicated by previous usage of BOXINT/UNBOXINT) + SASSERT_EQ(tag, get_tag(ptr)); return ptr; }