Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / smart_ptr / test / shared_ptr_alloc_test.cpp
1 //
2 //  shared_ptr_alloc_test.cpp - use to evaluate the impact of count allocations
3 //
4 //  Copyright (c) 2002, 2003 Peter Dimov
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <boost/shared_ptr.hpp>
12 #include <boost/config.hpp>
13 #include <boost/detail/quick_allocator.hpp>
14
15 #include <iostream>
16 #include <vector>
17 #include <ctime>
18 #include <cstddef>
19 #include <memory>
20
21 int const n = 1024 * 1024;
22
23 template<class T> void test(T * = 0)
24 {
25     std::clock_t t = std::clock();
26     std::clock_t t2;
27
28     {
29         std::vector< boost::shared_ptr<T> > v;
30
31         for(int i = 0; i < n; ++i)
32         {
33             boost::shared_ptr<T> pi(new T(i));
34             v.push_back(pi);
35         }
36
37         t2 = std::clock();
38     }
39
40     std::clock_t t3 = std::clock();
41
42     std::cout << "   " << static_cast<double>(t3 - t) / CLOCKS_PER_SEC << " seconds, " << static_cast<double>(t2 - t) / CLOCKS_PER_SEC << " + " << static_cast<double>(t3 - t2) / CLOCKS_PER_SEC << ".\n";
43 }
44
45 class X
46 {
47 public:
48
49     explicit X(int n): n_(n)
50     {
51     }
52
53     void * operator new(std::size_t)
54     {
55         return std::allocator<X>().allocate(1, static_cast<X*>(0));
56     }
57
58     void operator delete(void * p)
59     {
60         std::allocator<X>().deallocate(static_cast<X*>(p), 1);
61     }
62
63 private:
64
65     X(X const &);
66     X & operator=(X const &);
67
68     int n_;
69 };
70
71 class Y
72 {
73 public:
74
75     explicit Y(int n): n_(n)
76     {
77     }
78
79     void * operator new(std::size_t n)
80     {
81         return boost::detail::quick_allocator<Y>::alloc(n);
82     }
83
84     void operator delete(void * p, std::size_t n)
85     {
86         boost::detail::quick_allocator<Y>::dealloc(p, n);
87     }
88
89 private:
90
91     Y(Y const &);
92     Y & operator=(Y const &);
93
94     int n_;
95 };
96
97 class Z: public Y
98 {
99 public:
100
101     explicit Z(int n): Y(n), m_(n + 1)
102     {
103     }
104
105 private:
106
107     Z(Z const &);
108     Z & operator=(Z const &);
109
110     int m_;
111 };
112
113 int main()
114 {
115     std::cout << BOOST_COMPILER "\n";
116     std::cout << BOOST_PLATFORM "\n";
117     std::cout << BOOST_STDLIB "\n";
118
119 #if defined(BOOST_HAS_THREADS)
120     std::cout << "BOOST_HAS_THREADS: (defined)\n";
121 #else
122     std::cout << "BOOST_HAS_THREADS: (not defined)\n";
123 #endif
124
125 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
126     std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (defined)\n";
127 #else
128     std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (not defined)\n";
129 #endif
130
131 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
132     std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (defined)\n";
133 #else
134     std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (not defined)\n";
135 #endif
136
137 #if defined(BOOST_QA_PAGE_SIZE)
138     std::cout << "BOOST_QA_PAGE_SIZE: " << BOOST_QA_PAGE_SIZE << "\n";
139 #else
140     std::cout << "BOOST_QA_PAGE_SIZE: (not defined)\n";
141 #endif
142
143     std::cout << n << " shared_ptr<int> allocations + deallocations:\n";
144
145     test<int>();
146     test<int>();
147     test<int>();
148
149     std::cout << n << " shared_ptr<X> allocations + deallocations:\n";
150
151     test<X>();
152     test<X>();
153     test<X>();
154
155     std::cout << n << " shared_ptr<Y> allocations + deallocations:\n";
156
157     test<Y>();
158     test<Y>();
159     test<Y>();
160
161     std::cout << n << " shared_ptr<Z> allocations + deallocations:\n";
162
163     test<Z>();
164     test<Z>();
165     test<Z>();
166 }