doc_cached_adaptive_pool.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_cached_adaptive_pool
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/allocators/cached_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,test::get_process_id_name(), 65536);
  44. #else
  45. //->
  46. managed_shared_memory segment(create_only,
  47. "MySharedMemory", //segment name
  48. 65536);
  49. //<-
  50. #endif
  51. //->
  52. //Create a cached_adaptive_pool that allocates ints from the managed segment
  53. //The number of chunks per segment is the default value
  54. typedef cached_adaptive_pool<int, managed_shared_memory::segment_manager>
  55. cached_adaptive_pool_t;
  56. cached_adaptive_pool_t allocator_instance(segment.get_segment_manager());
  57. //The max cached nodes are configurable per instance
  58. allocator_instance.set_max_cached_nodes(3);
  59. //Create another cached_adaptive_pool. Since the segment manager address
  60. //is the same, this cached_adaptive_pool will be
  61. //attached to the same pool so "allocator_instance2" can deallocate
  62. //nodes allocated by "allocator_instance"
  63. cached_adaptive_pool_t allocator_instance2(segment.get_segment_manager());
  64. //The max cached nodes are configurable per instance
  65. allocator_instance2.set_max_cached_nodes(5);
  66. //Create another cached_adaptive_pool using copy-constructor. This
  67. //cached_adaptive_pool will also be attached to the same pool
  68. cached_adaptive_pool_t allocator_instance3(allocator_instance2);
  69. //We can clear the cache
  70. allocator_instance3.deallocate_cache();
  71. //All allocators are equal
  72. assert(allocator_instance == allocator_instance2);
  73. assert(allocator_instance2 == allocator_instance3);
  74. //So memory allocated with one can be deallocated with another
  75. allocator_instance2.deallocate(allocator_instance.allocate(1), 1);
  76. allocator_instance3.deallocate(allocator_instance2.allocate(1), 1);
  77. //The common pool will be destroyed here, since no allocator is
  78. //attached to the pool
  79. return 0;
  80. }
  81. //]
  82. #include <boost/interprocess/detail/config_end.hpp>