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
25 struct eraser
26 {
~erasereraser27 ~eraser()
28 {
29 shared_memory_object::remove(ShmName);
30 }
31 };
32
33 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> shared_memory;
34
35 //This wrapper is necessary to have a common constructor
36 //in generic named_creation_template functions
37 class shared_memory_creation_test_wrapper
38 : public eraser
39 , public shared_memory
40 {
41
42 public:
shared_memory_creation_test_wrapper(create_only_t)43 shared_memory_creation_test_wrapper(create_only_t)
44 : shared_memory(create_only, ShmName, ShmSize, read_write, 0, permissions())
45 {}
46
shared_memory_creation_test_wrapper(open_only_t)47 shared_memory_creation_test_wrapper(open_only_t)
48 : shared_memory(open_only, ShmName, read_write, 0)
49 {}
50
shared_memory_creation_test_wrapper(open_or_create_t)51 shared_memory_creation_test_wrapper(open_or_create_t)
52 : shared_memory(open_or_create, ShmName, ShmSize, read_write, 0, permissions())
53 {}
54 };
55
56
main()57 int main ()
58 {
59 try{
60 shared_memory_object::remove(ShmName);
61 test::test_named_creation<shared_memory_creation_test_wrapper>();
62
63 //Create and get name, size and address
64 {
65 shared_memory_object::remove(ShmName);
66 shared_memory shm1(create_only, ShmName, ShmSize, read_write, 0, permissions());
67
68 //Overwrite all memory
69 std::memset(shm1.get_user_address(), 0, shm1.get_user_size());
70
71 //Now test move semantics
72 shared_memory move_ctor(boost::move(shm1));
73 shared_memory move_assign;
74 move_assign = boost::move(move_ctor);
75 }
76 }
77 catch(std::exception &ex){
78 shared_memory_object::remove(ShmName);
79 std::cout << ex.what() << std::endl;
80 return 1;
81 }
82 shared_memory_object::remove(ShmName);
83 return 0;
84 }
85