annotate DEPENDENCIES/generic/include/boost/interprocess/allocators/node_allocator.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_INTERPROCESS_NODE_ALLOCATOR_HPP
Chris@16 12 #define BOOST_INTERPROCESS_NODE_ALLOCATOR_HPP
Chris@16 13
Chris@101 14 #ifndef BOOST_CONFIG_HPP
Chris@101 15 # include <boost/config.hpp>
Chris@101 16 #endif
Chris@101 17 #
Chris@101 18 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@16 19 # pragma once
Chris@16 20 #endif
Chris@16 21
Chris@16 22 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 23 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 24
Chris@16 25 #include <boost/intrusive/pointer_traits.hpp>
Chris@16 26
Chris@16 27 #include <boost/interprocess/interprocess_fwd.hpp>
Chris@16 28 #include <boost/assert.hpp>
Chris@16 29 #include <boost/utility/addressof.hpp>
Chris@16 30 #include <boost/interprocess/detail/utilities.hpp>
Chris@16 31 #include <boost/interprocess/detail/type_traits.hpp>
Chris@16 32 #include <boost/interprocess/allocators/detail/node_pool.hpp>
Chris@101 33 #include <boost/interprocess/containers/version_type.hpp>
Chris@16 34 #include <boost/container/detail/multiallocation_chain.hpp>
Chris@16 35 #include <boost/interprocess/exceptions.hpp>
Chris@16 36 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
Chris@101 37 #include <boost/move/adl_move_swap.hpp>
Chris@16 38 #include <cstddef>
Chris@16 39
Chris@16 40 //!\file
Chris@16 41 //!Describes node_allocator pooled shared memory STL compatible allocator
Chris@16 42
Chris@16 43 namespace boost {
Chris@16 44 namespace interprocess {
Chris@16 45
Chris@101 46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 47
Chris@16 48 namespace ipcdetail{
Chris@16 49
Chris@16 50 template < unsigned int Version
Chris@16 51 , class T
Chris@16 52 , class SegmentManager
Chris@16 53 , std::size_t NodesPerBlock
Chris@16 54 >
Chris@16 55 class node_allocator_base
Chris@16 56 : public node_pool_allocation_impl
Chris@16 57 < node_allocator_base
Chris@16 58 < Version, T, SegmentManager, NodesPerBlock>
Chris@16 59 , Version
Chris@16 60 , T
Chris@16 61 , SegmentManager
Chris@16 62 >
Chris@16 63 {
Chris@16 64 public:
Chris@16 65 typedef typename SegmentManager::void_pointer void_pointer;
Chris@16 66 typedef SegmentManager segment_manager;
Chris@16 67 typedef node_allocator_base
Chris@16 68 <Version, T, SegmentManager, NodesPerBlock> self_t;
Chris@16 69
Chris@101 70 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 71
Chris@16 72 template <int dummy>
Chris@16 73 struct node_pool
Chris@16 74 {
Chris@16 75 typedef ipcdetail::shared_node_pool
Chris@16 76 < SegmentManager, sizeof_value<T>::value, NodesPerBlock> type;
Chris@16 77
Chris@16 78 static type *get(void *p)
Chris@16 79 { return static_cast<type*>(p); }
Chris@16 80 };
Chris@101 81 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 82
Chris@16 83 BOOST_STATIC_ASSERT((Version <=2));
Chris@16 84
Chris@16 85 public:
Chris@16 86 //-------
Chris@16 87 typedef typename boost::intrusive::
Chris@16 88 pointer_traits<void_pointer>::template
Chris@16 89 rebind_pointer<T>::type pointer;
Chris@16 90 typedef typename boost::intrusive::
Chris@16 91 pointer_traits<void_pointer>::template
Chris@16 92 rebind_pointer<const T>::type const_pointer;
Chris@16 93 typedef T value_type;
Chris@16 94 typedef typename ipcdetail::add_reference
Chris@16 95 <value_type>::type reference;
Chris@16 96 typedef typename ipcdetail::add_reference
Chris@16 97 <const value_type>::type const_reference;
Chris@16 98 typedef typename segment_manager::size_type size_type;
Chris@16 99 typedef typename segment_manager::difference_type difference_type;
Chris@16 100
Chris@16 101 typedef boost::interprocess::version_type<node_allocator_base, Version> version;
Chris@16 102 typedef boost::container::container_detail::transform_multiallocation_chain
Chris@16 103 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
Chris@16 104
Chris@16 105 //!Obtains node_allocator_base from
Chris@16 106 //!node_allocator_base
Chris@16 107 template<class T2>
Chris@16 108 struct rebind
Chris@16 109 {
Chris@16 110 typedef node_allocator_base<Version, T2, SegmentManager, NodesPerBlock> other;
Chris@16 111 };
Chris@16 112
Chris@101 113 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 114 private:
Chris@16 115 //!Not assignable from related node_allocator_base
Chris@16 116 template<unsigned int Version2, class T2, class SegmentManager2, std::size_t N2>
Chris@16 117 node_allocator_base& operator=
Chris@16 118 (const node_allocator_base<Version2, T2, SegmentManager2, N2>&);
Chris@16 119
Chris@16 120 //!Not assignable from other node_allocator_base
Chris@16 121 //node_allocator_base& operator=(const node_allocator_base&);
Chris@101 122 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 123
Chris@16 124 public:
Chris@16 125 //!Constructor from a segment manager. If not present, constructs a node
Chris@16 126 //!pool. Increments the reference count of the associated node pool.
Chris@16 127 //!Can throw boost::interprocess::bad_alloc
Chris@16 128 node_allocator_base(segment_manager *segment_mngr)
Chris@16 129 : mp_node_pool(ipcdetail::get_or_create_node_pool<typename node_pool<0>::type>(segment_mngr)) { }
Chris@16 130
Chris@16 131 //!Copy constructor from other node_allocator_base. Increments the reference
Chris@16 132 //!count of the associated node pool. Never throws
Chris@16 133 node_allocator_base(const node_allocator_base &other)
Chris@16 134 : mp_node_pool(other.get_node_pool())
Chris@16 135 {
Chris@16 136 node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->inc_ref_count();
Chris@16 137 }
Chris@16 138
Chris@16 139 //!Copy constructor from related node_allocator_base. If not present, constructs
Chris@16 140 //!a node pool. Increments the reference count of the associated node pool.
Chris@16 141 //!Can throw boost::interprocess::bad_alloc
Chris@16 142 template<class T2>
Chris@16 143 node_allocator_base
Chris@16 144 (const node_allocator_base<Version, T2, SegmentManager, NodesPerBlock> &other)
Chris@16 145 : mp_node_pool(ipcdetail::get_or_create_node_pool<typename node_pool<0>::type>(other.get_segment_manager())) { }
Chris@16 146
Chris@16 147 //!Assignment from other node_allocator_base
Chris@16 148 node_allocator_base& operator=(const node_allocator_base &other)
Chris@16 149 {
Chris@16 150 node_allocator_base c(other);
Chris@101 151 boost::adl_move_swap(*this, c);
Chris@16 152 return *this;
Chris@16 153 }
Chris@16 154
Chris@16 155 //!Destructor, removes node_pool_t from memory
Chris@16 156 //!if its reference count reaches to zero. Never throws
Chris@16 157 ~node_allocator_base()
Chris@16 158 { ipcdetail::destroy_node_pool_if_last_link(node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))); }
Chris@16 159
Chris@16 160 //!Returns a pointer to the node pool.
Chris@16 161 //!Never throws
Chris@16 162 void* get_node_pool() const
Chris@16 163 { return ipcdetail::to_raw_pointer(mp_node_pool); }
Chris@16 164
Chris@16 165 //!Returns the segment manager.
Chris@16 166 //!Never throws
Chris@16 167 segment_manager* get_segment_manager()const
Chris@16 168 { return node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->get_segment_manager(); }
Chris@16 169
Chris@16 170 //!Swaps allocators. Does not throw. If each allocator is placed in a
Chris@16 171 //!different memory segment, the result is undefined.
Chris@16 172 friend void swap(self_t &alloc1, self_t &alloc2)
Chris@101 173 { boost::adl_move_swap(alloc1.mp_node_pool, alloc2.mp_node_pool); }
Chris@16 174
Chris@101 175 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 176 private:
Chris@16 177 void_pointer mp_node_pool;
Chris@101 178 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 179 };
Chris@16 180
Chris@16 181 //!Equality test for same type
Chris@16 182 //!of node_allocator_base
Chris@16 183 template<unsigned int V, class T, class S, std::size_t NPC> inline
Chris@16 184 bool operator==(const node_allocator_base<V, T, S, NPC> &alloc1,
Chris@16 185 const node_allocator_base<V, T, S, NPC> &alloc2)
Chris@16 186 { return alloc1.get_node_pool() == alloc2.get_node_pool(); }
Chris@16 187
Chris@16 188 //!Inequality test for same type
Chris@16 189 //!of node_allocator_base
Chris@16 190 template<unsigned int V, class T, class S, std::size_t NPC> inline
Chris@16 191 bool operator!=(const node_allocator_base<V, T, S, NPC> &alloc1,
Chris@16 192 const node_allocator_base<V, T, S, NPC> &alloc2)
Chris@16 193 { return alloc1.get_node_pool() != alloc2.get_node_pool(); }
Chris@16 194
Chris@16 195 template < class T
Chris@16 196 , class SegmentManager
Chris@16 197 , std::size_t NodesPerBlock = 64
Chris@16 198 >
Chris@16 199 class node_allocator_v1
Chris@16 200 : public node_allocator_base
Chris@16 201 < 1
Chris@16 202 , T
Chris@16 203 , SegmentManager
Chris@16 204 , NodesPerBlock
Chris@16 205 >
Chris@16 206 {
Chris@16 207 public:
Chris@16 208 typedef ipcdetail::node_allocator_base
Chris@16 209 < 1, T, SegmentManager, NodesPerBlock> base_t;
Chris@16 210
Chris@16 211 template<class T2>
Chris@16 212 struct rebind
Chris@16 213 {
Chris@16 214 typedef node_allocator_v1<T2, SegmentManager, NodesPerBlock> other;
Chris@16 215 };
Chris@16 216
Chris@16 217 node_allocator_v1(SegmentManager *segment_mngr)
Chris@16 218 : base_t(segment_mngr)
Chris@16 219 {}
Chris@16 220
Chris@16 221 template<class T2>
Chris@16 222 node_allocator_v1
Chris@16 223 (const node_allocator_v1<T2, SegmentManager, NodesPerBlock> &other)
Chris@16 224 : base_t(other)
Chris@16 225 {}
Chris@16 226 };
Chris@16 227
Chris@16 228 } //namespace ipcdetail{
Chris@16 229
Chris@101 230 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 231
Chris@16 232 //!An STL node allocator that uses a segment manager as memory
Chris@16 233 //!source. The internal pointer type will of the same type (raw, smart) as
Chris@16 234 //!"typename SegmentManager::void_pointer" type. This allows
Chris@16 235 //!placing the allocator in shared memory, memory mapped-files, etc...
Chris@16 236 //!This node allocator shares a segregated storage between all instances
Chris@16 237 //!of node_allocator with equal sizeof(T) placed in the same segment
Chris@16 238 //!group. NodesPerBlock is the number of nodes allocated at once when the allocator
Chris@101 239 //!runs out of nodes
Chris@16 240 template < class T
Chris@16 241 , class SegmentManager
Chris@16 242 , std::size_t NodesPerBlock
Chris@16 243 >
Chris@16 244 class node_allocator
Chris@101 245 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Chris@16 246 : public ipcdetail::node_allocator_base
Chris@16 247 < 2
Chris@16 248 , T
Chris@16 249 , SegmentManager
Chris@16 250 , NodesPerBlock
Chris@16 251 >
Chris@101 252 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 253 {
Chris@16 254
Chris@16 255 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 256 typedef ipcdetail::node_allocator_base
Chris@16 257 < 2, T, SegmentManager, NodesPerBlock> base_t;
Chris@16 258 public:
Chris@16 259 typedef boost::interprocess::version_type<node_allocator, 2> version;
Chris@16 260
Chris@16 261 template<class T2>
Chris@16 262 struct rebind
Chris@16 263 {
Chris@16 264 typedef node_allocator<T2, SegmentManager, NodesPerBlock> other;
Chris@16 265 };
Chris@16 266
Chris@16 267 node_allocator(SegmentManager *segment_mngr)
Chris@16 268 : base_t(segment_mngr)
Chris@16 269 {}
Chris@16 270
Chris@16 271 template<class T2>
Chris@16 272 node_allocator
Chris@16 273 (const node_allocator<T2, SegmentManager, NodesPerBlock> &other)
Chris@16 274 : base_t(other)
Chris@16 275 {}
Chris@16 276
Chris@16 277 #else //BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 278 public:
Chris@16 279 typedef implementation_defined::segment_manager segment_manager;
Chris@16 280 typedef segment_manager::void_pointer void_pointer;
Chris@16 281 typedef implementation_defined::pointer pointer;
Chris@16 282 typedef implementation_defined::const_pointer const_pointer;
Chris@16 283 typedef T value_type;
Chris@16 284 typedef typename ipcdetail::add_reference
Chris@16 285 <value_type>::type reference;
Chris@16 286 typedef typename ipcdetail::add_reference
Chris@16 287 <const value_type>::type const_reference;
Chris@16 288 typedef typename segment_manager::size_type size_type;
Chris@16 289 typedef typename segment_manager::difference_type difference_type;
Chris@16 290
Chris@16 291 //!Obtains node_allocator from
Chris@16 292 //!node_allocator
Chris@16 293 template<class T2>
Chris@16 294 struct rebind
Chris@16 295 {
Chris@16 296 typedef node_allocator<T2, SegmentManager, NodesPerBlock> other;
Chris@16 297 };
Chris@16 298
Chris@16 299 private:
Chris@16 300 //!Not assignable from
Chris@16 301 //!related node_allocator
Chris@16 302 template<class T2, class SegmentManager2, std::size_t N2>
Chris@16 303 node_allocator& operator=
Chris@16 304 (const node_allocator<T2, SegmentManager2, N2>&);
Chris@16 305
Chris@16 306 //!Not assignable from
Chris@16 307 //!other node_allocator
Chris@16 308 //node_allocator& operator=(const node_allocator&);
Chris@16 309
Chris@16 310 public:
Chris@16 311 //!Constructor from a segment manager. If not present, constructs a node
Chris@16 312 //!pool. Increments the reference count of the associated node pool.
Chris@16 313 //!Can throw boost::interprocess::bad_alloc
Chris@16 314 node_allocator(segment_manager *segment_mngr);
Chris@16 315
Chris@16 316 //!Copy constructor from other node_allocator. Increments the reference
Chris@16 317 //!count of the associated node pool. Never throws
Chris@16 318 node_allocator(const node_allocator &other);
Chris@16 319
Chris@16 320 //!Copy constructor from related node_allocator. If not present, constructs
Chris@16 321 //!a node pool. Increments the reference count of the associated node pool.
Chris@16 322 //!Can throw boost::interprocess::bad_alloc
Chris@16 323 template<class T2>
Chris@16 324 node_allocator
Chris@16 325 (const node_allocator<T2, SegmentManager, NodesPerBlock> &other);
Chris@16 326
Chris@16 327 //!Destructor, removes node_pool_t from memory
Chris@16 328 //!if its reference count reaches to zero. Never throws
Chris@16 329 ~node_allocator();
Chris@16 330
Chris@16 331 //!Returns a pointer to the node pool.
Chris@16 332 //!Never throws
Chris@16 333 void* get_node_pool() const;
Chris@16 334
Chris@16 335 //!Returns the segment manager.
Chris@16 336 //!Never throws
Chris@16 337 segment_manager* get_segment_manager()const;
Chris@16 338
Chris@16 339 //!Returns the number of elements that could be allocated.
Chris@16 340 //!Never throws
Chris@16 341 size_type max_size() const;
Chris@16 342
Chris@16 343 //!Allocate memory for an array of count elements.
Chris@16 344 //!Throws boost::interprocess::bad_alloc if there is no enough memory
Chris@16 345 pointer allocate(size_type count, cvoid_pointer hint = 0);
Chris@16 346
Chris@16 347 //!Deallocate allocated memory.
Chris@16 348 //!Never throws
Chris@16 349 void deallocate(const pointer &ptr, size_type count);
Chris@16 350
Chris@16 351 //!Deallocates all free blocks
Chris@16 352 //!of the pool
Chris@16 353 void deallocate_free_blocks();
Chris@16 354
Chris@16 355 //!Swaps allocators. Does not throw. If each allocator is placed in a
Chris@16 356 //!different memory segment, the result is undefined.
Chris@16 357 friend void swap(self_t &alloc1, self_t &alloc2);
Chris@16 358
Chris@16 359 //!Returns address of mutable object.
Chris@16 360 //!Never throws
Chris@16 361 pointer address(reference value) const;
Chris@16 362
Chris@16 363 //!Returns address of non mutable object.
Chris@16 364 //!Never throws
Chris@16 365 const_pointer address(const_reference value) const;
Chris@16 366
Chris@16 367 //!Copy construct an object.
Chris@16 368 //!Throws if T's copy constructor throws
Chris@16 369 void construct(const pointer &ptr, const_reference v);
Chris@16 370
Chris@16 371 //!Destroys object. Throws if object's
Chris@16 372 //!destructor throws
Chris@16 373 void destroy(const pointer &ptr);
Chris@16 374
Chris@16 375 //!Returns maximum the number of objects the previously allocated memory
Chris@16 376 //!pointed by p can hold. This size only works for memory allocated with
Chris@16 377 //!allocate, allocation_command and allocate_many.
Chris@16 378 size_type size(const pointer &p) const;
Chris@16 379
Chris@101 380 pointer allocation_command(boost::interprocess::allocation_type command,
Chris@101 381 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
Chris@16 382
Chris@16 383 //!Allocates many elements of size elem_size in a contiguous block
Chris@16 384 //!of memory. The minimum number to be allocated is min_elements,
Chris@16 385 //!the preferred and maximum number is
Chris@16 386 //!preferred_elements. The number of actually allocated elements is
Chris@16 387 //!will be assigned to received_size. The elements must be deallocated
Chris@16 388 //!with deallocate(...)
Chris@16 389 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
Chris@16 390
Chris@16 391 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
Chris@16 392 //!contiguous block
Chris@16 393 //!of memory. The elements must be deallocated
Chris@16 394 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
Chris@16 395
Chris@16 396 //!Allocates many elements of size elem_size in a contiguous block
Chris@16 397 //!of memory. The minimum number to be allocated is min_elements,
Chris@16 398 //!the preferred and maximum number is
Chris@16 399 //!preferred_elements. The number of actually allocated elements is
Chris@16 400 //!will be assigned to received_size. The elements must be deallocated
Chris@16 401 //!with deallocate(...)
Chris@16 402 void deallocate_many(multiallocation_chain &chain);
Chris@16 403
Chris@16 404 //!Allocates just one object. Memory allocated with this function
Chris@16 405 //!must be deallocated only with deallocate_one().
Chris@16 406 //!Throws boost::interprocess::bad_alloc if there is no enough memory
Chris@16 407 pointer allocate_one();
Chris@16 408
Chris@16 409 //!Allocates many elements of size == 1 in a contiguous block
Chris@16 410 //!of memory. The minimum number to be allocated is min_elements,
Chris@16 411 //!the preferred and maximum number is
Chris@16 412 //!preferred_elements. The number of actually allocated elements is
Chris@16 413 //!will be assigned to received_size. Memory allocated with this function
Chris@16 414 //!must be deallocated only with deallocate_one().
Chris@16 415 void allocate_individual(size_type num_elements, multiallocation_chain &chain);
Chris@16 416
Chris@16 417 //!Deallocates memory previously allocated with allocate_one().
Chris@16 418 //!You should never use deallocate_one to deallocate memory allocated
Chris@16 419 //!with other functions different from allocate_one(). Never throws
Chris@16 420 void deallocate_one(const pointer &p);
Chris@16 421
Chris@16 422 //!Allocates many elements of size == 1 in a contiguous block
Chris@16 423 //!of memory. The minimum number to be allocated is min_elements,
Chris@16 424 //!the preferred and maximum number is
Chris@16 425 //!preferred_elements. The number of actually allocated elements is
Chris@16 426 //!will be assigned to received_size. Memory allocated with this function
Chris@16 427 //!must be deallocated only with deallocate_one().
Chris@16 428 void deallocate_individual(multiallocation_chain &chain);
Chris@16 429 #endif
Chris@16 430 };
Chris@16 431
Chris@16 432 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Chris@16 433
Chris@16 434 //!Equality test for same type
Chris@16 435 //!of node_allocator
Chris@16 436 template<class T, class S, std::size_t NPC> inline
Chris@16 437 bool operator==(const node_allocator<T, S, NPC> &alloc1,
Chris@16 438 const node_allocator<T, S, NPC> &alloc2);
Chris@16 439
Chris@16 440 //!Inequality test for same type
Chris@16 441 //!of node_allocator
Chris@16 442 template<class T, class S, std::size_t NPC> inline
Chris@16 443 bool operator!=(const node_allocator<T, S, NPC> &alloc1,
Chris@16 444 const node_allocator<T, S, NPC> &alloc2);
Chris@16 445
Chris@16 446 #endif
Chris@16 447
Chris@16 448 } //namespace interprocess {
Chris@16 449 } //namespace boost {
Chris@16 450
Chris@16 451 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 452
Chris@16 453 #endif //#ifndef BOOST_INTERPROCESS_NODE_ALLOCATOR_HPP