Fixed UBsan warning "call to function through pointer to incorrect function type"
[libcds.git] / cds / gc / details / retired_ptr.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_GC_DETAILS_RETIRED_PTR_H
32 #define CDSLIB_GC_DETAILS_RETIRED_PTR_H
33
34 #include <cds/details/defs.h>
35 #include <cds/details/static_functor.h>
36
37 //@cond
38 namespace cds { namespace gc {
39     /// Common implementation details for any GC
40     namespace details {
41
42         /// Pointer to function to free (destruct and deallocate) retired pointer of specific type
43         typedef void (* free_retired_ptr_func )( void * );
44
45         /// Retired pointer
46         /**
47             Pointer to an object that is ready to delete.
48         */
49         struct retired_ptr {
50             /// Pointer type
51             typedef void *          pointer;
52
53             union {
54                 pointer                 m_p;        ///< retired pointer
55                 uintptr_t               m_n;
56             };
57             free_retired_ptr_func   m_funcFree; ///< pointer to the destructor function
58
59             /// Comparison of two retired pointers
60             static bool less( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
61             {
62                 return p1.m_p < p2.m_p;
63             }
64
65             /// Default ctor initializes pointer to \p nullptr
66             retired_ptr() CDS_NOEXCEPT
67                 : m_p( nullptr )
68                 , m_funcFree( nullptr )
69             {}
70
71             /// Ctor
72             retired_ptr( pointer p, free_retired_ptr_func func ) CDS_NOEXCEPT
73                 : m_p( p )
74                 , m_funcFree( func )
75             {}
76
77             /// Typecasting ctor
78             template <typename T>
79             retired_ptr( T* p, free_retired_ptr_func func) CDS_NOEXCEPT
80                 : m_p( reinterpret_cast<pointer>(p))
81                 , m_funcFree( func )
82             {}
83 /*
84             template <typename T>
85             retired_ptr( T * p, void (* pFreeFunc)(T *)) CDS_NOEXCEPT
86                 : m_p( reinterpret_cast<pointer>(p))
87                 , m_funcFree( reinterpret_cast< free_retired_ptr_func >( pFreeFunc ))
88             {}
89 */
90
91             /// Assignment operator
92             retired_ptr& operator =( retired_ptr const& s) CDS_NOEXCEPT
93             {
94                 m_p = s.m_p;
95                 m_funcFree = s.m_funcFree;
96                 return *this;
97             }
98
99             /// Invokes destructor function for the pointer
100             void free()
101             {
102                 assert( m_funcFree );
103                 assert( m_p );
104                 m_funcFree( m_p );
105
106                 CDS_STRICT_DO( clear());
107             }
108
109             /// Checks if the retired pointer is not empty
110             explicit operator bool() const CDS_NOEXCEPT
111             {
112                 return m_p != nullptr;
113             }
114
115             /// Clears retired pointer without \p free() call
116             void clear()
117             {
118                 m_p = nullptr;
119                 m_funcFree = nullptr;
120             }
121         };
122
123         static inline bool operator <( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
124         {
125             return retired_ptr::less( p1, p2 );
126         }
127
128         static inline bool operator ==( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
129         {
130             return p1.m_p == p2.m_p;
131         }
132
133         static inline bool operator !=( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
134         {
135             return !(p1 == p2);
136         }
137     }  // namespace details
138
139     template <typename Func, typename T>
140     static inline cds::gc::details::retired_ptr make_retired_ptr( T * p )
141     {
142         return cds::gc::details::retired_ptr( p, cds::details::static_functor<Func, T>::call );
143     }
144
145 }}   // namespace cds::gc
146 //@endcond
147
148 #endif // #ifndef CDSLIB_GC_DETAILS_RETIRED_PTR_H