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/allocators/allocator.hpp> |
12 | #include <boost/interprocess/managed_shared_memory.hpp> |
13 | #include <boost/interprocess/containers/vector.hpp> |
14 | #include <boost/interprocess/containers/list.hpp> |
15 | #include <iostream> |
16 | #include <functional> |
17 | #include "print_container.hpp" |
18 | #include <string> |
19 | #include "get_process_id_name.hpp" |
20 | |
21 | struct InstanceCounter |
22 | { |
23 | InstanceCounter(){++counter;} |
24 | InstanceCounter(const InstanceCounter&){++counter;} |
25 | InstanceCounter & operator=(const InstanceCounter&){ return *this; } |
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 | BOOST_TRY{ |
41 | shared_memory_object::remove(filename: 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 | BOOST_TRY{ |
56 | //Fill vector until there is no more memory |
57 | MyVect myvec(myallocator); |
58 | while(true){ |
59 | InstanceCounter a; |
60 | myvec.push_back(x: a); |
61 | } |
62 | } |
63 | BOOST_CATCH(boost::interprocess::bad_alloc &){ |
64 | if (InstanceCounter::counter != 0) { |
65 | std::cout << "Error: InstanceCounter::counter: " << InstanceCounter::counter; |
66 | return 1; |
67 | } |
68 | } BOOST_CATCH_END |
69 | |
70 | //We'll provoke an exception, let's see if exception handling works |
71 | BOOST_TRY{ |
72 | //Fill vector at the beginning until there is no more memory |
73 | MyVect myvec(myallocator); |
74 | std::size_t i; |
75 | InstanceCounter ic; |
76 | for(i = 0; true; ++i){ |
77 | myvec.insert(p: myvec.begin(), n: i, x: ic); |
78 | } |
79 | } |
80 | BOOST_CATCH(boost::interprocess::bad_alloc &){ |
81 | if(InstanceCounter::counter != 0){ |
82 | std::cout << "Error: InstanceCounter::counter: " << InstanceCounter::counter; |
83 | return 1; |
84 | } |
85 | } |
86 | BOOST_CATCH(std::length_error &){ |
87 | if(InstanceCounter::counter != 0){ |
88 | std::cout << "Error: InstanceCounter::counter: " << InstanceCounter::counter; |
89 | return 1; |
90 | } |
91 | } BOOST_CATCH_END |
92 | } |
93 | BOOST_CATCH(...){ |
94 | shared_memory_object::remove(filename: shMemName); |
95 | BOOST_RETHROW |
96 | } |
97 | BOOST_CATCH_END |
98 | |
99 | shared_memory_object::remove(filename: shMemName); |
100 | return 0; |
101 | } |
102 | |
103 | |