windows_shared_memory_mapping_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifdef BOOST_INTERPROCESS_WINDOWS
  13. #include <fstream>
  14. #include <iostream>
  15. #include <boost/interprocess/windows_shared_memory.hpp>
  16. #include <boost/interprocess/mapped_region.hpp>
  17. #include <string>
  18. #include "get_process_id_name.hpp"
  19. using namespace boost::interprocess;
  20. int main ()
  21. {
  22. try{
  23. const char *names[2] = { test::get_process_id_name(), 0 };
  24. for(unsigned int i_name = 0; i_name < sizeof(names)/sizeof(names[0]); ++i_name)
  25. {
  26. const std::size_t FileSize = 99999*2;
  27. //Create a file mapping
  28. windows_shared_memory mapping
  29. (create_only, names[i_name], read_write, FileSize);
  30. if(static_cast<std::size_t>(mapping.get_size()) < FileSize)
  31. return 1;
  32. {
  33. //Create two mapped regions, one half of the file each
  34. mapped_region region (mapping
  35. ,read_write
  36. ,0
  37. ,FileSize/2
  38. ,0);
  39. mapped_region region2(mapping
  40. ,read_write
  41. ,FileSize/2
  42. ,FileSize - FileSize/2
  43. ,0);
  44. //Fill two regions with a pattern
  45. unsigned char *filler = static_cast<unsigned char*>(region.get_address());
  46. for(std::size_t i = 0
  47. ;i < FileSize/2
  48. ;++i){
  49. *filler++ = static_cast<unsigned char>(i);
  50. }
  51. filler = static_cast<unsigned char*>(region2.get_address());
  52. for(std::size_t i = FileSize/2
  53. ;i < FileSize
  54. ;++i){
  55. *filler++ = static_cast<unsigned char>(i);
  56. }
  57. if(!region.flush(0, 0, false)){
  58. return 1;
  59. }
  60. if(!region2.flush(0, 0, true)){
  61. return 1;
  62. }
  63. }
  64. //See if the pattern is correct in the file using two mapped regions
  65. {
  66. mapped_region region (mapping, read_only, 0, FileSize/2, 0);
  67. mapped_region region2(mapping, read_only, FileSize/2, FileSize - FileSize/2, 0);
  68. unsigned char *checker = static_cast<unsigned char*>(region.get_address());
  69. //Check pattern
  70. for(std::size_t i = 0
  71. ;i < FileSize/2
  72. ;++i){
  73. if(*checker++ != static_cast<unsigned char>(i)){
  74. return 1;
  75. }
  76. }
  77. //Check second half
  78. checker = static_cast<unsigned char *>(region2.get_address());
  79. //Check pattern
  80. for(std::size_t i = FileSize/2
  81. ;i < FileSize
  82. ;++i){
  83. if(*checker++ != static_cast<unsigned char>(i)){
  84. return 1;
  85. }
  86. }
  87. }
  88. //Now check the pattern mapping a single read only mapped_region
  89. {
  90. //Create a single regions, mapping all the file
  91. mapped_region region (mapping, read_only);
  92. //Check pattern
  93. unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
  94. for(std::size_t i = 0
  95. ;i < FileSize
  96. ;++i, ++pattern){
  97. if(*pattern != static_cast<unsigned char>(i)){
  98. return 1;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. catch(std::exception &exc){
  105. //shared_memory_object::remove(test::get_process_id_name());
  106. std::cout << "Unhandled exception: " << exc.what() << std::endl;
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. #else
  112. int main()
  113. {
  114. return 0;
  115. }
  116. #endif
  117. #include <boost/interprocess/detail/config_end.hpp>