1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-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)
7 // See http://www.boost.org/libs/interprocess for documentation.
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>
17 #include "../test/get_process_id_name.hpp"
20 using namespace boost::interprocess;
24 //Remove shared memory on construction and destruction
29 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
30 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
33 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
34 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
43 //Create shared memory
46 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
49 managed_shared_memory segment(create_only,
50 "MySharedMemory", //segment name
56 //Create a cached_node_allocator that allocates ints from the managed segment
57 //The number of chunks per segment is the default value
58 typedef cached_node_allocator<int, managed_shared_memory::segment_manager>
59 cached_node_allocator_t;
60 cached_node_allocator_t allocator_instance(segment.get_segment_manager());
62 //The max cached nodes are configurable per instance
63 allocator_instance.set_max_cached_nodes(3);
65 //Create another cached_node_allocator. Since the segment manager address
66 //is the same, this cached_node_allocator will be
67 //attached to the same pool so "allocator_instance2" can deallocate
68 //nodes allocated by "allocator_instance"
69 cached_node_allocator_t allocator_instance2(segment.get_segment_manager());
71 //The max cached nodes are configurable per instance
72 allocator_instance2.set_max_cached_nodes(5);
74 //Create another cached_node_allocator using copy-constructor. This
75 //cached_node_allocator will also be attached to the same pool
76 cached_node_allocator_t allocator_instance3(allocator_instance2);
78 //We can clear the cache
79 allocator_instance3.deallocate_cache();
81 //All allocators are equal
82 assert(allocator_instance == allocator_instance2);
83 assert(allocator_instance2 == allocator_instance3);
85 //So memory allocated with one can be deallocated with another
86 allocator_instance2.deallocate(allocator_instance.allocate(1), 1);
87 allocator_instance3.deallocate(allocator_instance2.allocate(1), 1);
89 //The common pool will be destroyed here, since no allocator is
90 //attached to the pool
94 #include <boost/interprocess/detail/config_end.hpp>