bd199c946ab10d6b4e7e17ea8b292b3401575574
[libcds.git] / test / stress / framework / ellen_bintree_update_desc_pool.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 CDSUNIT_ELLEN_BINTREE_UPDATE_DESC_POOL_H
32 #define CDSUNIT_ELLEN_BINTREE_UPDATE_DESC_POOL_H
33
34 #include <cds/urcu/general_instant.h>
35 #include <cds/container/details/ellen_bintree_base.h>
36 #include <cds/memory/vyukov_queue_pool.h>
37 #include <cds/memory/pool_allocator.h>
38
39 namespace ellen_bintree_pool {
40     typedef cds::container::ellen_bintree::node_types< cds::urcu::gc< cds::urcu::general_instant<> >, int > node_types; // fake
41     typedef node_types::leaf_node_type     leaf_node;
42     typedef node_types::internal_node_type internal_node;
43     typedef node_types::update_desc_type   update_desc;
44
45     // Update descriptor pool based on Vyukov's queue
46     struct update_desc_pool_traits : public cds::memory::vyukov_queue_pool_traits
47     {
48         typedef cds::opt::v::static_buffer< cds::any_type, 4096 > buffer;
49     };
50
51     typedef cds::memory::vyukov_queue_pool< update_desc, update_desc_pool_traits > update_desc_pool_type;
52     extern update_desc_pool_type s_UpdateDescPool;
53
54     struct update_desc_pool_accessor {
55         typedef update_desc_pool_type::value_type     value_type;
56
57         update_desc_pool_type& operator()() const
58         {
59             return s_UpdateDescPool;
60         }
61     };
62
63     // Update descriptor pool based on bounded Vyukov's queue
64     struct bounded_update_desc_pool_traits : public cds::memory::vyukov_queue_pool_traits
65     {
66         typedef cds::opt::v::static_buffer< cds::any_type, 4096 > buffer;
67     };
68     typedef cds::memory::bounded_vyukov_queue_pool< update_desc, bounded_update_desc_pool_traits > bounded_update_desc_pool_type;
69     extern bounded_update_desc_pool_type s_BoundedUpdateDescPool;
70
71     struct bounded_update_desc_pool_accessor {
72         typedef bounded_update_desc_pool_type::value_type     value_type;
73
74         bounded_update_desc_pool_type& operator()() const
75         {
76             return s_BoundedUpdateDescPool;
77         }
78     };
79
80
81     // Internal node allocator
82     struct internal_node_counter
83     {
84         static cds::atomicity::event_counter   m_nAlloc;
85         static cds::atomicity::event_counter   m_nFree;
86
87         static void onAlloc()
88         {
89             ++m_nAlloc;
90         }
91         static void onFree()
92         {
93             ++m_nFree;
94         }
95
96         static void reset()
97         {
98             m_nAlloc.reset();
99             m_nFree.reset();
100         }
101     };
102
103     template <typename T, typename Alloc = CDS_DEFAULT_ALLOCATOR>
104     class internal_node_allocator
105         : public Alloc::template rebind< T >::other
106         , internal_node_counter
107     {
108         typedef typename Alloc::template rebind< T >::other  base_class;
109     public:
110         template <typename Other>
111         struct rebind {
112             typedef internal_node_allocator< Other, Alloc > other;
113         };
114
115         T * allocate( size_t n, void const * pHint = nullptr )
116         {
117             internal_node_counter::onAlloc();
118             T * p = base_class::allocate( n, pHint );
119             return p;
120         }
121
122         void deallocate( T * p, size_t n )
123         {
124             internal_node_counter::onFree();
125             return base_class::deallocate( p, n );
126         }
127     };
128
129 } // namespace ellen_bintree_pool
130
131 #endif // #ifndef CDSUNIT_ELLEN_BINTREE_UPDATE_DESC_POOL_H