doc_managed_aligned_allocation.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_aligned_allocation
  12. #include <boost/interprocess/managed_shared_memory.hpp>
  13. #include <cassert>
  14. //<-
  15. #include "../test/get_process_id_name.hpp"
  16. //->
  17. int main()
  18. {
  19. using namespace boost::interprocess;
  20. //Remove shared memory on construction and destruction
  21. struct shm_remove
  22. {
  23. //<-
  24. #if 1
  25. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  26. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  27. #else
  28. //->
  29. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  30. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  31. //<-
  32. #endif
  33. //->
  34. } remover;
  35. //<-
  36. (void)remover;
  37. //->
  38. //Managed memory segment that allocates portions of a shared memory
  39. //segment with the default management algorithm
  40. //<-
  41. #if 1
  42. managed_shared_memory managed_shm(create_only, test::get_process_id_name(), 65536);
  43. #else
  44. //->
  45. managed_shared_memory managed_shm(create_only, "MySharedMemory", 65536);
  46. //<-
  47. #endif
  48. //->
  49. const std::size_t Alignment = 128;
  50. //Allocate 100 bytes aligned to Alignment from segment, throwing version
  51. void *ptr = managed_shm.allocate_aligned(100, Alignment);
  52. //Check alignment
  53. assert((static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
  54. //Deallocate it
  55. managed_shm.deallocate(ptr);
  56. //Non throwing version
  57. ptr = managed_shm.allocate_aligned(100, Alignment, std::nothrow);
  58. //Check alignment
  59. assert((static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
  60. //Deallocate it
  61. managed_shm.deallocate(ptr);
  62. //If we want to efficiently allocate aligned blocks of memory
  63. //use managed_shared_memory::PayloadPerAllocation value
  64. assert(Alignment > managed_shared_memory::PayloadPerAllocation);
  65. //This allocation will maximize the size of the aligned memory
  66. //and will increase the possibility of finding more aligned memory
  67. ptr = managed_shm.allocate_aligned
  68. (3*Alignment - managed_shared_memory::PayloadPerAllocation, Alignment);
  69. //Check alignment
  70. assert((static_cast<char*>(ptr)-static_cast<char*>(0)) % Alignment == 0);
  71. //Deallocate it
  72. managed_shm.deallocate(ptr);
  73. return 0;
  74. }
  75. //]
  76. /*
  77. #include <vector>
  78. #include <boost/interprocess/managed_windows_shared_memory.hpp>
  79. int main()
  80. {
  81. using namespace boost::interprocess;
  82. typedef boost::interprocess::
  83. managed_windows_shared_memory shared_segment;
  84. std::vector<void *> ptrs;
  85. shared_segment m_segment(create_only, "shmem", 4096*16);
  86. try{
  87. while(1){
  88. //Now I have several allocate_aligned operations:
  89. ptrs.push_back(m_segment.allocate_aligned(128, 128));
  90. }
  91. }
  92. catch(...){
  93. m_segment.deallocate(ptrs.back());
  94. ptrs.pop_back();
  95. ptrs.push_back(m_segment.allocate_aligned(128, 128));
  96. }
  97. return 0;
  98. }
  99. */
  100. #include <boost/interprocess/detail/config_end.hpp>