• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Used in Boost.MultiIndex tests.
2  *
3  * Copyright 2003-2020 Joaquin M Lopez Munoz.
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org/libs/multi_index for library home page.
9  */
10 
11 #ifndef BOOST_MULTI_INDEX_TEST_COUNT_ALLOCATOR_HPP
12 #define BOOST_MULTI_INDEX_TEST_COUNT_ALLOCATOR_HPP
13 
14 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
15 #include <memory>
16 
17 template<typename T>
18 struct count_allocator:std::allocator<T>
19 {
20   typedef std::allocator<T> super;
21   template<class U>
22   struct rebind{typedef count_allocator<U> other;};
23 
count_allocatorcount_allocator24   count_allocator(std::size_t& element_count,std::size_t& allocator_count):
25     pelement_count(&element_count),pallocator_count(&allocator_count)
26     {++(*pallocator_count);}
count_allocatorcount_allocator27   count_allocator(const count_allocator<T>& x):
28     super(x),
29     pelement_count(x.pelement_count),pallocator_count(x.pallocator_count)
30     {++(*pallocator_count);}
count_allocatorcount_allocator31   template<class U>count_allocator(const count_allocator<U>& x):
32     super(x),
33     pelement_count(x.pelement_count),pallocator_count(x.pallocator_count)
34     {++(*pallocator_count);}
~count_allocatorcount_allocator35   ~count_allocator()
36     {--(*pallocator_count);}
37 
operator =count_allocator38   count_allocator& operator=(const count_allocator<T>& x)
39   {
40     pelement_count=x.pelement_count;
41     pallocator_count=x.pallocator_count;
42     return *this;
43   }
44 
allocatecount_allocator45   T* allocate(std::size_t n)
46   {
47     *pelement_count+=n;
48     return super::allocate(n);
49   }
50 
deallocatecount_allocator51   void deallocate(T* p,std::size_t n)
52   {
53     super::deallocate(p,n);
54     *pelement_count-=n;
55   }
56 
57   std::size_t* pelement_count;
58   std::size_t* pallocator_count;
59 };
60 
61 #endif
62