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