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