Ticket #6897: test.cc

File test.cc, 4.5 KB (added by Mathias Creutz <mathiascreutz@…>, 10 years ago)

For testing the different behaviour open_only vs open_read_only

Line 
1#include <utility>
2#include <string>
3#include <iostream>
4#include <functional>
5#include <algorithm>
6
7#include <boost/variant.hpp>
8#include <boost/interprocess/managed_shared_memory.hpp>
9#include <boost/interprocess/smart_ptr/shared_ptr.hpp>
10#include <boost/interprocess/allocators/allocator.hpp>
11#include <boost/interprocess/containers/string.hpp>
12#include <boost/interprocess/sync/scoped_lock.hpp>
13#include <boost/interprocess/sync/named_mutex.hpp>
14#include <boost/interprocess/sync/named_condition.hpp>
15#include <boost/unordered_map.hpp>
16#include <boost/functional/hash.hpp>
17
18using namespace boost::interprocess;
19
20typedef managed_shared_memory::allocator<char>::type char_allocator;
21typedef basic_string<char, std::char_traits<char>, char_allocator> shm_string;
22
23typedef shm_string KeyType;
24typedef shm_string MappedType;
25typedef std::pair<const KeyType, MappedType> ValueType;
26
27typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
28typedef boost::unordered_map
29< KeyType , MappedType
30 , boost::hash<KeyType> ,std::equal_to<KeyType>
31 , ShmemAllocator>
32Hash;
33
34const char * const K_MUTEX_SUFFIX = "__MUTEX";
35const char * const K_CONDITION_SUFFIX = "__CONDITION";
36
37std::string MutexName(const std::string segment_name);
38std::string ConditionalName(const std::string segment_name);
39
40struct shm_remove
41{
42 shm_remove(const std::string& segment_name)
43 : segment_name_(segment_name)
44 {
45 const std::string mutex_name = MutexName(segment_name_);
46 const std::string conditional_name = ConditionalName(segment_name_);
47 shared_memory_object::remove( segment_name_.c_str() );
48 boost::interprocess::named_mutex::remove( mutex_name.c_str() );
49 boost::interprocess::named_mutex::remove( conditional_name.c_str() );
50 }
51 ~shm_remove()
52 {
53 const std::string mutex_name = MutexName(segment_name_);
54 const std::string conditional_name = ConditionalName(segment_name_);
55 shared_memory_object::remove( segment_name_.c_str() );
56 boost::interprocess::named_mutex::remove( mutex_name.c_str() );
57 boost::interprocess::named_mutex::remove( conditional_name.c_str() );
58 }
59 std::string segment_name_;
60private:
61 // non copyable nor assignable ( could be movable though)
62 shm_remove(const shm_remove&);
63 shm_remove& operator=(shm_remove);
64};
65
66std::string MutexName(const std::string segment_name)
67{
68 std::stringstream str;
69 str << "K_" << segment_name << K_MUTEX_SUFFIX;
70 return str.str();
71}
72
73std::string ConditionalName(const std::string segment_name)
74{
75 std::stringstream str;
76 str << "K_" << segment_name << K_CONDITION_SUFFIX;
77 return str.str();
78}
79
80int main()
81{
82 // 23 + \0
83 const std::string key = "NokiaNokiaNokiaNokiaNok";
84 const std::string val = "NokiaNokiaNokiaNokiaNok_NokiaNokiaNokiaNokiaNok_NokiaNokiaNokiaNokiaNok_NokiaNokiaNokiaNokiaNok_NokiaNokiaNokiaNokiaNok_NokiaNokiaNokiaNokiaNok";
85
86 const std::string segment_name = "test segment numbero uno";
87 const std::string mutex_name = MutexName(segment_name);
88 const std::string conditional_name = ConditionalName(segment_name);
89 // remove shared memory on construction/destruction
90 shm_remove remover(segment_name);
91
92 // create shared memory
93 managed_shared_memory segment(create_only, segment_name.c_str(), 4 * 1024);
94
95 // construct a shared memory hash map
96 // note that the first parameter is the initial bucket count and
97 // after that, the hash function, the equality function and the allocator
98 Hash * h = segment.construct<Hash>("Hash") //object name
99 ( 3, boost::hash<KeyType>(), std::equal_to<KeyType>() //
100 , segment.get_allocator<ValueType>()); //allocator instance
101
102 char_allocator sa(segment.get_allocator<char>());
103 shm_string shared_key(key.c_str(), sa);
104 shm_string shared_val(val.c_str(), sa);
105 ValueType vt(shared_key, shared_val);
106 h->insert(vt);
107 {
108 // open_only works regardless of key length
109 // open_read_only segfaults at key byte len >= 24
110 managed_shared_memory segment(open_only, segment_name.c_str());
111 char_allocator sa(segment.get_allocator<char>());
112 // SEGFAULT here depending on mode and key length
113 shm_string shared_key(key.c_str(), sa);
114 Hash::const_iterator i = h->find(shared_key);
115 if(h->end() == i)
116 {
117 std::cerr << "no such key " << key << std::endl;
118 return 1;
119 }
120 const ValueType& vt = *i;
121 std::stringstream str;
122 str << vt.second;
123 std::cout << key << " => " << str.str() << std::endl;
124 }
125 return 0;
126}