1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/offset_ptr.hpp>
13 #include <boost/interprocess/detail/type_traits.hpp>
14 #include <boost/intrusive/pointer_traits.hpp>
15 #include <boost/static_assert.hpp>
17 using namespace boost::interprocess;
30 bool test_types_and_conversions()
32 typedef offset_ptr<int> pint_t;
33 typedef offset_ptr<const int> pcint_t;
34 typedef offset_ptr<volatile int> pvint_t;
35 typedef offset_ptr<const volatile int> pcvint_t;
37 BOOST_STATIC_ASSERT((ipcdetail::is_same<pint_t::element_type, int>::value));
38 BOOST_STATIC_ASSERT((ipcdetail::is_same<pcint_t::element_type, const int>::value));
39 BOOST_STATIC_ASSERT((ipcdetail::is_same<pvint_t::element_type, volatile int>::value));
40 BOOST_STATIC_ASSERT((ipcdetail::is_same<pcvint_t::element_type, const volatile int>::value));
42 BOOST_STATIC_ASSERT((ipcdetail::is_same<pint_t::value_type, int>::value));
43 BOOST_STATIC_ASSERT((ipcdetail::is_same<pcint_t::value_type, int>::value));
44 BOOST_STATIC_ASSERT((ipcdetail::is_same<pvint_t::value_type, int>::value));
45 BOOST_STATIC_ASSERT((ipcdetail::is_same<pcvint_t::value_type, int>::value));
48 { pint_t pint(&dummy_int); pcint_t pcint(pint);
49 if(pcint.get() != &dummy_int) return false; }
50 { pint_t pint(&dummy_int); pvint_t pvint(pint);
51 if(pvint.get() != &dummy_int) return false; }
52 { pint_t pint(&dummy_int); pcvint_t pcvint(pint);
53 if(pcvint.get() != &dummy_int) return false; }
54 { pcint_t pcint(&dummy_int); pcvint_t pcvint(pcint);
55 if(pcvint.get() != &dummy_int) return false; }
56 { pvint_t pvint(&dummy_int); pcvint_t pcvint(pvint);
57 if(pcvint.get() != &dummy_int) return false; }
69 { pcint = pint; if(pcint.get() != &dummy_int) return false; }
70 { pvint = pint; if(pvint.get() != &dummy_int) return false; }
71 { pcvint = pint; if(pcvint.get() != &dummy_int) return false; }
72 { pcvint = pcint; if(pcvint.get() != &dummy_int) return false; }
73 { pcvint = pvint; if(pcvint.get() != &dummy_int) return false; }
97 if( (pcint - pint) != 0)
100 if( (pint - pcint) != 0)
106 template<class BasePtr, class DerivedPtr>
107 bool test_base_derived_impl()
109 typename DerivedPtr::element_type d;
110 DerivedPtr pderi(&d);
112 BasePtr pbase(pderi);
116 if(!(pbase == pderi))
118 if((pbase - pderi) != 0)
124 if(!(pbase <= pderi))
126 if(!(pbase >= pderi))
132 bool test_base_derived()
134 typedef offset_ptr<Base> pbase_t;
135 typedef offset_ptr<const Base> pcbas_t;
136 typedef offset_ptr<Derived> pderi_t;
137 typedef offset_ptr<VirtualDerived> pvder_t;
139 if(!test_base_derived_impl<pbase_t, pderi_t>())
141 if(!test_base_derived_impl<pbase_t, pvder_t>())
143 if(!test_base_derived_impl<pcbas_t, pderi_t>())
145 if(!test_base_derived_impl<pcbas_t, pvder_t>())
151 bool test_arithmetic()
153 typedef offset_ptr<int> pint_t;
154 const int NumValues = 5;
155 int values[NumValues];
159 if(p.get() != values)
162 //Initialize p + NumValues
163 pint_t pe = &values[NumValues];
166 if(pe.get() != &values[NumValues])
170 if((pe - p) != NumValues)
173 if((pe - NumValues) != p)
176 if((p + NumValues) != pe)
179 if((NumValues + p) != pe)
182 if(pint_t(&p[NumValues]) != pe)
184 if(pint_t(&pe[-NumValues]) != p)
200 for(int j = 0; j != NumValues; ++j, ++penew);
205 for(int j = 0; j != NumValues; ++j, --p0);
210 for(int j = 0; j != NumValues; ++j){
211 pint_t p_new_copy = penew;
212 if(p_new_copy != penew++)
217 for(int j = 0; j != NumValues; ++j){
226 bool test_comparison()
228 typedef offset_ptr<int> pint_t;
229 const int NumValues = 5;
230 int values[NumValues];
234 if(p.get() != values)
237 //Initialize p + NumValues
238 pint_t pe = &values[NumValues];
241 if(pe.get() != &values[NumValues])
261 bool test_pointer_traits()
263 typedef offset_ptr<int> OInt;
264 typedef boost::intrusive::pointer_traits< OInt > PTOInt;
265 BOOST_STATIC_ASSERT((ipcdetail::is_same<PTOInt::element_type, int>::value));
266 BOOST_STATIC_ASSERT((ipcdetail::is_same<PTOInt::pointer, OInt >::value));
267 BOOST_STATIC_ASSERT((ipcdetail::is_same<PTOInt::difference_type, OInt::difference_type >::value));
268 BOOST_STATIC_ASSERT((ipcdetail::is_same<PTOInt::rebind_pointer<double>::type, offset_ptr<double> >::value));
271 if(boost::intrusive::pointer_traits<OInt>::pointer_to(dummy) != oi){
279 if(!test_types_and_conversions())
281 if(!test_base_derived())
283 if(!test_arithmetic())
285 if(!test_comparison())
287 if(!test_pointer_traits())
292 #include <boost/interprocess/detail/config_end.hpp>
295 //Offset ptr benchmark
298 #include <boost/interprocess/managed_shared_memory.hpp>
299 #include <boost/interprocess/containers/vector.hpp>
300 #include <boost/interprocess/allocators/allocator.hpp>
301 #include <boost/timer.hpp>
306 Ty accumulate2(InIt First, InIt Last, Ty Val)
307 { // return sum of Val and all in [First, Last)
308 for (; First != Last; ++First) //First = First + 1)
313 template <typename Vector>
314 void time_test(const Vector& vec, std::size_t iterations, const char* label) {
315 // assert(!vec.empty())
317 typename Vector::const_iterator first = vec.begin();
318 typename Vector::value_type result(0);
319 while (iterations != 0) {
320 result = accumulate2(first, first + vec.size(), result);
323 std::cout << label << t.elapsed() << " " << result << std::endl;
328 using namespace boost::interprocess;
329 typedef allocator<double, managed_shared_memory::segment_manager> alloc_t;
331 std::size_t n = 0x1 << 26;
332 std::size_t file_size = n * sizeof(double) + 1000000;
335 shared_memory_object::remove("MyMappedFile");
336 managed_shared_memory segment(open_or_create, "MyMappedFile", file_size);
337 shared_memory_object::remove("MyMappedFile");
338 alloc_t alloc_inst(segment.get_segment_manager());
339 vector<double, alloc_t> v0(n, double(42.42), alloc_inst);
340 time_test(v0, 10, "iterator shared: ");
343 std::vector<double> v1(n, double(42.42));
344 time_test(v1, 10, "iterator non-shared: ");