1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // |
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) |
6 | // |
7 | // See http://www.boost.org/libs/interprocess for documentation. |
8 | // |
9 | ////////////////////////////////////////////////////////////////////////////// |
10 | |
11 | //boost::unordered is supported only in C++11 and newer |
12 | #include <boost/config.hpp> |
13 | |
14 | #if BOOST_CXX_VERSION >=201103L |
15 | |
16 | #include <boost/interprocess/managed_shared_memory.hpp> |
17 | #include <boost/interprocess/allocators/allocator.hpp> |
18 | #include "get_process_id_name.hpp" |
19 | |
20 | //<- |
21 | //Shield against external warnings |
22 | #include <boost/interprocess/detail/config_external_begin.hpp> |
23 | //-> |
24 | |
25 | #include <boost/unordered_map.hpp> |
26 | #include <boost/unordered_set.hpp> |
27 | |
28 | //<- |
29 | #include <boost/interprocess/detail/config_external_end.hpp> |
30 | //-> |
31 | |
32 | #include <functional> //std::equal_to |
33 | #include <boost/container_hash/hash.hpp> //boost::hash |
34 | |
35 | namespace bip = boost::interprocess; |
36 | |
37 | typedef bip::allocator<int, bip::managed_shared_memory::segment_manager> ShmemAllocator; |
38 | typedef boost::unordered_set<int, boost::hash<int>, std::equal_to<int>, ShmemAllocator> MyUnorderedSet; |
39 | typedef boost::unordered_multiset<int, boost::hash<int>, std::equal_to<int>, ShmemAllocator> MyUnorderedMultiSet; |
40 | |
41 | int main() |
42 | { |
43 | //Remove any other old shared memory from the system |
44 | bip::shared_memory_object::remove(filename: bip::test::get_process_id_name()); |
45 | BOOST_TRY { |
46 | bip::managed_shared_memory shm(bip::create_only, bip::test::get_process_id_name(), 65536); |
47 | |
48 | //Elements to be inserted in unordered containers |
49 | const int elements[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
50 | const std::size_t elements_size = sizeof(elements)/sizeof(elements[0]); |
51 | |
52 | MyUnorderedSet *myset = |
53 | shm.construct<MyUnorderedSet>(name: bip::anonymous_instance) |
54 | ( elements_size |
55 | , MyUnorderedSet::hasher() |
56 | , MyUnorderedSet::key_equal() |
57 | , shm.get_allocator<int>()); |
58 | MyUnorderedMultiSet *mymset = |
59 | shm.construct<MyUnorderedMultiSet>(name: bip::anonymous_instance) |
60 | ( elements_size |
61 | , MyUnorderedSet::hasher() |
62 | , MyUnorderedSet::key_equal() |
63 | , shm.get_allocator<int>()); |
64 | |
65 | //Insert elements and check sizes |
66 | myset->insert(first: (&elements[0]), last: (&elements[std::ptrdiff_t(elements_size)])); |
67 | myset->insert(first: (&elements[0]), last: (&elements[std::ptrdiff_t(elements_size)])); |
68 | mymset->insert(first: (&elements[0]), last: (&elements[std::ptrdiff_t(elements_size)])); |
69 | mymset->insert(first: (&elements[0]), last: (&elements[std::ptrdiff_t(elements_size)])); |
70 | |
71 | if(myset->size() != elements_size) |
72 | return 1; |
73 | if(mymset->size() != elements_size*2u) |
74 | return 1; |
75 | |
76 | //Destroy elements and check sizes |
77 | myset->clear(); |
78 | mymset->clear(); |
79 | |
80 | if(!myset->empty()) |
81 | return 1; |
82 | if(!mymset->empty()) |
83 | return 1; |
84 | |
85 | //Destroy elements and check if memory has been deallocated |
86 | shm.destroy_ptr(ptr: myset); |
87 | shm.destroy_ptr(ptr: mymset); |
88 | |
89 | shm.shrink_to_fit_indexes(); |
90 | if(!shm.all_memory_deallocated()) |
91 | return 1; |
92 | |
93 | } |
94 | BOOST_CATCH(...){ |
95 | //Remove shared memory from the system |
96 | bip::shared_memory_object::remove(filename: bip::test::get_process_id_name()); |
97 | BOOST_RETHROW |
98 | } BOOST_CATCH_END |
99 | //Remove shared memory from the system |
100 | bip::shared_memory_object::remove(filename: bip::test::get_process_id_name()); |
101 | return 0; |
102 | } |
103 | |
104 | #else |
105 | int main() |
106 | { |
107 | return 0; |
108 | } |
109 | #endif //#if BOOST_CXX_VERSION >=201103L |
110 | |