xsi_shared_memory_mapping_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/detail/workaround.hpp>
  12. #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
  13. #include <fstream>
  14. #include <iostream>
  15. #include <boost/interprocess/xsi_shared_memory.hpp>
  16. #include <boost/interprocess/mapped_region.hpp>
  17. #include <boost/interprocess/file_mapping.hpp>
  18. #include <boost/interprocess/detail/file_wrapper.hpp>
  19. #include <string>
  20. #include <iostream>
  21. #include "get_process_id_name.hpp"
  22. using namespace boost::interprocess;
  23. void remove_shared_memory(const xsi_key &key)
  24. {
  25. try{
  26. xsi_shared_memory xsi(open_only, key);
  27. xsi_shared_memory::remove(xsi.get_shmid());
  28. }
  29. catch(interprocess_exception &e){
  30. if(e.get_error_code() != not_found_error)
  31. throw;
  32. }
  33. }
  34. class xsi_shared_memory_remover
  35. {
  36. public:
  37. xsi_shared_memory_remover(xsi_shared_memory &xsi_shm)
  38. : xsi_shm_(xsi_shm)
  39. {}
  40. ~xsi_shared_memory_remover()
  41. { xsi_shared_memory::remove(xsi_shm_.get_shmid()); }
  42. private:
  43. xsi_shared_memory & xsi_shm_;
  44. };
  45. inline std::string get_filename()
  46. {
  47. std::string ret (ipcdetail::get_temporary_path());
  48. ret += "/";
  49. ret += test::get_process_id_name();
  50. return ret;
  51. }
  52. int main ()
  53. {
  54. std::string filename(get_filename());
  55. const char *names[2] = { filename.c_str(), 0 };
  56. file_mapping::remove(names[0]);
  57. { ipcdetail::file_wrapper(create_only, names[0], read_write); }
  58. xsi_key key(names[0], 1);
  59. file_mapping::remove(names[0]);
  60. remove_shared_memory(key);
  61. unsigned int i;
  62. try{
  63. for(i = 0; i < sizeof(names)/sizeof(names[0]); ++i)
  64. {
  65. const std::size_t FileSize = 99999*2;
  66. //Create a file mapping
  67. xsi_shared_memory mapping (create_only, names[i] ? key : xsi_key(), FileSize);
  68. xsi_shared_memory_remover rem(mapping);
  69. try{
  70. {
  71. //Partial mapping should fail fox XSI shared memory
  72. bool thrown = false;
  73. try{
  74. mapped_region region2(mapping, read_write, FileSize/2, FileSize - FileSize/2, 0);
  75. }
  76. catch(...){
  77. thrown = true;
  78. }
  79. if(thrown == false){
  80. return 1;
  81. }
  82. //Create a mapped region
  83. mapped_region region (mapping, read_write, 0, FileSize, 0);
  84. //Fill two regions with a pattern
  85. unsigned char *filler = static_cast<unsigned char*>(region.get_address());
  86. for(std::size_t i = 0; i < FileSize; ++i){
  87. *filler++ = static_cast<unsigned char>(i);
  88. }
  89. }
  90. //Now check the pattern mapping a single read only mapped_region
  91. {
  92. //Create a single region, mapping all the file
  93. mapped_region region (mapping, read_only);
  94. //Check pattern
  95. unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
  96. for(std::size_t i = 0; i < FileSize; ++i, ++pattern){
  97. if(*pattern != static_cast<unsigned char>(i)){
  98. return 1;
  99. }
  100. }
  101. }
  102. }
  103. catch(std::exception &exc){
  104. std::cout << "Unhandled exception: " << exc.what() << std::endl;
  105. return 1;
  106. }
  107. }
  108. }
  109. catch(std::exception &exc){
  110. std::cout << "Unhandled exception: " << exc.what() << std::endl;
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. #else
  116. int main()
  117. {
  118. return 0;
  119. }
  120. #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  121. #include <boost/interprocess/detail/config_end.hpp>