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/shared_memory_object.hpp> |
12 | #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> |
13 | #include <boost/interprocess/exceptions.hpp> |
14 | #include "named_creation_template.hpp" |
15 | #include <cstring> //for strcmp, memset |
16 | #include <iostream> //for cout |
17 | #include <string> |
18 | #include "get_process_id_name.hpp" |
19 | |
20 | using namespace boost::interprocess; |
21 | |
22 | static const std::size_t ShmSize = 1000; |
23 | static const char * ShmName = test::get_process_id_name(); |
24 | #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES |
25 | static const wchar_t * ShmNameW = test::get_process_id_wname(); |
26 | #endif |
27 | |
28 | struct eraser |
29 | { |
30 | ~eraser() |
31 | { |
32 | shared_memory_object::remove(filename: ShmName); |
33 | } |
34 | }; |
35 | |
36 | typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> shared_memory; |
37 | |
38 | //This wrapper is necessary to have a common constructor |
39 | //in generic named_creation_template functions |
40 | class shared_memory_creation_test_wrapper |
41 | : public eraser |
42 | , public shared_memory |
43 | { |
44 | |
45 | public: |
46 | shared_memory_creation_test_wrapper(create_only_t) |
47 | : shared_memory(create_only, ShmName, ShmSize, read_write, 0, permissions()) |
48 | {} |
49 | |
50 | shared_memory_creation_test_wrapper(open_only_t) |
51 | : shared_memory(open_only, ShmName, read_write, 0) |
52 | {} |
53 | |
54 | shared_memory_creation_test_wrapper(open_or_create_t) |
55 | : shared_memory(open_or_create, ShmName, ShmSize, read_write, 0, permissions()) |
56 | {} |
57 | }; |
58 | |
59 | #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES |
60 | |
61 | class shared_memory_creation_test_wrapper_w |
62 | : public eraser |
63 | , public shared_memory |
64 | { |
65 | |
66 | public: |
67 | shared_memory_creation_test_wrapper_w(create_only_t) |
68 | : shared_memory(create_only, ShmNameW, ShmSize, read_write, 0, permissions()) |
69 | {} |
70 | |
71 | shared_memory_creation_test_wrapper_w(open_only_t) |
72 | : shared_memory(open_only, ShmNameW, read_write, 0) |
73 | {} |
74 | |
75 | shared_memory_creation_test_wrapper_w(open_or_create_t) |
76 | : shared_memory(open_or_create, ShmNameW, ShmSize, read_write, 0, permissions()) |
77 | {} |
78 | }; |
79 | |
80 | #endif |
81 | |
82 | int main () |
83 | { |
84 | BOOST_TRY{ |
85 | shared_memory_object::remove(filename: ShmName); |
86 | test::test_named_creation<shared_memory_creation_test_wrapper>(); |
87 | #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES |
88 | test::test_named_creation<shared_memory_creation_test_wrapper_w>(); |
89 | #endif |
90 | |
91 | //Create and get name, size and address |
92 | { |
93 | shared_memory_object::remove(filename: ShmName); |
94 | shared_memory shm1(create_only, ShmName, ShmSize, read_write, 0, permissions()); |
95 | |
96 | //Overwrite all memory |
97 | std::memset(s: shm1.get_user_address(), c: 0, n: shm1.get_user_size()); |
98 | |
99 | //Now test move semantics |
100 | shared_memory move_ctor(boost::move(t&: shm1)); |
101 | shared_memory move_assign; |
102 | move_assign = boost::move(t&: move_ctor); |
103 | } |
104 | } |
105 | BOOST_CATCH(std::exception &ex){ |
106 | shared_memory_object::remove(filename: ShmName); |
107 | std::cout << ex.what() << std::endl; |
108 | return 1; |
109 | } BOOST_CATCH_END |
110 | shared_memory_object::remove(filename: ShmName); |
111 | return 0; |
112 | } |
113 | |