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 #ifndef BOOST_POOL_TEST_SIMP_SEG_STORE_HPP
10 #define BOOST_POOL_TEST_SIMP_SEG_STORE_HPP
12 #include <boost/pool/simple_segregated_storage.hpp>
13 #include <boost/assert.hpp>
15 #include <boost/detail/lightweight_test.hpp>
24 class test_simp_seg_store : public boost::simple_segregated_storage<std::size_t>
27 // ::first is the address of the start of the added block,
28 // ::second is the size in bytes of the added block
29 std::vector<std::pair<void*, std::size_t> > allocated_blocks;
31 std::set<void*> allocated_chunks;
33 void set_partition_size(const size_type sz)
35 if(allocated_blocks.empty())
41 BOOST_ASSERT(np_sz == sz);
45 // Return: true if chunk is from added blocks, false otherwise
46 bool is_inside_allocated_blocks(void* const chunk) const
48 typedef std::vector<std::pair<void*, std::size_t> >::const_iterator
50 for(VPIter iter = allocated_blocks.begin();
51 iter != allocated_blocks.end();
54 if( std::less_equal<void*>()(iter->first, chunk)
55 && std::less_equal<void*>()(static_cast<char*>(chunk) + np_sz,
56 static_cast<char*>(iter->first) + iter->second) )
65 void check_in(void* const chunk)
67 // Check that the newly allocated chunk has not already previously
68 // been allocated, and that the memory does not overlap with
69 // previously allocated chunks
70 for(std::set<void*>::const_iterator iter = allocated_chunks.begin();
71 iter != allocated_chunks.end();
74 BOOST_TEST( std::less_equal<void*>()(static_cast<char*>(chunk)
76 || std::less_equal<void*>()(static_cast<char*>(*iter)
80 allocated_chunks.insert(chunk);
83 void check_out(void* const chunk)
85 BOOST_TEST(allocated_chunks.erase(chunk) == 1);
92 void* get_first() { return first; }
93 static void*& get_nextof(void* const ptr) { return nextof(ptr); }
95 // (Test) Pre: npartition_sz of all added block is the same
96 // different blocks of memory does not overlap
97 void add_block(void* const block,
98 const size_type nsz, const size_type npartition_sz)
100 set_partition_size(npartition_sz);
101 allocated_blocks.push_back(
102 std::pair<void*, std::size_t>(block, nsz) );
103 boost::simple_segregated_storage<std::size_t>::add_block(
104 block, nsz, npartition_sz );
106 BOOST_TEST(!empty());
109 // (Test) Pre: npartition_sz of all added block is the same
110 // different blocks of memory does not overlap
111 void add_ordered_block(void* const block,
112 const size_type nsz, const size_type npartition_sz)
114 set_partition_size(npartition_sz);
115 allocated_blocks.push_back(
116 std::pair<void*, std::size_t>(block, nsz) );
117 boost::simple_segregated_storage<std::size_t>::add_ordered_block(
118 block, nsz, npartition_sz );
120 BOOST_TEST(!empty());
126 = boost::simple_segregated_storage<std::size_t>::malloc();
127 // Chunk returned should actually be from added blocks
128 BOOST_TEST(is_inside_allocated_blocks(ret));
133 void free(void* const chunk)
137 boost::simple_segregated_storage<std::size_t>::free(chunk);
139 BOOST_TEST(!empty());
142 void ordered_free(void* const chunk)
146 boost::simple_segregated_storage<std::size_t>::ordered_free(chunk);
148 BOOST_TEST(!empty());
151 void* malloc_n(size_type n, size_type partition_size)
154 = boost::simple_segregated_storage<std::size_t>::malloc_n(
159 for(std::size_t i=0; i < n; ++i)
161 void* const chunk = static_cast<char*>(ret)
162 + (i * partition_size);
163 // Memory returned should actually be from added blocks
164 BOOST_TEST(is_inside_allocated_blocks(chunk));
173 #endif // BOOST_POOL_TEST_SIMP_SEG_STORE_HPP