]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/test/allocexcept_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / interprocess / test / allocexcept_test.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2004-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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/allocators/allocator.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/containers/vector.hpp>
15 #include <boost/interprocess/containers/list.hpp>
16 #include <iostream>
17 #include <functional>
18 #include "print_container.hpp"
19 #include <string>
20 #include "get_process_id_name.hpp"
21
22 struct InstanceCounter
23 {
24 InstanceCounter(){++counter;}
25 InstanceCounter(const InstanceCounter&){++counter;}
26 ~InstanceCounter(){--counter;}
27 static std::size_t counter;
28 };
29
30 std::size_t InstanceCounter::counter = 0;
31
32 using namespace boost::interprocess;
33
34
35 int main ()
36 {
37 const int memsize = 16384;
38 const char *const shMemName = test::get_process_id_name();
39
40 try{
41 shared_memory_object::remove(shMemName);
42
43 //Named allocate capable shared mem allocator
44 managed_shared_memory segment(create_only, shMemName, memsize);
45
46 //STL compatible allocator object, uses allocate(), deallocate() functions
47 typedef allocator<InstanceCounter,
48 managed_shared_memory::segment_manager>
49 inst_allocator_t;
50 const inst_allocator_t myallocator (segment.get_segment_manager());
51
52 typedef vector<InstanceCounter, inst_allocator_t> MyVect;
53
54 //We'll provoke an exception, let's see if exception handling works
55 try{
56 //Fill vector until there is no more memory
57 MyVect myvec(myallocator);
58 int i;
59 for(i = 0; true; ++i){
60 myvec.push_back(InstanceCounter());
61 }
62 }
63 catch(boost::interprocess::bad_alloc &){
64 if(InstanceCounter::counter != 0)
65 return 1;
66 }
67
68 //We'll provoke an exception, let's see if exception handling works
69 try{
70 //Fill vector at the beginning until there is no more memory
71 MyVect myvec(myallocator);
72 int i;
73 InstanceCounter ic;
74 for(i = 0; true; ++i){
75 myvec.insert(myvec.begin(), i, ic);
76 }
77 }
78 catch(boost::interprocess::bad_alloc &){
79 if(InstanceCounter::counter != 0)
80 return 1;
81 }
82 catch(std::length_error &){
83 if(InstanceCounter::counter != 0)
84 return 1;
85 }
86 }
87 catch(...){
88 shared_memory_object::remove(shMemName);
89 throw;
90 }
91 shared_memory_object::remove(shMemName);
92 return 0;
93 }
94
95 #include <boost/interprocess/detail/config_end.hpp>