Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / doc_cached_node_allocator.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_cached_node_allocator
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/allocators/cached_node_allocator.hpp>
15 #include <cassert>
16 //<-
17 #include "../test/get_process_id_name.hpp"
18 //->
19
20 using namespace boost::interprocess;
21
22 int main ()
23 {
24    //Remove shared memory on construction and destruction
25    struct shm_remove
26    {
27    //<-
28    #if 1
29       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
30       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
31    #else
32    //->
33       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
34       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
35    //<-
36    #endif
37    //->
38    } remover;
39
40    //Create shared memory
41    //<-
42    #if 1
43    managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
44    #else
45    //->
46    managed_shared_memory segment(create_only, 
47                                  "MySharedMemory",  //segment name
48                                  65536);
49    //<-
50    #endif
51    //->
52
53    //Create a cached_node_allocator that allocates ints from the managed segment
54    //The number of chunks per segment is the default value
55    typedef cached_node_allocator<int, managed_shared_memory::segment_manager>
56       cached_node_allocator_t;
57    cached_node_allocator_t allocator_instance(segment.get_segment_manager());
58
59    //The max cached nodes are configurable per instance
60    allocator_instance.set_max_cached_nodes(3);
61
62    //Create another cached_node_allocator. Since the segment manager address
63    //is the same, this cached_node_allocator will be
64    //attached to the same pool so "allocator_instance2" can deallocate
65    //nodes allocated by "allocator_instance"
66    cached_node_allocator_t allocator_instance2(segment.get_segment_manager());
67
68    //The max cached nodes are configurable per instance
69    allocator_instance2.set_max_cached_nodes(5);
70
71    //Create another cached_node_allocator using copy-constructor. This
72    //cached_node_allocator will also be attached to the same pool
73    cached_node_allocator_t allocator_instance3(allocator_instance2);
74
75    //We can clear the cache
76    allocator_instance3.deallocate_cache();
77
78    //All allocators are equal
79    assert(allocator_instance == allocator_instance2);
80    assert(allocator_instance2 == allocator_instance3);
81
82    //So memory allocated with one can be deallocated with another
83    allocator_instance2.deallocate(allocator_instance.allocate(1), 1);
84    allocator_instance3.deallocate(allocator_instance2.allocate(1), 1);
85
86    //The common pool will be destroyed here, since no allocator is
87    //attached to the pool
88    return 0;
89 }
90 //]
91 #include <boost/interprocess/detail/config_end.hpp>