]> git.sesse.net Git - casparcg/blob - dependencies64/boost/boost/interprocess/allocators/cached_node_allocator.hpp
Updated boost. Separate commit from the code changes. (So this revision will not...
[casparcg] / dependencies64 / boost / boost / interprocess / allocators / cached_node_allocator.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-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 #ifndef BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP
12 #define BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20
21 #include <boost/interprocess/interprocess_fwd.hpp>
22 #include <boost/interprocess/allocators/detail/node_pool.hpp>
23 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
24 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/interprocess/detail/utilities.hpp>
26 #include <boost/interprocess/containers/version_type.hpp>
27 #include <boost/interprocess/allocators/detail/node_tools.hpp>
28 #include <cstddef>
29
30 //!\file
31 //!Describes cached_cached_node_allocator pooled shared memory STL compatible allocator
32
33 namespace boost {
34 namespace interprocess {
35
36
37 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
38
39 namespace ipcdetail {
40
41 template < class T
42          , class SegmentManager
43          , std::size_t NodesPerBlock = 64
44          >
45 class cached_node_allocator_v1
46    :  public ipcdetail::cached_allocator_impl
47          < T
48          , ipcdetail::shared_node_pool
49             < SegmentManager
50             , sizeof_value<T>::value
51             , NodesPerBlock
52             >
53          , 1>
54 {
55    public:
56    typedef ipcdetail::cached_allocator_impl
57          < T
58          , ipcdetail::shared_node_pool
59             < SegmentManager
60             , sizeof_value<T>::value
61             , NodesPerBlock
62             >
63          , 1> base_t;
64
65    template<class T2>
66    struct rebind
67    {
68       typedef cached_node_allocator_v1
69          <T2, SegmentManager, NodesPerBlock>  other;
70    };
71
72    typedef typename base_t::size_type size_type;
73
74    cached_node_allocator_v1(SegmentManager *segment_mngr,
75                          size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
76       : base_t(segment_mngr, max_cached_nodes)
77    {}
78
79    template<class T2>
80    cached_node_allocator_v1
81       (const cached_node_allocator_v1
82          <T2, SegmentManager, NodesPerBlock> &other)
83       : base_t(other)
84    {}
85 };
86
87 }  //namespace ipcdetail{
88
89 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
90
91 template < class T
92          , class SegmentManager
93          , std::size_t NodesPerBlock
94          >
95 class cached_node_allocator
96    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
97    :  public ipcdetail::cached_allocator_impl
98          < T
99          , ipcdetail::shared_node_pool
100             < SegmentManager
101             , sizeof_value<T>::value
102             , NodesPerBlock
103             >
104          , 2>
105    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
106 {
107
108    #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
109    public:
110    typedef ipcdetail::cached_allocator_impl
111          < T
112          , ipcdetail::shared_node_pool
113             < SegmentManager
114             , sizeof_value<T>::value
115             , NodesPerBlock
116             >
117          , 2> base_t;
118
119    public:
120    typedef boost::interprocess::version_type<cached_node_allocator, 2>   version;
121    typedef typename base_t::size_type size_type;
122
123    template<class T2>
124    struct rebind
125    {
126       typedef cached_node_allocator<T2, SegmentManager, NodesPerBlock>  other;
127    };
128
129    cached_node_allocator(SegmentManager *segment_mngr,
130                          size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
131       : base_t(segment_mngr, max_cached_nodes)
132    {}
133
134    template<class T2>
135    cached_node_allocator
136       (const cached_node_allocator<T2, SegmentManager, NodesPerBlock> &other)
137       : base_t(other)
138    {}
139
140    #else
141    public:
142    typedef implementation_defined::segment_manager       segment_manager;
143    typedef segment_manager::void_pointer                 void_pointer;
144    typedef implementation_defined::pointer               pointer;
145    typedef implementation_defined::const_pointer         const_pointer;
146    typedef T                                             value_type;
147    typedef typename ipcdetail::add_reference
148                      <value_type>::type                  reference;
149    typedef typename ipcdetail::add_reference
150                      <const value_type>::type            const_reference;
151    typedef typename SegmentManager::size_type            size_type;
152    typedef typename SegmentManager::difference_type      difference_type;
153
154    //!Obtains cached_node_allocator from
155    //!cached_node_allocator
156    template<class T2>
157    struct rebind
158    {
159       typedef cached_node_allocator<T2, SegmentManager> other;
160    };
161
162    private:
163    //!Not assignable from
164    //!related cached_node_allocator
165    template<class T2, class SegmentManager2, std::size_t N2>
166    cached_node_allocator& operator=
167       (const cached_node_allocator<T2, SegmentManager2, N2>&);
168
169    //!Not assignable from
170    //!other cached_node_allocator
171    cached_node_allocator& operator=(const cached_node_allocator&);
172
173    public:
174    //!Constructor from a segment manager. If not present, constructs a node
175    //!pool. Increments the reference count of the associated node pool.
176    //!Can throw boost::interprocess::bad_alloc
177    cached_node_allocator(segment_manager *segment_mngr);
178
179    //!Copy constructor from other cached_node_allocator. Increments the reference
180    //!count of the associated node pool. Never throws
181    cached_node_allocator(const cached_node_allocator &other);
182
183    //!Copy constructor from related cached_node_allocator. If not present, constructs
184    //!a node pool. Increments the reference count of the associated node pool.
185    //!Can throw boost::interprocess::bad_alloc
186    template<class T2>
187    cached_node_allocator
188       (const cached_node_allocator<T2, SegmentManager, NodesPerBlock> &other);
189
190    //!Destructor, removes node_pool_t from memory
191    //!if its reference count reaches to zero. Never throws
192    ~cached_node_allocator();
193
194    //!Returns a pointer to the node pool.
195    //!Never throws
196    node_pool_t* get_node_pool() const;
197
198    //!Returns the segment manager.
199    //!Never throws
200    segment_manager* get_segment_manager()const;
201
202    //!Returns the number of elements that could be allocated.
203    //!Never throws
204    size_type max_size() const;
205
206    //!Allocate memory for an array of count elements.
207    //!Throws boost::interprocess::bad_alloc if there is no enough memory
208    pointer allocate(size_type count, cvoid_pointer hint = 0);
209
210    //!Deallocate allocated memory.
211    //!Never throws
212    void deallocate(const pointer &ptr, size_type count);
213
214    //!Deallocates all free blocks
215    //!of the pool
216    void deallocate_free_blocks();
217
218    //!Swaps allocators. Does not throw. If each allocator is placed in a
219    //!different memory segment, the result is undefined.
220    friend void swap(self_t &alloc1, self_t &alloc2);
221
222    //!Returns address of mutable object.
223    //!Never throws
224    pointer address(reference value) const;
225
226    //!Returns address of non mutable object.
227    //!Never throws
228    const_pointer address(const_reference value) const;
229
230    //!Default construct an object.
231    //!Throws if T's default constructor throws
232    void construct(const pointer &ptr, const_reference v);
233
234    //!Destroys object. Throws if object's
235    //!destructor throws
236    void destroy(const pointer &ptr);
237
238    //!Returns maximum the number of objects the previously allocated memory
239    //!pointed by p can hold. This size only works for memory allocated with
240    //!allocate, allocation_command and allocate_many.
241    size_type size(const pointer &p) const;
242
243    std::pair<pointer, bool>
244       allocation_command(boost::interprocess::allocation_type command,
245                          size_type limit_size,
246                          size_type preferred_size,
247                          size_type &received_size, const pointer &reuse = 0);
248
249    //!Allocates many elements of size elem_size in a contiguous block
250    //!of memory. The minimum number to be allocated is min_elements,
251    //!the preferred and maximum number is
252    //!preferred_elements. The number of actually allocated elements is
253    //!will be assigned to received_size. The elements must be deallocated
254    //!with deallocate(...)
255    void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
256
257    //!Allocates n_elements elements, each one of size elem_sizes[i]in a
258    //!contiguous block
259    //!of memory. The elements must be deallocated
260    void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
261
262    //!Allocates many elements of size elem_size in a contiguous block
263    //!of memory. The minimum number to be allocated is min_elements,
264    //!the preferred and maximum number is
265    //!preferred_elements. The number of actually allocated elements is
266    //!will be assigned to received_size. The elements must be deallocated
267    //!with deallocate(...)
268    void deallocate_many(multiallocation_chain &chain);
269
270    //!Allocates just one object. Memory allocated with this function
271    //!must be deallocated only with deallocate_one().
272    //!Throws boost::interprocess::bad_alloc if there is no enough memory
273    pointer allocate_one();
274
275    //!Allocates many elements of size == 1 in a contiguous block
276    //!of memory. The minimum number to be allocated is min_elements,
277    //!the preferred and maximum number is
278    //!preferred_elements. The number of actually allocated elements is
279    //!will be assigned to received_size. Memory allocated with this function
280    //!must be deallocated only with deallocate_one().
281    multiallocation_chain allocate_individual(size_type num_elements);
282
283    //!Deallocates memory previously allocated with allocate_one().
284    //!You should never use deallocate_one to deallocate memory allocated
285    //!with other functions different from allocate_one(). Never throws
286    void deallocate_one(const pointer &p);
287
288    //!Allocates many elements of size == 1 in a contiguous block
289    //!of memory. The minimum number to be allocated is min_elements,
290    //!the preferred and maximum number is
291    //!preferred_elements. The number of actually allocated elements is
292    //!will be assigned to received_size. Memory allocated with this function
293    //!must be deallocated only with deallocate_one().
294    void deallocate_individual(multiallocation_chain it);
295    //!Sets the new max cached nodes value. This can provoke deallocations
296    //!if "newmax" is less than current cached nodes. Never throws
297    void set_max_cached_nodes(size_type newmax);
298
299    //!Returns the max cached nodes parameter.
300    //!Never throws
301    size_type get_max_cached_nodes() const;
302    #endif
303 };
304
305 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
306
307 //!Equality test for same type
308 //!of cached_node_allocator
309 template<class T, class S, std::size_t NPC> inline
310 bool operator==(const cached_node_allocator<T, S, NPC> &alloc1,
311                 const cached_node_allocator<T, S, NPC> &alloc2);
312
313 //!Inequality test for same type
314 //!of cached_node_allocator
315 template<class T, class S, std::size_t NPC> inline
316 bool operator!=(const cached_node_allocator<T, S, NPC> &alloc1,
317                 const cached_node_allocator<T, S, NPC> &alloc2);
318
319 #endif
320
321 }  //namespace interprocess {
322 }  //namespace boost {
323
324 #include <boost/interprocess/detail/config_end.hpp>
325
326 #endif   //#ifndef BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP
327