Replace NULL with nullptr
[libcds.git] / tests / unit / michael_alloc.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_UNIT_MICHAEL_ALLOC_H
4 #define __CDS_UNIT_MICHAEL_ALLOC_H
5
6 #include <cds/memory/michael/allocator.h>
7 #include <memory>
8
9 namespace memory {
10
11     typedef cds::memory::michael::Heap<
12         cds::memory::michael::opt::check_bounds< cds::memory::michael::debug_bound_checking >
13     >    michael_heap;
14     extern michael_heap s_MichaelHeap;
15
16     template <class T>
17     class MichaelAllocator
18     {
19         typedef std::allocator<T>               std_allocator;
20     public:
21         // Declare typedefs from std::allocator
22         typedef typename std_allocator::const_pointer   const_pointer;
23         typedef typename std_allocator::pointer         pointer;
24         typedef typename std_allocator::const_reference const_reference;
25         typedef typename std_allocator::reference       reference;
26         typedef typename std_allocator::difference_type difference_type;
27         typedef typename std_allocator::size_type       size_type;
28         typedef typename std_allocator::value_type      value_type;
29
30         // Allocation function
31         pointer allocate( size_type _Count, const void* _Hint = nullptr )
32         {
33             return reinterpret_cast<pointer>( s_MichaelHeap.alloc( sizeof(T) * _Count ));
34         }
35
36         // Deallocation function
37         void deallocate( pointer _Ptr, size_type _Count )
38         {
39             s_MichaelHeap.free( _Ptr );
40         }
41
42         pointer address( reference r ) const
43         {
44             return &r;
45         }
46         const_pointer address( const_reference r ) const
47         {
48             return &r;
49         }
50         void construct( pointer p, const T& val )
51         {
52             return new( p ) T( val );
53         }
54         void destroy( pointer p )
55         {
56             p->T::~T();
57         }
58
59         // Rebinding allocator to other type
60         template <class Other>
61         struct rebind {
62             typedef MichaelAllocator<Other> other;
63         };
64     };
65 }   // namespace memory
66
67 #endif