doc_managed_external_buffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. //[doc_managed_external_buffer
  12. #include <boost/interprocess/managed_external_buffer.hpp>
  13. #include <boost/interprocess/allocators/allocator.hpp>
  14. #include <boost/interprocess/containers/list.hpp>
  15. #include <cstring>
  16. #include <boost/aligned_storage.hpp>
  17. int main()
  18. {
  19. using namespace boost::interprocess;
  20. //Create the static memory who will store all objects
  21. const int memsize = 65536;
  22. static boost::aligned_storage<memsize>::type static_buffer;
  23. //This managed memory will construct objects associated with
  24. //a wide string in the static buffer
  25. wmanaged_external_buffer objects_in_static_memory
  26. (create_only, &static_buffer, memsize);
  27. //We optimize resources to create 100 named objects in the static buffer
  28. objects_in_static_memory.reserve_named_objects(100);
  29. //Alias an integer node allocator type
  30. //This allocator will allocate memory inside the static buffer
  31. typedef allocator<int, wmanaged_external_buffer::segment_manager>
  32. allocator_t;
  33. //Alias a STL compatible list to be constructed in the static buffer
  34. typedef list<int, allocator_t> MyBufferList;
  35. //The list must be initialized with the allocator
  36. //All objects created with objects_in_static_memory will
  37. //be stored in the static_buffer!
  38. MyBufferList *list = objects_in_static_memory.construct<MyBufferList>(L"MyList")
  39. (objects_in_static_memory.get_segment_manager());
  40. //<-
  41. (void)list;
  42. //->
  43. //Since the allocation algorithm from wmanaged_external_buffer uses relative
  44. //pointers and all the pointers constructed int the static memory point
  45. //to objects in the same segment, we can create another static buffer
  46. //from the first one and duplicate all the data.
  47. static boost::aligned_storage<memsize>::type static_buffer2;
  48. std::memcpy(&static_buffer2, &static_buffer, memsize);
  49. //Now open the duplicated managed memory passing the memory as argument
  50. wmanaged_external_buffer objects_in_static_memory2
  51. (open_only, &static_buffer2, memsize);
  52. //Check that "MyList" has been duplicated in the second buffer
  53. if(!objects_in_static_memory2.find<MyBufferList>(L"MyList").first)
  54. return 1;
  55. //Destroy the lists from the static buffers
  56. objects_in_static_memory.destroy<MyBufferList>(L"MyList");
  57. objects_in_static_memory2.destroy<MyBufferList>(L"MyList");
  58. return 0;
  59. }
  60. //]
  61. #include <boost/interprocess/detail/config_end.hpp>