doc_where_allocate.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_where_allocate
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/containers/vector.hpp>
  15. #include <boost/interprocess/containers/string.hpp>
  16. #include <boost/interprocess/allocators/allocator.hpp>
  17. //<-
  18. #include "../test/get_process_id_name.hpp"
  19. //->
  20. int main ()
  21. {
  22. using namespace boost::interprocess;
  23. //Typedefs
  24. typedef allocator<char, managed_shared_memory::segment_manager>
  25. CharAllocator;
  26. typedef basic_string<char, std::char_traits<char>, CharAllocator>
  27. MyShmString;
  28. typedef allocator<MyShmString, managed_shared_memory::segment_manager>
  29. StringAllocator;
  30. typedef vector<MyShmString, StringAllocator>
  31. MyShmStringVector;
  32. //Open shared memory
  33. //Remove shared memory on construction and destruction
  34. struct shm_remove
  35. {
  36. //<-
  37. #if 1
  38. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  39. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  40. #else
  41. //->
  42. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  43. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  44. //<-
  45. #endif
  46. //->
  47. } remover;
  48. //<-
  49. (void)remover;
  50. //->
  51. //<-
  52. #if 1
  53. managed_shared_memory shm(create_only, test::get_process_id_name(), 10000);
  54. #else
  55. //->
  56. managed_shared_memory shm(create_only, "MySharedMemory", 10000);
  57. //<-
  58. #endif
  59. //->
  60. //Create allocators
  61. CharAllocator charallocator (shm.get_segment_manager());
  62. StringAllocator stringallocator(shm.get_segment_manager());
  63. //This string is in only in this process (the pointer pointing to the
  64. //buffer that will hold the text is not in shared memory).
  65. //But the buffer that will hold "this is my text" is allocated from
  66. //shared memory
  67. MyShmString mystring(charallocator);
  68. mystring = "this is my text";
  69. //This vector is only in this process (the pointer pointing to the
  70. //buffer that will hold the MyShmString-s is not in shared memory).
  71. //But the buffer that will hold 10 MyShmString-s is allocated from
  72. //shared memory using StringAllocator. Since strings use a shared
  73. //memory allocator (CharAllocator) the 10 buffers that hold
  74. //"this is my text" text are also in shared memory.
  75. MyShmStringVector myvector(stringallocator);
  76. myvector.insert(myvector.begin(), 10, mystring);
  77. //This vector is fully constructed in shared memory. All pointers
  78. //buffers are constructed in the same shared memory segment
  79. //This vector can be safely accessed from other processes.
  80. MyShmStringVector *myshmvector =
  81. shm.construct<MyShmStringVector>("myshmvector")(stringallocator);
  82. myshmvector->insert(myshmvector->begin(), 10, mystring);
  83. //Destroy vector. This will free all strings that the vector contains
  84. shm.destroy_ptr(myshmvector);
  85. return 0;
  86. }
  87. //]
  88. #include <boost/interprocess/detail/config_end.hpp>