1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. 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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/os_file_functions.hpp>
12 //[doc_managed_copy_on_write
13 #include <boost/interprocess/managed_mapped_file.hpp>
14 #include <fstream> //std::fstream
15 #include <iterator>//std::distance
18 #include "../test/get_process_id_name.hpp"
23 using namespace boost::interprocess;
28 std::string managed_file(boost::interprocess::detail::get_temporary_path());
29 managed_file += "/"; managed_file += test::get_process_id_name();
30 const char *ManagedFile = managed_file.c_str();
31 std::string managed_file2(boost::interprocess::detail::get_temporary_path());
32 managed_file2 += "/"; managed_file2 += test::get_process_id_name(); managed_file2 += "_2";
33 const char *ManagedFile2 = managed_file2.c_str();
36 const char *ManagedFile = "MyManagedFile";
37 const char *ManagedFile2 = "MyManagedFile2";
42 //Try to erase any previous managed segment with the same name
43 file_mapping::remove(ManagedFile);
44 file_mapping::remove(ManagedFile2);
45 remove_file_on_destroy destroyer1(ManagedFile);
46 remove_file_on_destroy destroyer2(ManagedFile2);
49 //Create an named integer in a managed mapped file
50 managed_mapped_file managed_file(create_only, ManagedFile, 65536);
51 managed_file.construct<int>("MyInt")(0u);
53 //Now create a copy on write version
54 managed_mapped_file managed_file_cow(open_copy_on_write, ManagedFile);
56 //Erase the int and create a new one
57 if(!managed_file_cow.destroy<int>("MyInt"))
59 managed_file_cow.construct<int>("MyInt2");
62 if(managed_file_cow.find<int>("MyInt").first && !managed_file_cow.find<int>("MyInt2").first)
65 //Check the original is intact
66 if(!managed_file.find<int>("MyInt").first && managed_file.find<int>("MyInt2").first)
69 { //Dump the modified copy on write segment to a file
70 std::fstream file(ManagedFile2, std::ios_base::out | std::ios_base::binary);
73 file.write(static_cast<const char *>(managed_file_cow.get_address()), managed_file_cow.get_size());
76 //Now open the modified file and test changes
77 managed_mapped_file managed_file_cow2(open_only, ManagedFile2);
78 if(managed_file_cow2.find<int>("MyInt").first && !managed_file_cow2.find<int>("MyInt2").first)
82 //Now create a read-only version
83 managed_mapped_file managed_file_ro(open_read_only, ManagedFile);
85 //Check the original is intact
86 if(!managed_file_ro.find<int>("MyInt").first && managed_file_ro.find<int>("MyInt2").first)
89 //Check the number of named objects using the iterators
90 if(std::distance(managed_file_ro.named_begin(), managed_file_ro.named_end()) != 1 &&
91 std::distance(managed_file_ro.unique_begin(), managed_file_ro.unique_end()) != 0 )
97 #include <boost/interprocess/detail/config_end.hpp>