Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_unordered_map.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2009. 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 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
12 //[doc_unordered_map
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
15
16 #include <boost/unordered_map.hpp>     //boost::unordered_map
17 #include <functional>                  //std::equal_to
18 #include <boost/functional/hash.hpp>   //boost::hash
19 //<-
20 #include "../test/get_process_id_name.hpp"
21 //->
22
23 int main ()
24 {
25    using namespace boost::interprocess;
26    //Remove shared memory on construction and destruction
27    struct shm_remove
28    {
29    //<-
30    #if 1
31       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
32       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
33    #else
34    //->
35       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
36       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
37    //<-
38    #endif
39    //->
40    } remover;
41
42    //Create shared memory
43    //<-
44    #if 1
45    managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
46    #else
47    //->
48    managed_shared_memory segment(create_only, "MySharedMemory", 65536);
49    //<-
50    #endif
51    //->
52
53    //Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
54    //so the allocator must allocate that pair.
55    typedef int    KeyType;
56    typedef float  MappedType;
57    typedef std::pair<const int, float> ValueType;
58
59    //Typedef the allocator
60    typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
61
62    //Alias an unordered_map of ints that uses the previous STL-like allocator.
63    typedef boost::unordered_map
64       < KeyType               , MappedType
65       , boost::hash<KeyType>  ,std::equal_to<KeyType>
66       , ShmemAllocator>
67    MyHashMap;
68
69    //Construct a shared memory hash map.
70    //Note that the first parameter is the initial bucket count and
71    //after that, the hash function, the equality function and the allocator
72    MyHashMap *myhashmap = segment.construct<MyHashMap>("MyHashMap")  //object name
73       ( 3, boost::hash<int>(), std::equal_to<int>()                  //
74       , segment.get_allocator<ValueType>());                         //allocator instance
75
76    //Insert data in the hash map
77    for(int i = 0; i < 100; ++i){
78       myhashmap->insert(ValueType(i, (float)i));
79    }
80    return 0;
81 }
82 //]
83 #include <boost/interprocess/detail/config_end.hpp>