doc_private_adaptive_pool.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_private_adaptive_pool
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
  15. #include <cassert>
  16. //<-
  17. #include "../test/get_process_id_name.hpp"
  18. //->
  19. using namespace boost::interprocess;
  20. int main ()
  21. {
  22. //Remove shared memory on construction and destruction
  23. struct shm_remove
  24. {
  25. //<-
  26. #if 1
  27. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  28. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  29. #else
  30. //->
  31. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  32. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  33. //<-
  34. #endif
  35. //->
  36. } remover;
  37. //<-
  38. (void)remover;
  39. //->
  40. //Create shared memory
  41. //<-
  42. #if 1
  43. managed_shared_memory segment(create_only,
  44. test::get_process_id_name(), //segment name
  45. 65536);
  46. #else
  47. //->
  48. managed_shared_memory segment(create_only,
  49. "MySharedMemory", //segment name
  50. 65536);
  51. //<-
  52. #endif
  53. //->
  54. //Create a private_adaptive_pool that allocates ints from the managed segment
  55. //The number of chunks per segment is the default value
  56. typedef private_adaptive_pool<int, managed_shared_memory::segment_manager>
  57. private_adaptive_pool_t;
  58. private_adaptive_pool_t allocator_instance(segment.get_segment_manager());
  59. //Create another private_adaptive_pool.
  60. private_adaptive_pool_t allocator_instance2(segment.get_segment_manager());
  61. //Although the segment manager address
  62. //is the same, this private_adaptive_pool will have its own pool so
  63. //"allocator_instance2" CAN'T deallocate nodes allocated by "allocator_instance".
  64. //"allocator_instance2" is NOT equal to "allocator_instance"
  65. assert(allocator_instance != allocator_instance2);
  66. //Create another adaptive_pool using copy-constructor.
  67. private_adaptive_pool_t allocator_instance3(allocator_instance2);
  68. //This allocator is also unequal to allocator_instance2
  69. assert(allocator_instance2 != allocator_instance3);
  70. //Pools are destroyed with the allocators
  71. return 0;
  72. }
  73. //]
  74. #include <boost/interprocess/detail/config_end.hpp>