I have a two structures just like below:
template <class T>
struct pointer_t
{
T *ptr;
uintptr_t tag;
};
template <class T>
struct Node
{
T data;
pointer_t<Node<T> > next;
};
and now I want to pass for example pointer_t<Node<T> > newNext
to __sync_bool_compare_and_swap()
function. According to the prototype of the function I pass:
__sync_bool_compare_and_swap((unsigned long*) &newTail.ptr->next, oldNext, newNext)
The problem is that if I don't cast newNext
to unsigned long
I'll get:
error: ‘struct pointer_t<Node<int> >’ used where a ‘long unsigned int’ was expected
if ( __sync_bool_compare_and_swap((unsigned long*) &newTail.ptr->next, newNext, newNext) )
if I cast it to unsigned long then:
if ( __sync_bool_compare_and_swap((unsigned long*) &newTail.ptr->next, (unsigned long) oldNext, (unsigned long) newNext) )
I'll get:
error: ‘struct pointer_t<Node<int> >’ used where a ‘long unsigned int’ was expected.
can someone explain me you can I use __sync_bool_compare_and_swap with these two structures?
thanks
Which doesn't really solve your pointer tagging problem. If you want to achieve pointer tagging then, just steal a few unused bits from the pointer and set those bits to tag the pointer, then unset them to reset the pointer. Don't forget to untag the bits before dereferencing.