1 /* Copyright (C) 2000, 2001 Stephen Cleary
2 * Copyright (C) 2011 Kwan Ting Chan
4 * Use, modification and distribution is subject to the
5 * Boost Software License, Version 1.0. (See accompanying
6 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
9 #include <boost/pool/pool_alloc.hpp>
10 #include <boost/pool/object_pool.hpp>
12 #include <boost/detail/lightweight_test.hpp>
24 // Each "tester" object below checks into and out of the "cdtor_checker",
25 // which will check for any problems related to the construction/destruction of
30 // Each constructed object registers its "this" pointer into "objs"
34 // True iff all objects that have checked in have checked out
35 bool ok() const { return objs
.empty(); }
42 void check_in(void * const This
)
44 BOOST_TEST(objs
.find(This
) == objs
.end());
48 void check_out(void * const This
)
50 BOOST_TEST(objs
.find(This
) != objs
.end());
54 static cdtor_checker mem
;
58 tester(bool throw_except
= false)
62 throw std::logic_error("Deliberate constructor exception");
68 tester(const tester
&)
79 // This is a wrapper around a UserAllocator. It just registers alloc/dealloc
80 // to/from the system memory. It's used to make sure pool's are allocating
81 // and deallocating system memory properly.
82 // Do NOT use this class with static or singleton pools.
83 template <typename UserAllocator
>
86 typedef typename
UserAllocator::size_type size_type
;
87 typedef typename
UserAllocator::difference_type difference_type
;
89 static std::set
<char *> allocated_blocks
;
91 static char * malloc(const size_type bytes
)
93 char * const ret
= UserAllocator::malloc(bytes
);
94 allocated_blocks
.insert(ret
);
98 static void free(char * const block
)
100 BOOST_TEST(allocated_blocks
.find(block
) != allocated_blocks
.end());
101 allocated_blocks
.erase(block
);
102 UserAllocator::free(block
);
107 return allocated_blocks
.empty();
110 template <typename UserAllocator
>
111 std::set
<char *> TrackAlloc
<UserAllocator
>::allocated_blocks
;
113 typedef TrackAlloc
<boost::default_user_allocator_new_delete
> track_alloc
;
119 boost::object_pool
<tester
> pool
;
123 // Construct several tester objects. Don't delete them (i.e.,
124 // test pool's garbage collection).
125 boost::object_pool
<tester
> pool
;
126 for(int i
=0; i
< 10; ++i
)
133 // Construct several tester objects. Delete some of them.
134 boost::object_pool
<tester
> pool
;
135 std::vector
<tester
*> v
;
136 for(int i
=0; i
< 10; ++i
)
138 v
.push_back(pool
.construct());
140 std::random_shuffle(v
.begin(), v
.end());
141 for(int j
=0; j
< 5; ++j
)
148 // Test how pool reacts with constructors that throw exceptions.
149 // Shouldn't have any memory leaks.
150 boost::object_pool
<tester
> pool
;
151 for(int i
=0; i
< 5; ++i
)
155 for(int j
=0; j
< 5; ++j
)
159 // The following constructions will raise an exception.
160 pool
.construct(true);
162 catch(const std::logic_error
&) {}
170 // Allocate several tester objects. Delete one.
171 std::vector
<tester
, boost::pool_allocator
<tester
> > l
;
172 for(int i
=0; i
< 10; ++i
)
174 l
.push_back(tester());
180 // Allocate several tester objects. Delete two.
181 std::deque
<tester
, boost::pool_allocator
<tester
> > l
;
182 for(int i
=0; i
< 10; ++i
)
184 l
.push_back(tester());
191 // Allocate several tester objects. Delete two.
192 std::list
<tester
, boost::fast_pool_allocator
<tester
> > l
;
193 // lists rebind their allocators, so dumping is useless
194 for(int i
=0; i
< 10; ++i
)
196 l
.push_back(tester());
204 // Create a memory leak on purpose. (Allocator doesn't have
205 // garbage collection)
206 // (Note: memory leak)
207 boost::pool_allocator
<tester
> a
;
208 tmp
= a
.allocate(1, 0);
213 BOOST_ERROR("Pool allocator cleaned up itself");
215 // Remove memory checker entry (to avoid error later) and
216 // clean up memory leak
218 boost::pool_allocator
<tester
>::deallocate(tmp
, 1);
220 // test allocating zero elements
222 boost::pool_allocator
<tester
> alloc
;
223 tester
* ip
= alloc
.allocate(0);
224 alloc
.deallocate(ip
, 0);
228 void test_mem_usage()
230 typedef boost::pool
<track_alloc
> pool_type
;
233 // Constructor should do nothing; no memory allocation
234 pool_type
pool(sizeof(int));
235 BOOST_TEST(track_alloc::ok());
236 BOOST_TEST(!pool
.release_memory());
237 BOOST_TEST(!pool
.purge_memory());
239 // Should allocate from system
240 pool
.free(pool
.malloc());
241 BOOST_TEST(!track_alloc::ok());
243 // Ask pool to give up memory it's not using; this should succeed
244 BOOST_TEST(pool
.release_memory());
245 BOOST_TEST(track_alloc::ok());
247 // Should allocate from system again
248 pool
.malloc(); // loses the pointer to the returned chunk (*A*)
250 // Ask pool to give up memory it's not using; this should fail
251 BOOST_TEST(!pool
.release_memory());
253 // Force pool to give up memory it's not using; this should succeed
254 // This will clean up the memory leak from (*A*)
255 BOOST_TEST(pool
.purge_memory());
256 BOOST_TEST(track_alloc::ok());
258 // Should allocate from system again
259 pool
.malloc(); // loses the pointer to the returned chunk (*B*)
261 // pool's destructor should purge the memory
262 // This will clean up the memory leak from (*B*)
265 BOOST_TEST(track_alloc::ok());
270 typedef boost::pool_allocator
<void> void_allocator
;
271 typedef boost::fast_pool_allocator
<void> fast_void_allocator
;
273 typedef void_allocator::rebind
<int>::other int_allocator
;
274 typedef fast_void_allocator::rebind
<int>::other fast_int_allocator
;
276 std::vector
<int, int_allocator
> v1
;
277 std::vector
<int, fast_int_allocator
> v2
;
282 std::srand(static_cast<unsigned>(std::time(0)));
289 return boost::report_errors();