managed_windows_shared_memory_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <boost/interprocess/allocators/allocator.hpp>
  14. #include <boost/interprocess/containers/vector.hpp>
  15. #include <boost/interprocess/managed_windows_shared_memory.hpp>
  16. #include <cstdio>
  17. #include <string>
  18. #include "get_process_id_name.hpp"
  19. using namespace boost::interprocess;
  20. int main ()
  21. {
  22. const int MemSize = 65536;
  23. const char *const MemName = test::get_process_id_name();
  24. //STL compatible allocator object for shared memory
  25. typedef allocator<int, managed_windows_shared_memory::segment_manager>
  26. allocator_int_t;
  27. //A vector that uses that allocator
  28. typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
  29. {
  30. const int max = 100;
  31. void *array[max];
  32. //Named allocate capable shared memory allocator
  33. managed_windows_shared_memory w_shm(create_only, MemName, MemSize);
  34. int i;
  35. //Let's allocate some memory
  36. for(i = 0; i < max; ++i){
  37. array[i] = w_shm.allocate(i+1);
  38. }
  39. //Deallocate allocated memory
  40. for(i = 0; i < max; ++i){
  41. w_shm.deallocate(array[i]);
  42. }
  43. }
  44. {
  45. //Named allocate capable shared memory managed memory class
  46. managed_windows_shared_memory w_shm(create_only, MemName, MemSize);
  47. //Construct the STL-like allocator with the segment manager
  48. const allocator_int_t myallocator (w_shm.get_segment_manager());
  49. //Construct vector
  50. MyVect *w_shm_vect = w_shm.construct<MyVect> ("MyVector") (myallocator);
  51. //Test that vector can be found via name
  52. if(w_shm_vect != w_shm.find<MyVect>("MyVector").first)
  53. return -1;
  54. //Destroy and check it is not present
  55. w_shm.destroy<MyVect> ("MyVector");
  56. if(0 != w_shm.find<MyVect>("MyVector").first)
  57. return -1;
  58. //Construct a vector in the shared memory
  59. w_shm_vect = w_shm.construct<MyVect> ("MyVector") (myallocator);
  60. {
  61. //Map preexisting segment again in memory
  62. managed_windows_shared_memory w_shm_new(open_only, MemName);
  63. //Check vector is still there
  64. w_shm_vect = w_shm_new.find<MyVect>("MyVector").first;
  65. if(!w_shm_vect)
  66. return -1;
  67. if(w_shm_new.get_size() != w_shm.get_size())
  68. return 1;
  69. {
  70. {
  71. //Map preexisting shmem again in copy-on-write
  72. managed_windows_shared_memory shmem(open_copy_on_write, MemName);
  73. //Check vector is still there
  74. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  75. if(!shmem_vect)
  76. return -1;
  77. //Erase vector
  78. shmem.destroy_ptr(shmem_vect);
  79. //Make sure vector is erased
  80. shmem_vect = shmem.find<MyVect>("MyVector").first;
  81. if(shmem_vect)
  82. return -1;
  83. }
  84. //Now check vector is still in the s
  85. {
  86. //Map preexisting shmem again in copy-on-write
  87. managed_windows_shared_memory shmem(open_copy_on_write, MemName);
  88. //Check vector is still there
  89. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  90. if(!shmem_vect)
  91. return -1;
  92. }
  93. }
  94. {
  95. //Map preexisting shmem again in copy-on-write
  96. managed_windows_shared_memory shmem(open_read_only, MemName);
  97. //Check vector is still there
  98. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  99. if(!shmem_vect)
  100. return -1;
  101. }
  102. //Destroy and check it is not present
  103. w_shm_new.destroy_ptr(w_shm_vect);
  104. if(0 != w_shm_new.find<MyVect>("MyVector").first)
  105. return 1;
  106. //Now test move semantics
  107. managed_windows_shared_memory original(open_only, MemName);
  108. managed_windows_shared_memory move_ctor(boost::move(original));
  109. managed_windows_shared_memory move_assign;
  110. move_assign = boost::move(move_ctor);
  111. }
  112. }
  113. return 0;
  114. }
  115. #else
  116. int main()
  117. {
  118. return 0;
  119. }
  120. #endif
  121. #include <boost/interprocess/detail/config_end.hpp>