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 #include <boost/interprocess/detail/workaround.hpp>
11
12 #if defined(BOOST_INTERPROCESS_MAPPED_FILES)
13
14 #include <boost/interprocess/allocators/allocator.hpp>
15 #include <boost/interprocess/containers/vector.hpp>
16 #include <boost/interprocess/detail/file_wrapper.hpp>
17 #include <boost/interprocess/file_mapping.hpp>
18 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
19 #include "named_creation_template.hpp"
20 #include <cstdio>
21 #include <cstring>
22 #include <string>
23 #include <boost/interprocess/detail/os_file_functions.hpp>
24 #include "get_process_id_name.hpp"
25
26 using namespace boost::interprocess;
27
28 static const std::size_t FileSize = 1000;
get_filename()29 inline std::string get_filename()
30 {
31 std::string ret (ipcdetail::get_temporary_path());
32 ret += "/";
33 ret += test::get_process_id_name();
34 return ret;
35 }
36
37 struct file_destroyer
38 {
~file_destroyerfile_destroyer39 ~file_destroyer()
40 {
41 //The last destructor will destroy the file
42 file_mapping::remove(get_filename().c_str());
43 }
44 };
45
46 //This wrapper is necessary to have a common constructor
47 //in generic named_creation_template functions
48 class mapped_file_creation_test_wrapper
49 : public file_destroyer
50 , public boost::interprocess::ipcdetail::managed_open_or_create_impl
51 <boost::interprocess::ipcdetail::file_wrapper, 0, true, false>
52 {
53 typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
54 <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
55 public:
mapped_file_creation_test_wrapper(boost::interprocess::create_only_t)56 mapped_file_creation_test_wrapper(boost::interprocess::create_only_t)
57 : mapped_file(boost::interprocess::create_only, get_filename().c_str(), FileSize, read_write, 0, permissions())
58 {}
59
mapped_file_creation_test_wrapper(boost::interprocess::open_only_t)60 mapped_file_creation_test_wrapper(boost::interprocess::open_only_t)
61 : mapped_file(boost::interprocess::open_only, get_filename().c_str(), read_write, 0)
62 {}
63
mapped_file_creation_test_wrapper(boost::interprocess::open_or_create_t)64 mapped_file_creation_test_wrapper(boost::interprocess::open_or_create_t)
65 : mapped_file(boost::interprocess::open_or_create, get_filename().c_str(), FileSize, read_write, 0, permissions())
66 {}
67 };
68
main()69 int main ()
70 {
71 typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
72 <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
73 file_mapping::remove(get_filename().c_str());
74 test::test_named_creation<mapped_file_creation_test_wrapper>();
75
76 //Create and get name, size and address
77 {
78 mapped_file file1(create_only, get_filename().c_str(), FileSize, read_write, 0, permissions());
79
80 //Overwrite all memory
81 std::memset(file1.get_user_address(), 0, file1.get_user_size());
82
83 //Now test move semantics
84 mapped_file move_ctor(boost::move(file1));
85 mapped_file move_assign;
86 move_assign = boost::move(move_ctor);
87 }
88 // file_mapping::remove(get_filename().c_str());
89 return 0;
90 }
91
92 #else //#if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
93
main()94 int main()
95 {
96 return 0;
97 }
98
99 #endif//#if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
100