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/workaround.hpp> |
12 | |
13 | #ifdef BOOST_INTERPROCESS_WINDOWS |
14 | |
15 | #include <boost/interprocess/allocators/allocator.hpp> |
16 | #include <boost/interprocess/containers/vector.hpp> |
17 | #include <boost/interprocess/managed_windows_shared_memory.hpp> |
18 | #include <cstdio> |
19 | #include <string> |
20 | #include "get_process_id_name.hpp" |
21 | |
22 | using namespace boost::interprocess; |
23 | |
24 | int main () |
25 | { |
26 | const int MemSize = 65536; |
27 | const char *const MemName = test::get_process_id_name(); |
28 | |
29 | //STL compatible allocator object for shared memory |
30 | typedef allocator<int, managed_windows_shared_memory::segment_manager> |
31 | allocator_int_t; |
32 | //A vector that uses that allocator |
33 | typedef boost::interprocess::vector<int, allocator_int_t> MyVect; |
34 | |
35 | { |
36 | const int max = 100; |
37 | void *array[std::size_t(max)]; |
38 | //Named allocate capable shared memory allocator |
39 | managed_windows_shared_memory w_shm(create_only, MemName, MemSize); |
40 | |
41 | std::size_t i; |
42 | //Let's allocate some memory |
43 | for(i = 0; i < max; ++i){ |
44 | array[std::ptrdiff_t(i)] = w_shm.allocate(i+1u); |
45 | } |
46 | |
47 | //Deallocate allocated memory |
48 | for(i = 0; i < max; ++i){ |
49 | w_shm.deallocate(array[std::ptrdiff_t(i)]); |
50 | } |
51 | } |
52 | |
53 | { |
54 | //Named allocate capable shared memory managed memory class |
55 | managed_windows_shared_memory w_shm(create_only, MemName, MemSize); |
56 | |
57 | //Construct the STL-like allocator with the segment manager |
58 | const allocator_int_t myallocator (w_shm.get_segment_manager()); |
59 | |
60 | //Named allocate capable shared memory managed memory class |
61 | managed_windows_shared_memory w_tmp(open_only, MemName); |
62 | } |
63 | { |
64 | bool throws_ok = false; |
65 | //Check memory is gone |
66 | BOOST_TRY{ |
67 | managed_windows_shared_memory w_tmp(open_only, MemName); |
68 | } |
69 | BOOST_CATCH(interprocess_exception &e) { |
70 | throws_ok = e.get_error_code() == not_found_error; |
71 | } |
72 | BOOST_CATCH_END |
73 | if (!throws_ok) |
74 | return 1; |
75 | } |
76 | { |
77 | //Named allocate capable shared memory managed memory class |
78 | managed_windows_shared_memory w_shm(open_or_create, MemName, MemSize); |
79 | |
80 | //Construct the STL-like allocator with the segment manager |
81 | const allocator_int_t myallocator (w_shm.get_segment_manager()); |
82 | |
83 | //Construct vector |
84 | MyVect *w_shm_vect = w_shm.construct<MyVect> ("MyVector" ) (myallocator); |
85 | |
86 | //Test that vector can be found via name |
87 | if(w_shm_vect != w_shm.find<MyVect>("MyVector" ).first) |
88 | return -1; |
89 | |
90 | //Destroy and check it is not present |
91 | w_shm.destroy<MyVect> ("MyVector" ); |
92 | if(0 != w_shm.find<MyVect>("MyVector" ).first) |
93 | return -1; |
94 | |
95 | //Construct a vector in the shared memory |
96 | w_shm_vect = w_shm.construct<MyVect> ("MyVector" ) (myallocator); |
97 | |
98 | { |
99 | //Map preexisting segment again in memory |
100 | managed_windows_shared_memory w_shm_new(open_only, MemName); |
101 | |
102 | //Check vector is still there |
103 | w_shm_vect = w_shm_new.find<MyVect>("MyVector" ).first; |
104 | if(!w_shm_vect) |
105 | return -1; |
106 | |
107 | if(w_shm_new.get_size() != w_shm.get_size()) |
108 | return 1; |
109 | } |
110 | { |
111 | //Map preexisting segment again in memory |
112 | managed_windows_shared_memory w_shm_new(open_or_create, MemName, MemSize); |
113 | |
114 | //Check vector is still there |
115 | w_shm_vect = w_shm_new.find<MyVect>("MyVector" ).first; |
116 | if(!w_shm_vect) |
117 | return -1; |
118 | |
119 | if(w_shm_new.get_size() != w_shm.get_size()) |
120 | return 1; |
121 | |
122 | { |
123 | { |
124 | //Map preexisting shmem again in copy-on-write |
125 | managed_windows_shared_memory shmem(open_copy_on_write, MemName); |
126 | |
127 | //Check vector is still there |
128 | MyVect *shmem_vect = shmem.find<MyVect>("MyVector" ).first; |
129 | if(!shmem_vect) |
130 | return -1; |
131 | |
132 | //Erase vector |
133 | shmem.destroy_ptr(shmem_vect); |
134 | |
135 | //Make sure vector is erased |
136 | shmem_vect = shmem.find<MyVect>("MyVector" ).first; |
137 | if(shmem_vect) |
138 | return -1; |
139 | } |
140 | //Now check vector is still in the s |
141 | { |
142 | //Map preexisting shmem again in copy-on-write |
143 | managed_windows_shared_memory shmem(open_copy_on_write, MemName); |
144 | |
145 | //Check vector is still there |
146 | MyVect *shmem_vect = shmem.find<MyVect>("MyVector" ).first; |
147 | if(!shmem_vect) |
148 | return -1; |
149 | } |
150 | } |
151 | { |
152 | //Map preexisting shmem again in read-only |
153 | managed_windows_shared_memory shmem(open_read_only, MemName); |
154 | |
155 | //Check vector is still there |
156 | MyVect *shmem_vect = shmem.find<MyVect>("MyVector" ).first; |
157 | if(!shmem_vect) |
158 | return -1; |
159 | } |
160 | |
161 | //Destroy and check it is not present |
162 | w_shm_new.destroy_ptr(w_shm_vect); |
163 | if(0 != w_shm_new.find<MyVect>("MyVector" ).first) |
164 | return 1; |
165 | |
166 | //Now test move semantics |
167 | managed_windows_shared_memory original(open_only, MemName); |
168 | managed_windows_shared_memory move_ctor(boost::move(original)); |
169 | managed_windows_shared_memory move_assign; |
170 | move_assign = boost::move(move_ctor); |
171 | } |
172 | } |
173 | |
174 | return 0; |
175 | } |
176 | |
177 | #else |
178 | |
179 | int main() |
180 | { |
181 | return 0; |
182 | } |
183 | |
184 | #endif |
185 | |