File: doc_managed_mapped_file.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (91 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2009. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>

#include <boost/interprocess/containers/list.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <cstddef>
#include <cstdio>

using namespace boost::interprocess;
typedef list<int, allocator<int, managed_mapped_file::segment_manager> > 
   MyList;

int main ()
{
   const char *FileName       = "file_mapping";
   const std::size_t FileSize = 1000;
   file_mapping::remove(FileName);
   try{
      std::size_t old_size = 0;
      managed_mapped_file::handle_t list_handle;
      {
         managed_mapped_file mfile_memory(create_only, FileName, FileSize);
         MyList *mylist = mfile_memory.construct<MyList>("MyList")
                              (mfile_memory.get_segment_manager());

         //Obtain handle, that identifies the list in the buffer
         list_handle = mfile_memory.get_handle_from_address(mylist);

         //Fill list until there is no more room in the file
         try{
            while(1) {
               mylist->insert(mylist->begin(), 0);
            }
         }
         catch(const bad_alloc &){
            //mapped file is full
         }
         //Let's obtain the size of the list
         old_size = mylist->size();
      }
      //To make the list bigger, let's increase the mapped file
      //in FileSize bytes more.
      managed_mapped_file::grow(FileName, FileSize*2);

      {
         managed_mapped_file mfile_memory(open_only, FileName);


         //If mapping address has changed, the old pointer is invalid,
         //so use previously obtained handle to find the new pointer.
         MyList *mylist = static_cast<MyList *>
                           (mfile_memory.get_address_from_handle(list_handle));
         
         //Fill list until there is no more room in the file
         try{
            while(1) {
               mylist->insert(mylist->begin(), 0);
            }
         }
         catch(const bad_alloc &){
            //mapped file is full
         }

         //Let's obtain the new size of the list      
         std::size_t new_size = mylist->size();

         assert(new_size > old_size);

         //Destroy list
         mfile_memory.destroy_ptr(mylist);
      }
   }
   catch(...){
      file_mapping::remove(FileName);
      throw;
   }
   file_mapping::remove(FileName);
   return 0;
}

#include <boost/interprocess/detail/config_end.hpp>