I have unique_ptr<T> which is always aligned to 4096 - means the lowest 12bits of the pointer value are always equal to 0. My intension is to store a byte of payload data / metadata "in within the pointer itself". I mean to create a class which will have a move converting constructor like:
template<typename T>
struct PayloadPtr {
/* ... */
PayloadPTr( unique_ptr<T> ptr ) noexcept { ... }
/* ... */
};
which will 'sink' the unique_ptr<T> and let it store as member but reusing the lowest 12bits to store the byte variable... The overall layout is important as given by the HW.
{
uint32_t payload : 12;
uint32_t pointer : 20;
}
The whole 32bit value can be read as pointer masking out the payload, the payload can be read out masking the highest 20bits... Important stuff is the T's constructor/destructor is not called during the storage within the PayloadPtr, just like regular unique_ptr member variable storage.
How to achieve such behavior? To use union having unique_ptr as one member while having the payload as other? Or to store is as plain data (e.g. uintptr_t) and reinterpret the data in getters/setters?
What would be the recommended way to implement that? Thanks to anyone willing to help!