cds  2.3.2
cds::details::marked_ptr< T, Bitmask > Class Template Reference

Marked pointer. More...

#include <cds/details/marked_ptr.h>

Public Types

typedef T value_type
 type of value the class points to
 
typedef T * pointer_type
 type of pointer
 

Public Member Functions

constexpr marked_ptr () noexcept
 Constructs null marked pointer. The flag is cleared.
 
constexpr marked_ptr (value_type *ptr) noexcept
 Constructs marked pointer with ptr value. The least bit(s) of ptr is the flag.
 
 marked_ptr (value_type *ptr, int nMask) noexcept
 Constructs marked pointer with ptr value and nMask flag. More...
 
 marked_ptr (marked_ptr const &src) noexcept=default
 Copy constructor.
 
marked_ptroperator= (marked_ptr const &p) noexcept=default
 Copy-assignment operator.
 
value_typeptr () const noexcept
 Returns the pointer without mark bits (real pointer) const version.
 
value_typeall () const noexcept
 Returns the pointer and bits together.
 
uintptr_t bits () const noexcept
 Returns the least bits of pointer according to Bitmask template argument of the class.
 
value_typeoperator-> () const noexcept
 Analogue for ptr.
 
marked_ptr operator= (T *p) noexcept
 Assignment operator sets markup bits to zero.
 
marked_ptroperator|= (int nBits) noexcept
 Set LSB bits as bits() | nBits
 
marked_ptroperator&= (int nBits) noexcept
 Set LSB bits as bits() & nBits
 
marked_ptroperator^= (int nBits) noexcept
 Set LSB bits as bits() ^ nBits
 

Static Public Attributes

static constexpr const uintptr_t bitmask = Bitmask
 bitfield bitmask
 
static constexpr const uintptr_t pointer_bitmask = ~bitmask
 pointer bitmask
 

Private Attributes

T * m_ptr
 pointer and its mark bits
 

Friends

marked_ptr operator| (marked_ptr p, int nBits) noexcept
 Returns p |= nBits
 
marked_ptr operator| (int nBits, marked_ptr p) noexcept
 Returns p |= nBits
 
marked_ptr operator& (marked_ptr p, int nBits) noexcept
 Returns p &= nBits
 
marked_ptr operator& (int nBits, marked_ptr p) noexcept
 Returns p &= nBits
 
marked_ptr operator^ (marked_ptr p, int nBits) noexcept
 Returns p ^= nBits
 
marked_ptr operator^ (int nBits, marked_ptr p) noexcept
 Returns p ^= nBits
 
marked_ptr operator~ (marked_ptr p) noexcept
 Inverts LSBs of pointer p.
 
bool operator== (marked_ptr p1, marked_ptr p2) noexcept
 Comparing two marked pointer including its mark bits.
 
bool operator== (marked_ptr p1, value_type const *p2) noexcept
 Comparing marked pointer and raw pointer, mark bits of p1 is ignored.
 
bool operator== (value_type const *p1, marked_ptr p2) noexcept
 Comparing marked pointer and raw pointer, mark bits of p2 is ignored.
 
bool operator!= (marked_ptr p1, marked_ptr p2) noexcept
 Comparing two marked pointer including its mark bits.
 
bool operator!= (marked_ptr p1, value_type const *p2) noexcept
 Comparing marked pointer and raw pointer, mark bits of p1 is ignored.
 
bool operator!= (value_type const *p1, marked_ptr p2) noexcept
 Comparing marked pointer and raw pointer, mark bits of p2 is ignored.
 

Detailed Description

template<typename T, int Bitmask>
class cds::details::marked_ptr< T, Bitmask >

Marked pointer.

On the modern architectures, the default data alignment is 4 (for 32bit) or 8 byte for 64bit. Therefore, the least 2 or 3 bits of the pointer is always zero and can be used as a bitfield to store logical flags. This trick is widely used in lock-free programming to operate with the pointer and its flags atomically.

Template parameters:

  • T - type of pointer
  • Bitmask - bitmask of least unused bits

Constructor & Destructor Documentation

◆ marked_ptr()

template<typename T , int Bitmask>
cds::details::marked_ptr< T, Bitmask >::marked_ptr ( value_type ptr,
int  nMask 
)
inlinenoexcept

Constructs marked pointer with ptr value and nMask flag.

The nMask argument defines the OR-bits


The documentation for this class was generated from the following file:

cds 2.3.2 Developed by Maxim Khizhinsky aka khizmax and other contributors 2007 - 2017
Autogenerated Sun Dec 31 2017 12:10:35 by Doxygen 1.8.13