Removed redundant spaces
[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-2016
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, void (* pFreeFunc)(T *)) CDS_NOEXCEPT
80                 : m_p( reinterpret_cast<pointer>(p))
81                 , m_funcFree( reinterpret_cast< free_retired_ptr_func >( pFreeFunc ))
82             {}
83
84             /// Assignment operator
85             retired_ptr& operator =( retired_ptr const& s) CDS_NOEXCEPT
86             {
87                 m_p = s.m_p;
88                 m_funcFree = s.m_funcFree;
89                 return *this;
90             }
91
92             /// Invokes destructor function for the pointer
93             void free()
94             {
95                 assert( m_funcFree );
96                 assert( m_p );
97                 m_funcFree( m_p );
98
99                 CDS_STRICT_DO( clear());
100             }
101
102             /// Checks if the retired pointer is not empty
103             explicit operator bool() const CDS_NOEXCEPT
104             {
105                 return m_p != nullptr;
106             }
107
108             /// Clears retired pointer without \p free() call
109             void clear()
110             {
111                 m_p = nullptr;
112                 m_funcFree = nullptr;
113             }
114         };
115
116         static inline bool operator <( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
117         {
118             return retired_ptr::less( p1, p2 );
119         }
120
121         static inline bool operator ==( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
122         {
123             return p1.m_p == p2.m_p;
124         }
125
126         static inline bool operator !=( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
127         {
128             return !(p1 == p2);
129         }
130     }  // namespace details
131
132     template <typename Func, typename T>
133     static inline cds::gc::details::retired_ptr make_retired_ptr( T * p )
134     {
135         return cds::gc::details::retired_ptr( p, cds::details::static_functor<Func, T>::call );
136     }
137
138 }}   // namespace cds::gc
139 //@endcond
140
141 #endif // #ifndef CDSLIB_GC_DETAILS_RETIRED_PTR_H