doc_managed_raw_allocation.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_raw_allocation
  12. #include <boost/interprocess/managed_shared_memory.hpp>
  13. //<-
  14. #include "../test/get_process_id_name.hpp"
  15. //->
  16. int main()
  17. {
  18. using namespace boost::interprocess;
  19. //Remove shared memory on construction and destruction
  20. struct shm_remove
  21. {
  22. //<-
  23. #if 1
  24. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  25. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  26. #else
  27. //->
  28. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  29. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  30. //<-
  31. #endif
  32. //->
  33. } remover;
  34. //<-
  35. (void)remover;
  36. //->
  37. //Managed memory segment that allocates portions of a shared memory
  38. //segment with the default management algorithm
  39. //<-
  40. #if 1
  41. managed_shared_memory managed_shm(create_only,test::get_process_id_name(), 65536);
  42. #else
  43. //->
  44. managed_shared_memory managed_shm(create_only,"MySharedMemory", 65536);
  45. //<-
  46. #endif
  47. //->
  48. //Allocate 100 bytes of memory from segment, throwing version
  49. void *ptr = managed_shm.allocate(100);
  50. //Deallocate it
  51. managed_shm.deallocate(ptr);
  52. //Non throwing version
  53. ptr = managed_shm.allocate(100, std::nothrow);
  54. //Deallocate it
  55. managed_shm.deallocate(ptr);
  56. return 0;
  57. }
  58. //]
  59. #include <boost/interprocess/detail/config_end.hpp>