2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_GC_DETAILS_RETIRED_PTR_H
32 #define CDSLIB_GC_DETAILS_RETIRED_PTR_H
34 #include <cds/details/defs.h>
35 #include <cds/details/static_functor.h>
38 namespace cds { namespace gc {
39 /// Common implementation details for any GC
42 /// Pointer to function to free (destruct and deallocate) retired pointer of specific type
43 typedef void (* free_retired_ptr_func )( void * );
47 Pointer to an object that is ready to delete.
51 typedef void * pointer;
54 pointer m_p; ///< retired pointer
57 free_retired_ptr_func m_funcFree; ///< pointer to the destructor function
59 /// Comparison of two retired pointers
60 static bool less( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
62 return p1.m_p < p2.m_p;
65 /// Default ctor initializes pointer to \p nullptr
66 retired_ptr() CDS_NOEXCEPT
68 , m_funcFree( nullptr )
72 retired_ptr( pointer p, free_retired_ptr_func func ) CDS_NOEXCEPT
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 ))
84 /// Assignment operator
85 retired_ptr& operator =( retired_ptr const& s) CDS_NOEXCEPT
88 m_funcFree = s.m_funcFree;
92 /// Invokes destructor function for the pointer
99 CDS_STRICT_DO( clear());
102 /// Checks if the retired pointer is not empty
103 explicit operator bool() const CDS_NOEXCEPT
105 return m_p != nullptr;
108 /// Clears retired pointer without \p free() call
112 m_funcFree = nullptr;
116 static inline bool operator <( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
118 return retired_ptr::less( p1, p2 );
121 static inline bool operator ==( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
123 return p1.m_p == p2.m_p;
126 static inline bool operator !=( const retired_ptr& p1, const retired_ptr& p2 ) CDS_NOEXCEPT
130 } // namespace details
132 template <typename Func, typename T>
133 static inline cds::gc::details::retired_ptr make_retired_ptr( T * p )
135 return cds::gc::details::retired_ptr( p, cds::details::static_functor<Func, T>::call );
138 }} // namespace cds::gc
141 #endif // #ifndef CDSLIB_GC_DETAILS_RETIRED_PTR_H