managed_xsi_shared_memory_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-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 <boost/interprocess/detail/config_begin.hpp>
  14. #include <boost/interprocess/allocators/allocator.hpp>
  15. #include <boost/interprocess/containers/vector.hpp>
  16. #include <boost/interprocess/managed_xsi_shared_memory.hpp>
  17. #include <boost/interprocess/detail/file_wrapper.hpp>
  18. #include <boost/interprocess/file_mapping.hpp>
  19. #include <cstdio>
  20. #include <string>
  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. const int ShmemSize = 65536;
  55. std::string filename = get_filename();
  56. const char *const ShmemName = filename.c_str();
  57. file_mapping::remove(ShmemName);
  58. { ipcdetail::file_wrapper(create_only, ShmemName, read_write); }
  59. xsi_key key(ShmemName, 1);
  60. file_mapping::remove(ShmemName);
  61. int shmid;
  62. //STL compatible allocator object for memory-mapped shmem
  63. typedef allocator<int, managed_xsi_shared_memory::segment_manager>
  64. allocator_int_t;
  65. //A vector that uses that allocator
  66. typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
  67. {
  68. //Remove the shmem it is already created
  69. remove_shared_memory(key);
  70. const int max = 100;
  71. void *array[max];
  72. //Named allocate capable shared memory allocator
  73. managed_xsi_shared_memory shmem(create_only, key, ShmemSize);
  74. shmid = shmem.get_shmid();
  75. int i;
  76. //Let's allocate some memory
  77. for(i = 0; i < max; ++i){
  78. array[i] = shmem.allocate(i+1);
  79. }
  80. //Deallocate allocated memory
  81. for(i = 0; i < max; ++i){
  82. shmem.deallocate(array[i]);
  83. }
  84. }
  85. {
  86. //Remove the shmem it is already created
  87. xsi_shared_memory::remove(shmid);
  88. //Named allocate capable memory mapped shmem managed memory class
  89. managed_xsi_shared_memory shmem(create_only, key, ShmemSize);
  90. shmid = shmem.get_shmid();
  91. //Construct the STL-like allocator with the segment manager
  92. const allocator_int_t myallocator (shmem.get_segment_manager());
  93. //Construct vector
  94. MyVect *shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
  95. //Test that vector can be found via name
  96. if(shmem_vect != shmem.find<MyVect>("MyVector").first)
  97. return -1;
  98. //Destroy and check it is not present
  99. shmem.destroy<MyVect> ("MyVector");
  100. if(0 != shmem.find<MyVect>("MyVector").first)
  101. return -1;
  102. //Construct a vector in the memory-mapped shmem
  103. shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
  104. }
  105. {
  106. //Map preexisting shmem again in memory
  107. managed_xsi_shared_memory shmem(open_only, key);
  108. shmid = shmem.get_shmid();
  109. //Check vector is still there
  110. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  111. if(!shmem_vect)
  112. return -1;
  113. }
  114. {
  115. //Now destroy the vector
  116. {
  117. //Map preexisting shmem again in memory
  118. managed_xsi_shared_memory shmem(open_only, key);
  119. shmid = shmem.get_shmid();
  120. //Destroy and check it is not present
  121. shmem.destroy<MyVect>("MyVector");
  122. if(0 != shmem.find<MyVect>("MyVector").first)
  123. return -1;
  124. }
  125. {
  126. //Now test move semantics
  127. managed_xsi_shared_memory original(open_only, key);
  128. managed_xsi_shared_memory move_ctor(boost::move(original));
  129. managed_xsi_shared_memory move_assign;
  130. move_assign = boost::move(move_ctor);
  131. move_assign.swap(original);
  132. }
  133. }
  134. xsi_shared_memory::remove(shmid);
  135. return 0;
  136. }
  137. #else
  138. int main()
  139. {
  140. return 0;
  141. }
  142. #endif //#ifndef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  143. #include <boost/interprocess/detail/config_end.hpp>