Move libcds 1.6.0 from SVN
[libcds.git] / tests / unit / ellen_bintree_update_desc_pool.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_UNIT_ELLEN_BINTREE_UPDATE_DESC_POOL_H
4 #define __CDS_UNIT_ELLEN_BINTREE_UPDATE_DESC_POOL_H
5
6 #include <cds/urcu/general_instant.h>
7 #include <cds/container/ellen_bintree_base.h>
8 #include <cds/memory/vyukov_queue_pool.h>
9 #include <cds/memory/pool_allocator.h>
10
11 namespace ellen_bintree_pool {
12     typedef cds::container::ellen_bintree::node_types< cds::urcu::gc< cds::urcu::general_instant<> >, int > node_types ; // fake
13     typedef node_types::leaf_node_type     leaf_node;
14     typedef node_types::internal_node_type internal_node;
15     typedef node_types::update_desc_type   update_desc;
16
17     // Update descriptor pool based on Vyukov's queue
18     typedef cds::memory::vyukov_queue_pool<
19         update_desc,
20         cds::opt::buffer< cds::opt::v::static_buffer< cds::any_type, 4096 > >
21     > update_desc_pool_type;
22
23     extern update_desc_pool_type s_UpdateDescPool;
24
25     struct update_desc_pool_accessor {
26         typedef update_desc_pool_type::value_type     value_type;
27
28         update_desc_pool_type& operator()() const
29         {
30             return s_UpdateDescPool;
31         }
32     };
33
34     // Update descriptor pool based on bounded Vyukov's queue
35     typedef cds::memory::bounded_vyukov_queue_pool<
36         update_desc,
37         cds::opt::buffer< cds::opt::v::static_buffer< cds::any_type, 4096 > >
38     > bounded_update_desc_pool_type;
39
40     extern bounded_update_desc_pool_type s_BoundedUpdateDescPool;
41
42     struct bounded_update_desc_pool_accessor {
43         typedef bounded_update_desc_pool_type::value_type     value_type;
44
45         bounded_update_desc_pool_type& operator()() const
46         {
47             return s_BoundedUpdateDescPool;
48         }
49     };
50
51
52     // Internal node allocator
53     struct internal_node_counter
54     {
55         static cds::atomicity::event_counter   m_nAlloc;
56         static cds::atomicity::event_counter   m_nFree;
57
58         static void onAlloc()
59         {
60             ++m_nAlloc;
61         }
62         static void onFree()
63         {
64             ++m_nFree;
65         }
66
67         static void reset()
68         {
69             m_nAlloc.reset();
70             m_nFree.reset();
71         }
72     };
73
74     template <typename T, typename Alloc = CDS_DEFAULT_ALLOCATOR>
75     class internal_node_allocator
76         : public Alloc::template rebind< T >::other
77         , internal_node_counter
78     {
79         typedef typename Alloc::template rebind< T >::other  base_class;
80     public:
81         template <typename Other>
82         struct rebind {
83             typedef internal_node_allocator< Other, Alloc > other;
84         };
85
86         T * allocate( size_t n, void const * pHint = NULL )
87         {
88             internal_node_counter::onAlloc();
89             return base_class::allocate( n, pHint );
90         }
91
92         void deallocate( T * p, size_t n )
93         {
94             internal_node_counter::onFree();
95             return base_class::deallocate( p, n );
96         }
97     };
98
99 } // namespace ellen_bintree_pool
100
101 #endif // #ifndef __CDS_UNIT_ELLEN_BINTREE_UPDATE_DESC_POOL_H