mirror of
https://github.com/boostorg/interprocess.git
synced 2026-01-19 04:12:13 +00:00
Added XSI shared memory
[SVN r67451]
This commit is contained in:
@@ -8,28 +8,50 @@
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#include <boost/interprocess/detail/config_begin.hpp>
|
||||
#include <boost/interprocess/detail/os_file_functions.hpp>
|
||||
//[doc_managed_copy_on_write
|
||||
#include <boost/interprocess/managed_mapped_file.hpp>
|
||||
#include <fstream> //std::fstream
|
||||
#include <iterator>//std::distance
|
||||
|
||||
//<-
|
||||
#include "../test/get_process_id_name.hpp"
|
||||
//->
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost::interprocess;
|
||||
|
||||
//Define file names
|
||||
//<-
|
||||
#if 1
|
||||
std::string managed_file(boost::interprocess::detail::get_temporary_path());
|
||||
managed_file += "/"; managed_file += test::get_process_id_name();
|
||||
const char *ManagedFile = managed_file.c_str();
|
||||
std::string managed_file2(boost::interprocess::detail::get_temporary_path());
|
||||
managed_file2 += "/"; managed_file2 += test::get_process_id_name(); managed_file2 += "_2";
|
||||
const char *ManagedFile2 = managed_file2.c_str();
|
||||
#else
|
||||
//->
|
||||
const char *ManagedFile = "MyManagedFile";
|
||||
const char *ManagedFile2 = "MyManagedFile2";
|
||||
//<-
|
||||
#endif
|
||||
//->
|
||||
|
||||
//Try to erase any previous managed segment with the same name
|
||||
file_mapping::remove("MyManagedFile");
|
||||
file_mapping::remove("MyManagedFile2");
|
||||
remove_file_on_destroy destroyer1("MyManagedFile");
|
||||
remove_file_on_destroy destroyer2("MyManagedFile2");
|
||||
file_mapping::remove(ManagedFile);
|
||||
file_mapping::remove(ManagedFile2);
|
||||
remove_file_on_destroy destroyer1(ManagedFile);
|
||||
remove_file_on_destroy destroyer2(ManagedFile2);
|
||||
|
||||
{
|
||||
//Create an named integer in a managed mapped file
|
||||
managed_mapped_file managed_file(create_only, "MyManagedFile", 65536);
|
||||
managed_mapped_file managed_file(create_only, ManagedFile, 65536);
|
||||
managed_file.construct<int>("MyInt")(0u);
|
||||
|
||||
//Now create a copy on write version
|
||||
managed_mapped_file managed_file_cow(open_copy_on_write, "MyManagedFile");
|
||||
managed_mapped_file managed_file_cow(open_copy_on_write, ManagedFile);
|
||||
|
||||
//Erase the int and create a new one
|
||||
if(!managed_file_cow.destroy<int>("MyInt"))
|
||||
@@ -45,20 +67,20 @@ int main()
|
||||
throw int(0);
|
||||
|
||||
{ //Dump the modified copy on write segment to a file
|
||||
std::fstream file("MyManagedFile2", std::ios_base::out | std::ios_base::binary);
|
||||
std::fstream file(ManagedFile2, std::ios_base::out | std::ios_base::binary);
|
||||
if(!file)
|
||||
throw int(0);
|
||||
file.write(static_cast<const char *>(managed_file_cow.get_address()), managed_file_cow.get_size());
|
||||
}
|
||||
|
||||
//Now open the modified file and test changes
|
||||
managed_mapped_file managed_file_cow2(open_only, "MyManagedFile2");
|
||||
managed_mapped_file managed_file_cow2(open_only, ManagedFile2);
|
||||
if(managed_file_cow2.find<int>("MyInt").first && !managed_file_cow2.find<int>("MyInt2").first)
|
||||
throw int(0);
|
||||
}
|
||||
{
|
||||
//Now create a read-only version
|
||||
managed_mapped_file managed_file_ro(open_read_only, "MyManagedFile");
|
||||
managed_mapped_file managed_file_ro(open_read_only, ManagedFile);
|
||||
|
||||
//Check the original is intact
|
||||
if(!managed_file_ro.find<int>("MyInt").first && managed_file_ro.find<int>("MyInt2").first)
|
||||
|
||||
Reference in New Issue
Block a user