shared_memory_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-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/shared_memory_object.hpp>
  12. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  13. #include <boost/interprocess/exceptions.hpp>
  14. #include "named_creation_template.hpp"
  15. #include <cstring> //for strcmp, memset
  16. #include <iostream> //for cout
  17. #include <string>
  18. #include "get_process_id_name.hpp"
  19. using namespace boost::interprocess;
  20. static const std::size_t ShmSize = 1000;
  21. static const char * ShmName = test::get_process_id_name();
  22. struct eraser
  23. {
  24. ~eraser()
  25. {
  26. shared_memory_object::remove(ShmName);
  27. }
  28. };
  29. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> shared_memory;
  30. //This wrapper is necessary to have a common constructor
  31. //in generic named_creation_template functions
  32. class shared_memory_creation_test_wrapper
  33. : public eraser
  34. , public shared_memory
  35. {
  36. public:
  37. shared_memory_creation_test_wrapper(create_only_t)
  38. : shared_memory(create_only, ShmName, ShmSize, read_write, 0, permissions())
  39. {}
  40. shared_memory_creation_test_wrapper(open_only_t)
  41. : shared_memory(open_only, ShmName, read_write, 0)
  42. {}
  43. shared_memory_creation_test_wrapper(open_or_create_t)
  44. : shared_memory(open_or_create, ShmName, ShmSize, read_write, 0, permissions())
  45. {}
  46. };
  47. int main ()
  48. {
  49. try{
  50. shared_memory_object::remove(ShmName);
  51. test::test_named_creation<shared_memory_creation_test_wrapper>();
  52. //Create and get name, size and address
  53. {
  54. shared_memory_object::remove(ShmName);
  55. shared_memory shm1(create_only, ShmName, ShmSize, read_write, 0, permissions());
  56. //Overwrite all memory
  57. std::memset(shm1.get_user_address(), 0, shm1.get_user_size());
  58. //Now test move semantics
  59. shared_memory move_ctor(boost::move(shm1));
  60. shared_memory move_assign;
  61. move_assign = boost::move(move_ctor);
  62. }
  63. }
  64. catch(std::exception &ex){
  65. shared_memory_object::remove(ShmName);
  66. std::cout << ex.what() << std::endl;
  67. return 1;
  68. }
  69. shared_memory_object::remove(ShmName);
  70. return 0;
  71. }
  72. #include <boost/interprocess/detail/config_end.hpp>