doc_named_alloc.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-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. //[doc_named_alloc
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <cstdlib> //std::system
  15. #include <cstddef>
  16. #include <cassert>
  17. #include <utility>
  18. //<-
  19. #include "../test/get_process_id_name.hpp"
  20. //->
  21. int main(int argc, char *argv[])
  22. {
  23. using namespace boost::interprocess;
  24. typedef std::pair<double, int> MyType;
  25. if(argc == 1){ //Parent process
  26. //Remove shared memory on construction and destruction
  27. struct shm_remove
  28. {
  29. //<-
  30. #if 1
  31. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  32. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  33. #else
  34. //->
  35. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  36. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  37. //<-
  38. #endif
  39. //->
  40. } remover;
  41. //<-
  42. (void)remover;
  43. //->
  44. //Construct managed shared memory
  45. //<-
  46. #if 1
  47. managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
  48. #else
  49. //->
  50. managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  51. //<-
  52. #endif
  53. //->
  54. //Create an object of MyType initialized to {0.0, 0}
  55. MyType *instance = segment.construct<MyType>
  56. ("MyType instance") //name of the object
  57. (0.0, 0); //ctor first argument
  58. //Create an array of 10 elements of MyType initialized to {0.0, 0}
  59. MyType *array = segment.construct<MyType>
  60. ("MyType array") //name of the object
  61. [10] //number of elements
  62. (0.0, 0); //Same two ctor arguments for all objects
  63. //Create an array of 3 elements of MyType initializing each one
  64. //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
  65. float float_initializer[3] = { 0.0, 1.0, 2.0 };
  66. int int_initializer[3] = { 0, 1, 2 };
  67. MyType *array_it = segment.construct_it<MyType>
  68. ("MyType array from it") //name of the object
  69. [3] //number of elements
  70. ( &float_initializer[0] //Iterator for the 1st ctor argument
  71. , &int_initializer[0]); //Iterator for the 2nd ctor argument
  72. //Launch child process
  73. std::string s(argv[0]); s += " child ";
  74. //<-
  75. s += test::get_process_id_name();
  76. //->
  77. if(0 != std::system(s.c_str()))
  78. return 1;
  79. //<-
  80. (void)instance;
  81. (void)array;
  82. (void)array_it;
  83. //->
  84. //Check child has destroyed all objects
  85. if(segment.find<MyType>("MyType array").first ||
  86. segment.find<MyType>("MyType instance").first ||
  87. segment.find<MyType>("MyType array from it").first)
  88. return 1;
  89. }
  90. else{
  91. //Open managed shared memory
  92. //<-
  93. #if 1
  94. managed_shared_memory segment(open_only, argv[2]);
  95. #else
  96. //->
  97. managed_shared_memory segment(open_only, "MySharedMemory");
  98. //<-
  99. #endif
  100. //->
  101. std::pair<MyType*, managed_shared_memory::size_type> res;
  102. //Find the array
  103. res = segment.find<MyType> ("MyType array");
  104. //Length should be 10
  105. if(res.second != 10) return 1;
  106. //Find the object
  107. res = segment.find<MyType> ("MyType instance");
  108. //Length should be 1
  109. if(res.second != 1) return 1;
  110. //Find the array constructed from iterators
  111. res = segment.find<MyType> ("MyType array from it");
  112. //Length should be 3
  113. if(res.second != 3) return 1;
  114. //We're done, delete all the objects
  115. segment.destroy<MyType>("MyType array");
  116. segment.destroy<MyType>("MyType instance");
  117. segment.destroy<MyType>("MyType array from it");
  118. }
  119. return 0;
  120. }
  121. //]
  122. #include <boost/interprocess/detail/config_end.hpp>