]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_managed_copy_on_write.cpp
1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-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)
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 const char *ManagedFile
= 0;
29 const char *ManagedFile2
= 0;
30 std::string
managed_file_name(boost::interprocess::ipcdetail::get_temporary_path());
31 managed_file_name
+= "/"; managed_file_name
+= test::get_process_id_name();
32 ManagedFile
= managed_file_name
.c_str();
33 std::string
managed_file2_name(boost::interprocess::ipcdetail::get_temporary_path());
34 managed_file2_name
+= "/"; managed_file2_name
+= test::get_process_id_name(); managed_file2_name
+= "_2";
35 ManagedFile2
= managed_file2_name
.c_str();
38 const char *ManagedFile
= "MyManagedFile";
39 const char *ManagedFile2
= "MyManagedFile2";
44 //Try to erase any previous managed segment with the same name
45 file_mapping::remove(ManagedFile
);
46 file_mapping::remove(ManagedFile2
);
47 remove_file_on_destroy
destroyer1(ManagedFile
);
48 remove_file_on_destroy
destroyer2(ManagedFile2
);
51 //Create an named integer in a managed mapped file
52 managed_mapped_file
managed_file(create_only
, ManagedFile
, 65536);
53 managed_file
.construct
<int>("MyInt")(0u);
55 //Now create a copy on write version
56 managed_mapped_file
managed_file_cow(open_copy_on_write
, ManagedFile
);
58 //Erase the int and create a new one
59 if(!managed_file_cow
.destroy
<int>("MyInt"))
61 managed_file_cow
.construct
<int>("MyInt2");
64 if(managed_file_cow
.find
<int>("MyInt").first
&& !managed_file_cow
.find
<int>("MyInt2").first
)
67 //Check the original is intact
68 if(!managed_file
.find
<int>("MyInt").first
&& managed_file
.find
<int>("MyInt2").first
)
71 { //Dump the modified copy on write segment to a file
72 std::fstream
file(ManagedFile2
, std::ios_base::out
| std::ios_base::binary
);
75 file
.write(static_cast<const char *>(managed_file_cow
.get_address()), (std::streamsize
)managed_file_cow
.get_size());
78 //Now open the modified file and test changes
79 managed_mapped_file
managed_file_cow2(open_only
, ManagedFile2
);
80 if(managed_file_cow2
.find
<int>("MyInt").first
&& !managed_file_cow2
.find
<int>("MyInt2").first
)
84 //Now create a read-only version
85 managed_mapped_file
managed_file_ro(open_read_only
, ManagedFile
);
87 //Check the original is intact
88 if(!managed_file_ro
.find
<int>("MyInt").first
&& managed_file_ro
.find
<int>("MyInt2").first
)
91 //Check the number of named objects using the iterators
92 if(std::distance(managed_file_ro
.named_begin(), managed_file_ro
.named_end()) != 1 &&
93 std::distance(managed_file_ro
.unique_begin(), managed_file_ro
.unique_end()) != 0 )
99 #include <boost/interprocess/detail/config_end.hpp>