doc_allocator.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_allocator
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/allocators/allocator.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 an allocator that allocates ints from the managed segment
  53. allocator<int, managed_shared_memory::segment_manager>
  54. allocator_instance(segment.get_segment_manager());
  55. //Copy constructed allocator is equal
  56. allocator<int, managed_shared_memory::segment_manager>
  57. allocator_instance2(allocator_instance);
  58. assert(allocator_instance2 == allocator_instance);
  59. //Allocate and deallocate memory for 100 ints
  60. allocator_instance2.deallocate(allocator_instance.allocate(100), 100);
  61. return 0;
  62. }
  63. //]
  64. #include <boost/interprocess/detail/config_end.hpp>