managed_shared_memory_test.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/allocators/allocator.hpp>
  12. #include <boost/interprocess/containers/vector.hpp>
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <cstdio>
  15. #include <string>
  16. #include "get_process_id_name.hpp"
  17. using namespace boost::interprocess;
  18. int main ()
  19. {
  20. const int ShmemSize = 65536;
  21. const char *const ShmemName = test::get_process_id_name();
  22. //STL compatible allocator object for memory-mapped shmem
  23. typedef allocator<int, managed_shared_memory::segment_manager>
  24. allocator_int_t;
  25. //A vector that uses that allocator
  26. typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
  27. {
  28. //Remove the shmem it is already created
  29. shared_memory_object::remove(ShmemName);
  30. const int max = 100;
  31. void *array[max];
  32. //Named allocate capable shared memory allocator
  33. managed_shared_memory shmem(create_only, ShmemName, ShmemSize);
  34. int i;
  35. //Let's allocate some memory
  36. for(i = 0; i < max; ++i){
  37. array[i] = shmem.allocate(i+1);
  38. }
  39. //Deallocate allocated memory
  40. for(i = 0; i < max; ++i){
  41. shmem.deallocate(array[i]);
  42. }
  43. }
  44. {
  45. //Remove the shmem it is already created
  46. shared_memory_object::remove(ShmemName);
  47. //Named allocate capable memory mapped shmem managed memory class
  48. managed_shared_memory shmem(create_only, ShmemName, ShmemSize);
  49. //Construct the STL-like allocator with the segment manager
  50. const allocator_int_t myallocator (shmem.get_segment_manager());
  51. //Construct vector
  52. MyVect *shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
  53. //Test that vector can be found via name
  54. if(shmem_vect != shmem.find<MyVect>("MyVector").first)
  55. return -1;
  56. //Destroy and check it is not present
  57. shmem.destroy<MyVect> ("MyVector");
  58. if(0 != shmem.find<MyVect>("MyVector").first)
  59. return -1;
  60. //Construct a vector in the memory-mapped shmem
  61. shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
  62. }
  63. {
  64. //Map preexisting shmem again in memory
  65. managed_shared_memory shmem(open_only, ShmemName);
  66. //Check vector is still there
  67. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  68. if(!shmem_vect)
  69. return -1;
  70. }
  71. {
  72. {
  73. //Map preexisting shmem again in copy-on-write
  74. managed_shared_memory shmem(open_copy_on_write, ShmemName);
  75. //Check vector is still there
  76. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  77. if(!shmem_vect)
  78. return -1;
  79. //Erase vector
  80. shmem.destroy_ptr(shmem_vect);
  81. //Make sure vector is erased
  82. shmem_vect = shmem.find<MyVect>("MyVector").first;
  83. if(shmem_vect)
  84. return -1;
  85. }
  86. //Now check vector is still in the shmem
  87. {
  88. //Map preexisting shmem again in copy-on-write
  89. managed_shared_memory shmem(open_copy_on_write, ShmemName);
  90. //Check vector is still there
  91. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  92. if(!shmem_vect)
  93. return -1;
  94. }
  95. }
  96. {
  97. //Map preexisting shmem again in copy-on-write
  98. managed_shared_memory shmem(open_read_only, ShmemName);
  99. //Check vector is still there
  100. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  101. if(!shmem_vect)
  102. return -1;
  103. }
  104. #ifndef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS_NO_GROW
  105. {
  106. managed_shared_memory::size_type old_free_memory;
  107. {
  108. //Map preexisting shmem again in memory
  109. managed_shared_memory shmem(open_only, ShmemName);
  110. old_free_memory = shmem.get_free_memory();
  111. }
  112. //Now grow the shmem
  113. managed_shared_memory::grow(ShmemName, ShmemSize);
  114. //Map preexisting shmem again in memory
  115. managed_shared_memory shmem(open_only, ShmemName);
  116. //Check vector is still there
  117. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  118. if(!shmem_vect)
  119. return -1;
  120. if(shmem.get_size() != (ShmemSize*2))
  121. return -1;
  122. if(shmem.get_free_memory() <= old_free_memory)
  123. return -1;
  124. }
  125. {
  126. managed_shared_memory::size_type old_free_memory, next_free_memory,
  127. old_shmem_size, next_shmem_size, final_shmem_size;
  128. {
  129. //Map preexisting shmem again in memory
  130. managed_shared_memory shmem(open_only, ShmemName);
  131. old_free_memory = shmem.get_free_memory();
  132. old_shmem_size = shmem.get_size();
  133. }
  134. //Now shrink the shmem
  135. managed_shared_memory::shrink_to_fit(ShmemName);
  136. {
  137. //Map preexisting shmem again in memory
  138. managed_shared_memory shmem(open_only, ShmemName);
  139. next_shmem_size = shmem.get_size();
  140. //Check vector is still there
  141. MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
  142. if(!shmem_vect)
  143. return -1;
  144. next_free_memory = shmem.get_free_memory();
  145. if(next_free_memory >= old_free_memory)
  146. return -1;
  147. if(old_shmem_size <= next_shmem_size)
  148. return -1;
  149. }
  150. //Now destroy the vector
  151. {
  152. //Map preexisting shmem again in memory
  153. managed_shared_memory shmem(open_only, ShmemName);
  154. //Destroy and check it is not present
  155. shmem.destroy<MyVect>("MyVector");
  156. if(0 != shmem.find<MyVect>("MyVector").first)
  157. return -1;
  158. }
  159. //Now shrink the shmem
  160. managed_shared_memory::shrink_to_fit(ShmemName);
  161. {
  162. //Map preexisting shmem again in memory
  163. managed_shared_memory shmem(open_only, ShmemName);
  164. final_shmem_size = shmem.get_size();
  165. if(next_shmem_size <= final_shmem_size)
  166. return -1;
  167. }
  168. }
  169. #endif //ifndef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS_NO_GROW
  170. {
  171. //Now test move semantics
  172. managed_shared_memory original(open_only, ShmemName);
  173. managed_shared_memory move_ctor(boost::move(original));
  174. managed_shared_memory move_assign;
  175. move_assign = boost::move(move_ctor);
  176. move_assign.swap(original);
  177. }
  178. shared_memory_object::remove(ShmemName);
  179. return 0;
  180. }
  181. #include <boost/interprocess/detail/config_end.hpp>