Remove ugly reinterpret_cast from HP GC
[libcds.git] / cds / gc / details / retired_ptr.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_GC_DETAILS_RETIRED_PTR_H
4 #define __CDS_GC_DETAILS_RETIRED_PTR_H
5
6 #include <cds/details/defs.h>
7
8 //@cond
9 namespace cds { namespace gc {
10     /// Common implementation details for any GC
11     namespace details {
12
13         /// Pointer to function to free (destruct and deallocate) retired pointer of specific type
14         typedef void (* free_retired_ptr_func )( void * );
15
16         /// Retired pointer
17         /**
18             Pointer to an object that is ready to delete.
19         */
20         struct retired_ptr {
21             /// Pointer type
22             typedef void *          pointer;
23
24             union {
25                 pointer                 m_p;        ///< retired pointer
26                 uintptr_t               m_n;
27             };
28             free_retired_ptr_func   m_funcFree; ///< pointer to the destructor function
29
30             /// Comparison of two retired pointers
31             static bool less( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
32             {
33                 return p1.m_p < p2.m_p;
34             }
35
36             /// Default ctor initializes pointer to \p nullptr
37             retired_ptr() CDS_NOEXCEPT
38                 : m_p( nullptr )
39                 , m_funcFree( nullptr )
40             {}
41
42             /// Ctor
43             retired_ptr( pointer p, free_retired_ptr_func func ) CDS_NOEXCEPT
44                 : m_p( p )
45                 , m_funcFree( func )
46             {}
47
48             /// Typecasting ctor
49             template <typename T>
50             retired_ptr( T * p, void (* pFreeFunc)(T *)) CDS_NOEXCEPT
51                 : m_p( reinterpret_cast<pointer>(p))
52                 , m_funcFree( reinterpret_cast< free_retired_ptr_func >( pFreeFunc ))
53             {}
54
55             /// Assignment operator
56             retired_ptr& operator =( retired_ptr const& s) CDS_NOEXCEPT
57             {
58                 m_p = s.m_p;
59                 m_funcFree = s.m_funcFree;
60                 return *this;
61             }
62
63             /// Invokes destructor function for the pointer
64             void free()
65             {
66                 assert( m_funcFree );
67                 assert( m_p );
68                 m_funcFree( m_p );
69
70                 CDS_STRICT_DO( m_p = nullptr );
71                 CDS_STRICT_DO( m_funcFree = nullptr );
72             }
73         };
74
75         static inline bool operator <( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
76         {
77             return retired_ptr::less( p1, p2 );
78         }
79
80         static inline bool operator ==( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
81         {
82             return p1.m_p == p2.m_p;
83         }
84
85         static inline bool operator !=( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
86         {
87             return !(p1 == p2);
88         }
89     }  // namespace details
90 }}   // namespace cds::gc
91 //@endcond
92
93 #endif // #ifndef __CDS_GC_DETAILS_RETIRED_PTR_H