doc_managed_mapped_file.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #if defined(BOOST_INTERPROCESS_MAPPED_FILES)
  11. #include <boost/interprocess/detail/config_begin.hpp>
  12. #include <boost/interprocess/detail/workaround.hpp>
  13. #include <boost/interprocess/containers/list.hpp>
  14. #include <boost/interprocess/managed_mapped_file.hpp>
  15. #include <boost/interprocess/allocators/allocator.hpp>
  16. #include <cstddef>
  17. #include <cstdio>
  18. //<-
  19. #include "../test/get_process_id_name.hpp"
  20. //->
  21. using namespace boost::interprocess;
  22. typedef list<int, allocator<int, managed_mapped_file::segment_manager> >
  23. MyList;
  24. int main ()
  25. {
  26. //Define file names
  27. //<-
  28. #if 1
  29. std::string file(boost::interprocess::ipcdetail::get_temporary_path());
  30. file += "/"; file += test::get_process_id_name();
  31. const char *FileName = file.c_str();
  32. #else
  33. //->
  34. const char *FileName = "file_mapping";
  35. //<-
  36. #endif
  37. //->
  38. const std::size_t FileSize = 1000;
  39. file_mapping::remove(FileName);
  40. try{
  41. MyList::size_type old_size = 0;
  42. managed_mapped_file::handle_t list_handle;
  43. {
  44. managed_mapped_file mfile_memory(create_only, FileName, FileSize);
  45. MyList *mylist = mfile_memory.construct<MyList>("MyList")
  46. (mfile_memory.get_segment_manager());
  47. //Obtain handle, that identifies the list in the buffer
  48. list_handle = mfile_memory.get_handle_from_address(mylist);
  49. //Fill list until there is no more room in the file
  50. try{
  51. while(1) {
  52. mylist->insert(mylist->begin(), 0);
  53. }
  54. }
  55. catch(const bad_alloc &){
  56. //mapped file is full
  57. }
  58. //Let's obtain the size of the list
  59. old_size = mylist->size();
  60. }
  61. //To make the list bigger, let's increase the mapped file
  62. //in FileSize bytes more.
  63. managed_mapped_file::grow(FileName, FileSize*2);
  64. {
  65. managed_mapped_file mfile_memory(open_only, FileName);
  66. //If mapping address has changed, the old pointer is invalid,
  67. //so use previously obtained handle to find the new pointer.
  68. MyList *mylist = static_cast<MyList *>
  69. (mfile_memory.get_address_from_handle(list_handle));
  70. //Fill list until there is no more room in the file
  71. try{
  72. while(1) {
  73. mylist->insert(mylist->begin(), 0);
  74. }
  75. }
  76. catch(const bad_alloc &){
  77. //mapped file is full
  78. }
  79. //Let's obtain the new size of the list
  80. MyList::size_type new_size = mylist->size();
  81. assert(new_size > old_size);
  82. //Destroy list
  83. mfile_memory.destroy_ptr(mylist);
  84. }
  85. }
  86. catch(...){
  87. file_mapping::remove(FileName);
  88. throw;
  89. }
  90. file_mapping::remove(FileName);
  91. return 0;
  92. }
  93. #include <boost/interprocess/detail/config_end.hpp>
  94. #else //#if defined(BOOST_INTERPROCESS_MAPPED_FILES)
  95. int main()
  96. {
  97. return 0;
  98. }
  99. #endif//#if defined(BOOST_INTERPROCESS_MAPPED_FILES)