File: doc_where_allocate.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (74 lines) | stat: -rw-r--r-- 3,029 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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. 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>
//[doc_where_allocate
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

int main ()
{
   using namespace boost::interprocess;
   //Typedefs
   typedef allocator<char, managed_shared_memory::segment_manager> 
      CharAllocator;
   typedef basic_string<char, std::char_traits<char>, CharAllocator>
      MyShmString;
   typedef allocator<MyShmString, managed_shared_memory::segment_manager>
      StringAllocator;      
   typedef vector<MyShmString, StringAllocator>
      MyShmStringVector;

   //Open shared memory
   shared_memory_object::remove("myshm");
   try{
      managed_shared_memory shm(create_only, "myshm", 10000);

      //Create allocators
      CharAllocator     charallocator  (shm.get_segment_manager());
      StringAllocator   stringallocator(shm.get_segment_manager());

      //This string is in only in this process (the pointer pointing to the
      //buffer that will hold the text is not in shared memory). 
      //But the buffer that will hold "this is my text" is allocated from 
      //shared memory
      MyShmString mystring(charallocator);
      mystring = "this is my text";

      //This vector is only in this process (the pointer pointing to the
      //buffer that will hold the MyShmString-s is not in shared memory). 
      //But the buffer that will hold 10 MyShmString-s is allocated from 
      //shared memory using StringAllocator. Since strings use a shared 
      //memory allocator (CharAllocator) the 10 buffers that hold 
      //"this is my text" text are also in shared memory.
      MyShmStringVector myvector(stringallocator);
      myvector.insert(myvector.begin(), 10, mystring);

      //This vector is fully constructed in shared memory. All pointers
      //buffers are constructed in the same shared memory segment
      //This vector can be safely accessed from other processes.
      MyShmStringVector *myshmvector = 
         shm.construct<MyShmStringVector>("myshmvector")(stringallocator);
      myshmvector->insert(myshmvector->begin(), 10, mystring);

      //Destroy vector. This will free all strings that the vector contains
      shm.destroy_ptr(myshmvector);
   }
   catch(...){
      shared_memory_object::remove("myshm");
      throw;
   }
   shared_memory_object::remove("myshm");
   return 0;
}
//]
#include <boost/interprocess/detail/config_end.hpp>