8e456fe07bbded2621616c223e076f875240dbfc
[libcds.git] / cds / container / details / make_skip_list_map.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_CONTAINER_DETAILS_MAKE_SKIP_LIST_MAP_H
32 #define CDSLIB_CONTAINER_DETAILS_MAKE_SKIP_LIST_MAP_H
33
34 #include <cds/container/details/skip_list_base.h>
35 #include <cds/details/binary_functor_wrapper.h>
36
37 //@cond
38 namespace cds { namespace container { namespace details {
39
40     template <typename GC, typename K, typename T, typename Traits>
41     struct make_skip_list_map
42     {
43         typedef GC      gc;
44         typedef K       key_type;
45         typedef T       mapped_type;
46         typedef std::pair< key_type const, mapped_type> value_type;
47         typedef Traits  traits;
48
49         typedef cds::intrusive::skip_list::node< gc >   intrusive_node_type;
50         struct node_type: public intrusive_node_type
51         {
52             typedef intrusive_node_type                     base_class;
53             typedef typename base_class::atomic_marked_ptr  atomic_marked_ptr;
54             typedef value_type                              stored_value_type;
55
56             value_type m_Value;
57             //atomic_marked_ptr m_arrTower[] ;  // allocated together with node_type in single memory block
58
59             template <typename Q>
60             node_type( unsigned int nHeight, atomic_marked_ptr * pTower, Q const& key )
61                 : m_Value( std::make_pair( key, mapped_type()))
62             {
63                 init_tower( nHeight, pTower );
64             }
65
66             template <typename Q, typename U>
67             node_type( unsigned int nHeight, atomic_marked_ptr * pTower, Q const& key, U const& val )
68                 : m_Value( std::make_pair( key, val ))
69             {
70                 init_tower( nHeight, pTower );
71             }
72
73             template <typename Q, typename... Args>
74             node_type( unsigned int nHeight, atomic_marked_ptr * pTower, Q&& key, Args&&... args )
75                 : m_Value( std::forward<Q>(key), std::move( mapped_type( std::forward<Args>(args)... )))
76             {
77                 init_tower( nHeight, pTower );
78             }
79
80             node_type() = delete;
81
82         private:
83             void init_tower( unsigned int nHeight, atomic_marked_ptr * pTower )
84             {
85                 if ( nHeight > 1 ) {
86                     // TSan: make_tower() issues atomic_thread_fence( release )
87                     CDS_TSAN_ANNOTATE_IGNORE_WRITES_BEGIN;
88                     new (pTower) atomic_marked_ptr[ nHeight - 1 ];
89                     base_class::make_tower( nHeight, pTower );
90                     CDS_TSAN_ANNOTATE_IGNORE_WRITES_END;
91                 }
92             }
93         };
94
95         class node_allocator : public skip_list::details::node_allocator< node_type, traits>
96         {
97             typedef skip_list::details::node_allocator< node_type, traits> base_class;
98         public:
99             template <typename Q>
100             node_type * New( unsigned int nHeight, Q const& key )
101             {
102                 return base_class::New( nHeight, key_type( key ));
103             }
104             template <typename Q, typename U>
105             node_type * New( unsigned int nHeight, Q const& key, U const& val )
106             {
107                 unsigned char * pMem = base_class::alloc_space( nHeight );
108                 return new( pMem )
109                     node_type( nHeight,
110                         nHeight > 1 ? reinterpret_cast<typename base_class::node_tower_item *>( pMem + base_class::c_nNodeSize ) : nullptr,
111                         key_type( key ), mapped_type( val )
112                     );
113             }
114             template <typename... Args>
115             node_type * New( unsigned int nHeight, Args&&... args )
116             {
117                 unsigned char * pMem = base_class::alloc_space( nHeight );
118                 return new( pMem )
119                     node_type( nHeight,
120                         nHeight > 1 ? reinterpret_cast<typename base_class::node_tower_item *>( pMem + base_class::c_nNodeSize ) : nullptr,
121                         std::forward<Args>(args)...
122                     );
123             }
124         };
125
126         struct node_deallocator {
127             void operator ()( node_type * pNode )
128             {
129                 node_allocator().Delete( pNode );
130             }
131         };
132
133         typedef skip_list::details::dummy_node_builder<intrusive_node_type> dummy_node_builder;
134
135         struct key_accessor
136         {
137             key_type const & operator()( node_type const& node ) const
138             {
139                 return node.m_Value.first;
140             }
141         };
142         typedef typename opt::details::make_comparator< key_type, traits >::type key_comparator;
143
144         class intrusive_type_traits: public cds::intrusive::skip_list::make_traits<
145             cds::opt::type_traits< traits >
146             ,cds::intrusive::opt::hook< intrusive::skip_list::base_hook< cds::opt::gc< gc > > >
147             ,cds::intrusive::opt::disposer< node_deallocator >
148             ,cds::intrusive::skip_list::internal_node_builder< dummy_node_builder >
149             ,cds::opt::compare< cds::details::compare_wrapper< node_type, key_comparator, key_accessor > >
150         >::type
151         {};
152
153         typedef cds::intrusive::SkipListSet< gc, node_type, intrusive_type_traits>   type;
154     };
155
156 }}} // namespace cds::container::details
157 //@endcond
158
159 #endif // CDSLIB_CONTAINER_DETAILS_MAKE_SKIP_LIST_MAP_H