• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2008-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 #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
14 
15 #include <boost/interprocess/allocators/allocator.hpp>
16 #include <boost/interprocess/containers/vector.hpp>
17 #include <boost/interprocess/managed_xsi_shared_memory.hpp>
18 #include <boost/interprocess/detail/file_wrapper.hpp>
19 #include <boost/interprocess/file_mapping.hpp>
20 #include <cstdio>
21 #include <string>
22 #include "get_process_id_name.hpp"
23 
24 using namespace boost::interprocess;
25 
remove_shared_memory(const xsi_key & key)26 void remove_shared_memory(const xsi_key &key)
27 {
28    try{
29       xsi_shared_memory xsi(open_only, key);
30       xsi_shared_memory::remove(xsi.get_shmid());
31    }
32    catch(interprocess_exception &e){
33       if(e.get_error_code() != not_found_error)
34          throw;
35    }
36 }
37 
38 class xsi_shared_memory_remover
39 {
40    public:
xsi_shared_memory_remover(xsi_shared_memory & xsi_shm)41    xsi_shared_memory_remover(xsi_shared_memory &xsi_shm)
42       : xsi_shm_(xsi_shm)
43    {}
44 
~xsi_shared_memory_remover()45    ~xsi_shared_memory_remover()
46    {  xsi_shared_memory::remove(xsi_shm_.get_shmid());  }
47    private:
48    xsi_shared_memory & xsi_shm_;
49 };
50 
get_filename()51 inline std::string get_filename()
52 {
53    std::string ret (ipcdetail::get_temporary_path());
54    ret += "/";
55    ret += test::get_process_id_name();
56    return ret;
57 }
58 
main()59 int main ()
60 {
61    const int ShmemSize          = 65536;
62    std::string filename = get_filename();
63    const char *const ShmemName = filename.c_str();
64 
65    file_mapping::remove(ShmemName);
66    {  ipcdetail::file_wrapper(create_only, ShmemName, read_write); }
67    xsi_key key(ShmemName, 1);
68    file_mapping::remove(ShmemName);
69    int shmid;
70 
71    //STL compatible allocator object for memory-mapped shmem
72    typedef allocator<int, managed_xsi_shared_memory::segment_manager>
73       allocator_int_t;
74    //A vector that uses that allocator
75    typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
76 
77    {
78       //Remove the shmem it is already created
79       remove_shared_memory(key);
80 
81       const int max              = 100;
82       void *array[max];
83       //Named allocate capable shared memory allocator
84       managed_xsi_shared_memory shmem(create_only, key, ShmemSize);
85       shmid = shmem.get_shmid();
86       int i;
87       //Let's allocate some memory
88       for(i = 0; i < max; ++i){
89          array[i] = shmem.allocate(i+1);
90       }
91 
92       //Deallocate allocated memory
93       for(i = 0; i < max; ++i){
94          shmem.deallocate(array[i]);
95       }
96    }
97 
98    {
99       //Remove the shmem it is already created
100       xsi_shared_memory::remove(shmid);
101 
102       //Named allocate capable memory mapped shmem managed memory class
103       managed_xsi_shared_memory shmem(create_only, key, ShmemSize);
104       shmid = shmem.get_shmid();
105 
106       //Construct the STL-like allocator with the segment manager
107       const allocator_int_t myallocator (shmem.get_segment_manager());
108 
109       //Construct vector
110       MyVect *shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
111 
112       //Test that vector can be found via name
113       if(shmem_vect != shmem.find<MyVect>("MyVector").first)
114          return -1;
115 
116       //Destroy and check it is not present
117       shmem.destroy<MyVect> ("MyVector");
118       if(0 != shmem.find<MyVect>("MyVector").first)
119          return -1;
120 
121       //Construct a vector in the memory-mapped shmem
122       shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
123    }
124    {
125       //Map preexisting shmem again in memory
126       managed_xsi_shared_memory shmem(open_only, key);
127       shmid = shmem.get_shmid();
128 
129       //Check vector is still there
130       MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
131       if(!shmem_vect)
132          return -1;
133    }
134 
135 
136    {
137       //Now destroy the vector
138       {
139          //Map preexisting shmem again in memory
140          managed_xsi_shared_memory shmem(open_only, key);
141          shmid = shmem.get_shmid();
142 
143          //Destroy and check it is not present
144          shmem.destroy<MyVect>("MyVector");
145          if(0 != shmem.find<MyVect>("MyVector").first)
146             return -1;
147       }
148 
149       {
150          //Now test move semantics
151          managed_xsi_shared_memory original(open_only, key);
152          managed_xsi_shared_memory move_ctor(boost::move(original));
153          managed_xsi_shared_memory move_assign;
154          move_assign = boost::move(move_ctor);
155          move_assign.swap(original);
156       }
157    }
158 
159    xsi_shared_memory::remove(shmid);
160    return 0;
161 }
162 
163 #else
164 
main()165 int main()
166 {
167    return 0;
168 }
169 
170 #endif  //#ifndef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
171