doc_managed_copy_on_write.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  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)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  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
  16. //<-
  17. #include "../test/get_process_id_name.hpp"
  18. //->
  19. int main()
  20. {
  21. using namespace boost::interprocess;
  22. //Define file names
  23. //<-
  24. #if 1
  25. const char *ManagedFile = 0;
  26. const char *ManagedFile2 = 0;
  27. std::string managed_file_name(boost::interprocess::ipcdetail::get_temporary_path());
  28. managed_file_name += "/"; managed_file_name += test::get_process_id_name();
  29. ManagedFile = managed_file_name.c_str();
  30. std::string managed_file2_name(boost::interprocess::ipcdetail::get_temporary_path());
  31. managed_file2_name += "/"; managed_file2_name += test::get_process_id_name(); managed_file2_name += "_2";
  32. ManagedFile2 = managed_file2_name.c_str();
  33. #else
  34. //->
  35. const char *ManagedFile = "MyManagedFile";
  36. const char *ManagedFile2 = "MyManagedFile2";
  37. //<-
  38. #endif
  39. //->
  40. //Try to erase any previous managed segment with the same name
  41. file_mapping::remove(ManagedFile);
  42. file_mapping::remove(ManagedFile2);
  43. remove_file_on_destroy destroyer1(ManagedFile);
  44. remove_file_on_destroy destroyer2(ManagedFile2);
  45. {
  46. //Create an named integer in a managed mapped file
  47. managed_mapped_file managed_file(create_only, ManagedFile, 65536);
  48. managed_file.construct<int>("MyInt")(0u);
  49. //Now create a copy on write version
  50. managed_mapped_file managed_file_cow(open_copy_on_write, ManagedFile);
  51. //Erase the int and create a new one
  52. if(!managed_file_cow.destroy<int>("MyInt"))
  53. throw int(0);
  54. managed_file_cow.construct<int>("MyInt2");
  55. //Check changes
  56. if(managed_file_cow.find<int>("MyInt").first && !managed_file_cow.find<int>("MyInt2").first)
  57. throw int(0);
  58. //Check the original is intact
  59. if(!managed_file.find<int>("MyInt").first && managed_file.find<int>("MyInt2").first)
  60. throw int(0);
  61. { //Dump the modified copy on write segment to a file
  62. std::fstream file(ManagedFile2, std::ios_base::out | std::ios_base::binary);
  63. if(!file)
  64. throw int(0);
  65. file.write(static_cast<const char *>(managed_file_cow.get_address()), (std::streamsize)managed_file_cow.get_size());
  66. }
  67. //Now open the modified file and test changes
  68. managed_mapped_file managed_file_cow2(open_only, ManagedFile2);
  69. if(managed_file_cow2.find<int>("MyInt").first && !managed_file_cow2.find<int>("MyInt2").first)
  70. throw int(0);
  71. }
  72. {
  73. //Now create a read-only version
  74. managed_mapped_file managed_file_ro(open_read_only, ManagedFile);
  75. //Check the original is intact
  76. if(!managed_file_ro.find<int>("MyInt").first && managed_file_ro.find<int>("MyInt2").first)
  77. throw int(0);
  78. //Check the number of named objects using the iterators
  79. if(std::distance(managed_file_ro.named_begin(), managed_file_ro.named_end()) != 1 &&
  80. std::distance(managed_file_ro.unique_begin(), managed_file_ro.unique_end()) != 0 )
  81. throw int(0);
  82. }
  83. return 0;
  84. }
  85. //]
  86. #include <boost/interprocess/detail/config_end.hpp>