• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include <boost/interprocess/detail/workaround.hpp>
12 
13 #ifdef BOOST_INTERPROCESS_WINDOWS
14 
15 #include <boost/interprocess/allocators/allocator.hpp>
16 #include <boost/interprocess/containers/vector.hpp>
17 #include <boost/interprocess/managed_windows_shared_memory.hpp>
18 #include <cstdio>
19 #include <string>
20 #include "get_process_id_name.hpp"
21 
22 using namespace boost::interprocess;
23 
main()24 int main ()
25 {
26    const int MemSize          = 65536;
27    const char *const MemName  = test::get_process_id_name();
28 
29    //STL compatible allocator object for shared memory
30    typedef allocator<int, managed_windows_shared_memory::segment_manager>
31       allocator_int_t;
32    //A vector that uses that allocator
33    typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
34 
35    {
36       const int max              = 100;
37       void *array[max];
38       //Named allocate capable shared memory allocator
39       managed_windows_shared_memory w_shm(create_only, MemName, MemSize);
40 
41       int i;
42       //Let's allocate some memory
43       for(i = 0; i < max; ++i){
44          array[i] = w_shm.allocate(i+1);
45       }
46 
47       //Deallocate allocated memory
48       for(i = 0; i < max; ++i){
49          w_shm.deallocate(array[i]);
50       }
51    }
52 
53    {
54       //Named allocate capable shared memory managed memory class
55       managed_windows_shared_memory w_shm(create_only, MemName, MemSize);
56 
57       //Construct the STL-like allocator with the segment manager
58       const allocator_int_t myallocator (w_shm.get_segment_manager());
59 
60       //Construct vector
61       MyVect *w_shm_vect = w_shm.construct<MyVect> ("MyVector") (myallocator);
62 
63       //Test that vector can be found via name
64       if(w_shm_vect != w_shm.find<MyVect>("MyVector").first)
65          return -1;
66 
67       //Destroy and check it is not present
68       w_shm.destroy<MyVect> ("MyVector");
69       if(0 != w_shm.find<MyVect>("MyVector").first)
70          return -1;
71 
72       //Construct a vector in the shared memory
73       w_shm_vect = w_shm.construct<MyVect> ("MyVector") (myallocator);
74 
75       {
76          //Map preexisting segment again in memory
77          managed_windows_shared_memory w_shm_new(open_only, MemName);
78 
79          //Check vector is still there
80          w_shm_vect = w_shm_new.find<MyVect>("MyVector").first;
81          if(!w_shm_vect)
82             return -1;
83 
84          if(w_shm_new.get_size() != w_shm.get_size())
85             return 1;
86 
87          {
88             {
89                //Map preexisting shmem again in copy-on-write
90                managed_windows_shared_memory shmem(open_copy_on_write, MemName);
91 
92                //Check vector is still there
93                MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
94                if(!shmem_vect)
95                   return -1;
96 
97                //Erase vector
98                shmem.destroy_ptr(shmem_vect);
99 
100                //Make sure vector is erased
101                shmem_vect = shmem.find<MyVect>("MyVector").first;
102                if(shmem_vect)
103                   return -1;
104             }
105             //Now check vector is still in the s
106             {
107                //Map preexisting shmem again in copy-on-write
108                managed_windows_shared_memory shmem(open_copy_on_write, MemName);
109 
110                //Check vector is still there
111                MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
112                if(!shmem_vect)
113                   return -1;
114             }
115          }
116          {
117             //Map preexisting shmem again in copy-on-write
118             managed_windows_shared_memory shmem(open_read_only, MemName);
119 
120             //Check vector is still there
121             MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
122             if(!shmem_vect)
123                return -1;
124          }
125 
126          //Destroy and check it is not present
127          w_shm_new.destroy_ptr(w_shm_vect);
128          if(0 != w_shm_new.find<MyVect>("MyVector").first)
129             return 1;
130 
131          //Now test move semantics
132          managed_windows_shared_memory original(open_only, MemName);
133          managed_windows_shared_memory move_ctor(boost::move(original));
134          managed_windows_shared_memory move_assign;
135          move_assign = boost::move(move_ctor);
136       }
137    }
138 
139    return 0;
140 }
141 
142 #else
143 
main()144 int main()
145 {
146    return 0;
147 }
148 
149 #endif
150