allocator_common.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-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. #ifndef BOOST_INTERPROCESS_ALLOCATOR_DETAIL_ALLOCATOR_COMMON_HPP
  11. #define BOOST_INTERPROCESS_ALLOCATOR_DETAIL_ALLOCATOR_COMMON_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/intrusive/pointer_traits.hpp>
  22. #include <boost/interprocess/interprocess_fwd.hpp>
  23. #include <boost/interprocess/detail/utilities.hpp> //to_raw_pointer
  24. #include <boost/utility/addressof.hpp> //boost::addressof
  25. #include <boost/assert.hpp> //BOOST_ASSERT
  26. #include <boost/interprocess/exceptions.hpp> //bad_alloc
  27. #include <boost/interprocess/sync/scoped_lock.hpp> //scoped_lock
  28. #include <boost/interprocess/containers/allocation_type.hpp> //boost::interprocess::allocation_type
  29. #include <boost/container/detail/multiallocation_chain.hpp>
  30. #include <boost/interprocess/mem_algo/detail/mem_algo_common.hpp>
  31. #include <boost/interprocess/detail/segment_manager_helper.hpp>
  32. #include <boost/move/utility_core.hpp>
  33. #include <boost/interprocess/detail/type_traits.hpp>
  34. #include <boost/interprocess/detail/utilities.hpp>
  35. #include <boost/container/detail/placement_new.hpp>
  36. #include <boost/move/adl_move_swap.hpp>
  37. namespace boost {
  38. namespace interprocess {
  39. template <class T>
  40. struct sizeof_value
  41. {
  42. static const std::size_t value = sizeof(T);
  43. };
  44. template <>
  45. struct sizeof_value<void>
  46. {
  47. static const std::size_t value = sizeof(void*);
  48. };
  49. template <>
  50. struct sizeof_value<const void>
  51. {
  52. static const std::size_t value = sizeof(void*);
  53. };
  54. template <>
  55. struct sizeof_value<volatile void>
  56. {
  57. static const std::size_t value = sizeof(void*);
  58. };
  59. template <>
  60. struct sizeof_value<const volatile void>
  61. {
  62. static const std::size_t value = sizeof(void*);
  63. };
  64. namespace ipcdetail {
  65. //!Object function that creates the node allocator if it is not created and
  66. //!increments reference count if it is already created
  67. template<class NodePool>
  68. struct get_or_create_node_pool_func
  69. {
  70. //!This connects or constructs the unique instance of node_pool_t
  71. //!Can throw boost::interprocess::bad_alloc
  72. void operator()()
  73. {
  74. //Find or create the node_pool_t
  75. mp_node_pool = mp_segment_manager->template find_or_construct
  76. <NodePool>(boost::interprocess::unique_instance)(mp_segment_manager);
  77. //If valid, increment link count
  78. if(mp_node_pool != 0)
  79. mp_node_pool->inc_ref_count();
  80. }
  81. //!Constructor. Initializes function
  82. //!object parameters
  83. get_or_create_node_pool_func(typename NodePool::segment_manager *mngr)
  84. : mp_segment_manager(mngr){}
  85. NodePool *mp_node_pool;
  86. typename NodePool::segment_manager *mp_segment_manager;
  87. };
  88. template<class NodePool>
  89. inline NodePool *get_or_create_node_pool(typename NodePool::segment_manager *mgnr)
  90. {
  91. ipcdetail::get_or_create_node_pool_func<NodePool> func(mgnr);
  92. mgnr->atomic_func(func);
  93. return func.mp_node_pool;
  94. }
  95. //!Object function that decrements the reference count. If the count
  96. //!reaches to zero destroys the node allocator from memory.
  97. //!Never throws
  98. template<class NodePool>
  99. struct destroy_if_last_link_func
  100. {
  101. //!Decrements reference count and destroys the object if there is no
  102. //!more attached allocators. Never throws
  103. void operator()()
  104. {
  105. //If not the last link return
  106. if(mp_node_pool->dec_ref_count() != 0) return;
  107. //Last link, let's destroy the segment_manager
  108. mp_node_pool->get_segment_manager()->template destroy<NodePool>(boost::interprocess::unique_instance);
  109. }
  110. //!Constructor. Initializes function
  111. //!object parameters
  112. destroy_if_last_link_func(NodePool *pool)
  113. : mp_node_pool(pool)
  114. {}
  115. NodePool *mp_node_pool;
  116. };
  117. //!Destruction function, initializes and executes destruction function
  118. //!object. Never throws
  119. template<class NodePool>
  120. inline void destroy_node_pool_if_last_link(NodePool *pool)
  121. {
  122. //Get segment manager
  123. typename NodePool::segment_manager *mngr = pool->get_segment_manager();
  124. //Execute destruction functor atomically
  125. destroy_if_last_link_func<NodePool>func(pool);
  126. mngr->atomic_func(func);
  127. }
  128. template<class NodePool>
  129. class cache_impl
  130. {
  131. typedef typename NodePool::segment_manager::
  132. void_pointer void_pointer;
  133. typedef typename boost::intrusive::
  134. pointer_traits<void_pointer>::template
  135. rebind_pointer<NodePool>::type node_pool_ptr;
  136. typedef typename NodePool::multiallocation_chain multiallocation_chain;
  137. typedef typename NodePool::segment_manager::size_type size_type;
  138. node_pool_ptr mp_node_pool;
  139. multiallocation_chain m_cached_nodes;
  140. size_type m_max_cached_nodes;
  141. public:
  142. typedef typename NodePool::segment_manager segment_manager;
  143. cache_impl(segment_manager *segment_mngr, size_type max_cached_nodes)
  144. : mp_node_pool(get_or_create_node_pool<NodePool>(segment_mngr))
  145. , m_max_cached_nodes(max_cached_nodes)
  146. {}
  147. cache_impl(const cache_impl &other)
  148. : mp_node_pool(other.get_node_pool())
  149. , m_max_cached_nodes(other.get_max_cached_nodes())
  150. {
  151. mp_node_pool->inc_ref_count();
  152. }
  153. ~cache_impl()
  154. {
  155. this->deallocate_all_cached_nodes();
  156. ipcdetail::destroy_node_pool_if_last_link(ipcdetail::to_raw_pointer(mp_node_pool));
  157. }
  158. NodePool *get_node_pool() const
  159. { return ipcdetail::to_raw_pointer(mp_node_pool); }
  160. segment_manager *get_segment_manager() const
  161. { return mp_node_pool->get_segment_manager(); }
  162. size_type get_max_cached_nodes() const
  163. { return m_max_cached_nodes; }
  164. void *cached_allocation()
  165. {
  166. //If don't have any cached node, we have to get a new list of free nodes from the pool
  167. if(m_cached_nodes.empty()){
  168. mp_node_pool->allocate_nodes(m_max_cached_nodes/2, m_cached_nodes);
  169. }
  170. void *ret = ipcdetail::to_raw_pointer(m_cached_nodes.pop_front());
  171. return ret;
  172. }
  173. void cached_allocation(size_type n, multiallocation_chain &chain)
  174. {
  175. size_type count = n, allocated(0);
  176. BOOST_TRY{
  177. //If don't have any cached node, we have to get a new list of free nodes from the pool
  178. while(!m_cached_nodes.empty() && count--){
  179. void *ret = ipcdetail::to_raw_pointer(m_cached_nodes.pop_front());
  180. chain.push_back(ret);
  181. ++allocated;
  182. }
  183. if(allocated != n){
  184. mp_node_pool->allocate_nodes(n - allocated, chain);
  185. }
  186. }
  187. BOOST_CATCH(...){
  188. this->cached_deallocation(chain);
  189. BOOST_RETHROW
  190. } BOOST_CATCH_END
  191. }
  192. void cached_deallocation(void *ptr)
  193. {
  194. //Check if cache is full
  195. if(m_cached_nodes.size() >= m_max_cached_nodes){
  196. //This only occurs if this allocator deallocate memory allocated
  197. //with other equal allocator. Since the cache is full, and more
  198. //deallocations are probably coming, we'll make some room in cache
  199. //in a single, efficient multi node deallocation.
  200. this->priv_deallocate_n_nodes(m_cached_nodes.size() - m_max_cached_nodes/2);
  201. }
  202. m_cached_nodes.push_front(ptr);
  203. }
  204. void cached_deallocation(multiallocation_chain &chain)
  205. {
  206. m_cached_nodes.splice_after(m_cached_nodes.before_begin(), chain);
  207. //Check if cache is full
  208. if(m_cached_nodes.size() >= m_max_cached_nodes){
  209. //This only occurs if this allocator deallocate memory allocated
  210. //with other equal allocator. Since the cache is full, and more
  211. //deallocations are probably coming, we'll make some room in cache
  212. //in a single, efficient multi node deallocation.
  213. this->priv_deallocate_n_nodes(m_cached_nodes.size() - m_max_cached_nodes/2);
  214. }
  215. }
  216. //!Sets the new max cached nodes value. This can provoke deallocations
  217. //!if "newmax" is less than current cached nodes. Never throws
  218. void set_max_cached_nodes(size_type newmax)
  219. {
  220. m_max_cached_nodes = newmax;
  221. this->priv_deallocate_remaining_nodes();
  222. }
  223. //!Frees all cached nodes.
  224. //!Never throws
  225. void deallocate_all_cached_nodes()
  226. {
  227. if(m_cached_nodes.empty()) return;
  228. mp_node_pool->deallocate_nodes(m_cached_nodes);
  229. }
  230. private:
  231. //!Frees all cached nodes at once.
  232. //!Never throws
  233. void priv_deallocate_remaining_nodes()
  234. {
  235. if(m_cached_nodes.size() > m_max_cached_nodes){
  236. priv_deallocate_n_nodes(m_cached_nodes.size()-m_max_cached_nodes);
  237. }
  238. }
  239. //!Frees n cached nodes at once. Never throws
  240. void priv_deallocate_n_nodes(size_type n)
  241. {
  242. //This only occurs if this allocator deallocate memory allocated
  243. //with other equal allocator. Since the cache is full, and more
  244. //deallocations are probably coming, we'll make some room in cache
  245. //in a single, efficient multi node deallocation.
  246. size_type count(n);
  247. typename multiallocation_chain::iterator it(m_cached_nodes.before_begin());
  248. while(count--){
  249. ++it;
  250. }
  251. multiallocation_chain chain;
  252. chain.splice_after(chain.before_begin(), m_cached_nodes, m_cached_nodes.before_begin(), it, n);
  253. //Deallocate all new linked list at once
  254. mp_node_pool->deallocate_nodes(chain);
  255. }
  256. public:
  257. void swap(cache_impl &other)
  258. {
  259. ::boost::adl_move_swap(mp_node_pool, other.mp_node_pool);
  260. ::boost::adl_move_swap(m_cached_nodes, other.m_cached_nodes);
  261. ::boost::adl_move_swap(m_max_cached_nodes, other.m_max_cached_nodes);
  262. }
  263. };
  264. template<class Derived, class T, class SegmentManager>
  265. class array_allocation_impl
  266. {
  267. const Derived *derived() const
  268. { return static_cast<const Derived*>(this); }
  269. Derived *derived()
  270. { return static_cast<Derived*>(this); }
  271. typedef typename SegmentManager::void_pointer void_pointer;
  272. public:
  273. typedef typename boost::intrusive::
  274. pointer_traits<void_pointer>::template
  275. rebind_pointer<T>::type pointer;
  276. typedef typename boost::intrusive::
  277. pointer_traits<void_pointer>::template
  278. rebind_pointer<const T>::type const_pointer;
  279. typedef T value_type;
  280. typedef typename ipcdetail::add_reference
  281. <value_type>::type reference;
  282. typedef typename ipcdetail::add_reference
  283. <const value_type>::type const_reference;
  284. typedef typename SegmentManager::size_type size_type;
  285. typedef typename SegmentManager::difference_type difference_type;
  286. typedef boost::container::dtl::transform_multiallocation_chain
  287. <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
  288. public:
  289. //!Returns maximum the number of objects the previously allocated memory
  290. //!pointed by p can hold. This size only works for memory allocated with
  291. //!allocate, allocation_command and allocate_many.
  292. size_type size(const pointer &p) const
  293. {
  294. return (size_type)this->derived()->get_segment_manager()->size(ipcdetail::to_raw_pointer(p))/sizeof(T);
  295. }
  296. pointer allocation_command(boost::interprocess::allocation_type command,
  297. size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
  298. {
  299. value_type *reuse_raw = ipcdetail::to_raw_pointer(reuse);
  300. pointer const p = this->derived()->get_segment_manager()->allocation_command
  301. (command, limit_size, prefer_in_recvd_out_size, reuse_raw);
  302. reuse = reuse_raw;
  303. return p;
  304. }
  305. //!Allocates many elements of size elem_size in a contiguous block
  306. //!of memory. The minimum number to be allocated is min_elements,
  307. //!the preferred and maximum number is
  308. //!preferred_elements. The number of actually allocated elements is
  309. //!will be assigned to received_size. The elements must be deallocated
  310. //!with deallocate(...)
  311. void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain)
  312. {
  313. if(size_overflows<sizeof(T)>(elem_size)){
  314. throw bad_alloc();
  315. }
  316. this->derived()->get_segment_manager()->allocate_many(elem_size*sizeof(T), num_elements, chain);
  317. }
  318. //!Allocates n_elements elements, each one of size elem_sizes[i]in a
  319. //!contiguous block
  320. //!of memory. The elements must be deallocated
  321. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
  322. {
  323. this->derived()->get_segment_manager()->allocate_many(elem_sizes, n_elements, sizeof(T), chain);
  324. }
  325. //!Allocates many elements of size elem_size in a contiguous block
  326. //!of memory. The minimum number to be allocated is min_elements,
  327. //!the preferred and maximum number is
  328. //!preferred_elements. The number of actually allocated elements is
  329. //!will be assigned to received_size. The elements must be deallocated
  330. //!with deallocate(...)
  331. void deallocate_many(multiallocation_chain &chain)
  332. { this->derived()->get_segment_manager()->deallocate_many(chain); }
  333. //!Returns the number of elements that could be
  334. //!allocated. Never throws
  335. size_type max_size() const
  336. { return this->derived()->get_segment_manager()->get_size()/sizeof(T); }
  337. //!Returns address of mutable object.
  338. //!Never throws
  339. pointer address(reference value) const
  340. { return pointer(boost::addressof(value)); }
  341. //!Returns address of non mutable object.
  342. //!Never throws
  343. const_pointer address(const_reference value) const
  344. { return const_pointer(boost::addressof(value)); }
  345. //!Constructs an object
  346. //!Throws if T's constructor throws
  347. //!For backwards compatibility with libraries using C++03 allocators
  348. template<class P>
  349. void construct(const pointer &ptr, BOOST_FWD_REF(P) p)
  350. { ::new((void*)ipcdetail::to_raw_pointer(ptr), boost_container_new_t()) value_type(::boost::forward<P>(p)); }
  351. //!Destroys object. Throws if object's
  352. //!destructor throws
  353. void destroy(const pointer &ptr)
  354. { BOOST_ASSERT(ptr != 0); (*ptr).~value_type(); }
  355. };
  356. template<class Derived, unsigned int Version, class T, class SegmentManager>
  357. class node_pool_allocation_impl
  358. : public array_allocation_impl
  359. < Derived
  360. , T
  361. , SegmentManager>
  362. {
  363. const Derived *derived() const
  364. { return static_cast<const Derived*>(this); }
  365. Derived *derived()
  366. { return static_cast<Derived*>(this); }
  367. typedef typename SegmentManager::void_pointer void_pointer;
  368. typedef typename boost::intrusive::
  369. pointer_traits<void_pointer>::template
  370. rebind_pointer<const void>::type cvoid_pointer;
  371. public:
  372. typedef typename boost::intrusive::
  373. pointer_traits<void_pointer>::template
  374. rebind_pointer<T>::type pointer;
  375. typedef typename boost::intrusive::
  376. pointer_traits<void_pointer>::template
  377. rebind_pointer<const T>::type const_pointer;
  378. typedef T value_type;
  379. typedef typename ipcdetail::add_reference
  380. <value_type>::type reference;
  381. typedef typename ipcdetail::add_reference
  382. <const value_type>::type const_reference;
  383. typedef typename SegmentManager::size_type size_type;
  384. typedef typename SegmentManager::difference_type difference_type;
  385. typedef boost::container::dtl::transform_multiallocation_chain
  386. <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
  387. template <int Dummy>
  388. struct node_pool
  389. {
  390. typedef typename Derived::template node_pool<0>::type type;
  391. static type *get(void *p)
  392. { return static_cast<type*>(p); }
  393. };
  394. public:
  395. //!Allocate memory for an array of count elements.
  396. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  397. pointer allocate(size_type count, cvoid_pointer hint = 0)
  398. {
  399. (void)hint;
  400. typedef typename node_pool<0>::type node_pool_t;
  401. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  402. if(size_overflows<sizeof(T)>(count)){
  403. throw bad_alloc();
  404. }
  405. else if(Version == 1 && count == 1){
  406. return pointer(static_cast<value_type*>
  407. (pool->allocate_node()));
  408. }
  409. else{
  410. return pointer(static_cast<value_type*>
  411. (pool->get_segment_manager()->allocate(count*sizeof(T))));
  412. }
  413. }
  414. //!Deallocate allocated memory. Never throws
  415. void deallocate(const pointer &ptr, size_type count)
  416. {
  417. (void)count;
  418. typedef typename node_pool<0>::type node_pool_t;
  419. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  420. if(Version == 1 && count == 1)
  421. pool->deallocate_node(ipcdetail::to_raw_pointer(ptr));
  422. else
  423. pool->get_segment_manager()->deallocate((void*)ipcdetail::to_raw_pointer(ptr));
  424. }
  425. //!Allocates just one object. Memory allocated with this function
  426. //!must be deallocated only with deallocate_one().
  427. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  428. pointer allocate_one()
  429. {
  430. typedef typename node_pool<0>::type node_pool_t;
  431. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  432. return pointer(static_cast<value_type*>(pool->allocate_node()));
  433. }
  434. //!Allocates many elements of size == 1 in a contiguous block
  435. //!of memory. The minimum number to be allocated is min_elements,
  436. //!the preferred and maximum number is
  437. //!preferred_elements. The number of actually allocated elements is
  438. //!will be assigned to received_size. Memory allocated with this function
  439. //!must be deallocated only with deallocate_one().
  440. void allocate_individual(size_type num_elements, multiallocation_chain &chain)
  441. {
  442. typedef typename node_pool<0>::type node_pool_t;
  443. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  444. pool->allocate_nodes(num_elements, chain);
  445. }
  446. //!Deallocates memory previously allocated with allocate_one().
  447. //!You should never use deallocate_one to deallocate memory allocated
  448. //!with other functions different from allocate_one(). Never throws
  449. void deallocate_one(const pointer &p)
  450. {
  451. typedef typename node_pool<0>::type node_pool_t;
  452. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  453. pool->deallocate_node(ipcdetail::to_raw_pointer(p));
  454. }
  455. //!Allocates many elements of size == 1 in a contiguous block
  456. //!of memory. The minimum number to be allocated is min_elements,
  457. //!the preferred and maximum number is
  458. //!preferred_elements. The number of actually allocated elements is
  459. //!will be assigned to received_size. Memory allocated with this function
  460. //!must be deallocated only with deallocate_one().
  461. void deallocate_individual(multiallocation_chain &chain)
  462. {
  463. node_pool<0>::get(this->derived()->get_node_pool())->deallocate_nodes
  464. (chain);
  465. }
  466. //!Deallocates all free blocks of the pool
  467. void deallocate_free_blocks()
  468. { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_free_blocks(); }
  469. //!Deprecated, use deallocate_free_blocks.
  470. //!Deallocates all free chunks of the pool.
  471. void deallocate_free_chunks()
  472. { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_free_blocks(); }
  473. };
  474. template<class T, class NodePool, unsigned int Version>
  475. class cached_allocator_impl
  476. : public array_allocation_impl
  477. <cached_allocator_impl<T, NodePool, Version>, T, typename NodePool::segment_manager>
  478. {
  479. cached_allocator_impl & operator=(const cached_allocator_impl& other);
  480. typedef array_allocation_impl
  481. < cached_allocator_impl
  482. <T, NodePool, Version>
  483. , T
  484. , typename NodePool::segment_manager> base_t;
  485. public:
  486. typedef NodePool node_pool_t;
  487. typedef typename NodePool::segment_manager segment_manager;
  488. typedef typename segment_manager::void_pointer void_pointer;
  489. typedef typename boost::intrusive::
  490. pointer_traits<void_pointer>::template
  491. rebind_pointer<const void>::type cvoid_pointer;
  492. typedef typename base_t::pointer pointer;
  493. typedef typename base_t::size_type size_type;
  494. typedef typename base_t::multiallocation_chain multiallocation_chain;
  495. typedef typename base_t::value_type value_type;
  496. public:
  497. static const std::size_t DEFAULT_MAX_CACHED_NODES = 64;
  498. cached_allocator_impl(segment_manager *segment_mngr, size_type max_cached_nodes)
  499. : m_cache(segment_mngr, max_cached_nodes)
  500. {}
  501. cached_allocator_impl(const cached_allocator_impl &other)
  502. : m_cache(other.m_cache)
  503. {}
  504. //!Copy constructor from related cached_adaptive_pool_base. If not present, constructs
  505. //!a node pool. Increments the reference count of the associated node pool.
  506. //!Can throw boost::interprocess::bad_alloc
  507. template<class T2, class NodePool2>
  508. cached_allocator_impl
  509. (const cached_allocator_impl
  510. <T2, NodePool2, Version> &other)
  511. : m_cache(other.get_segment_manager(), other.get_max_cached_nodes())
  512. {}
  513. //!Returns a pointer to the node pool.
  514. //!Never throws
  515. node_pool_t* get_node_pool() const
  516. { return m_cache.get_node_pool(); }
  517. //!Returns the segment manager.
  518. //!Never throws
  519. segment_manager* get_segment_manager()const
  520. { return m_cache.get_segment_manager(); }
  521. //!Sets the new max cached nodes value. This can provoke deallocations
  522. //!if "newmax" is less than current cached nodes. Never throws
  523. void set_max_cached_nodes(size_type newmax)
  524. { m_cache.set_max_cached_nodes(newmax); }
  525. //!Returns the max cached nodes parameter.
  526. //!Never throws
  527. size_type get_max_cached_nodes() const
  528. { return m_cache.get_max_cached_nodes(); }
  529. //!Allocate memory for an array of count elements.
  530. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  531. pointer allocate(size_type count, cvoid_pointer hint = 0)
  532. {
  533. (void)hint;
  534. void * ret;
  535. if(size_overflows<sizeof(T)>(count)){
  536. throw bad_alloc();
  537. }
  538. else if(Version == 1 && count == 1){
  539. ret = m_cache.cached_allocation();
  540. }
  541. else{
  542. ret = this->get_segment_manager()->allocate(count*sizeof(T));
  543. }
  544. return pointer(static_cast<T*>(ret));
  545. }
  546. //!Deallocate allocated memory. Never throws
  547. void deallocate(const pointer &ptr, size_type count)
  548. {
  549. (void)count;
  550. if(Version == 1 && count == 1){
  551. m_cache.cached_deallocation(ipcdetail::to_raw_pointer(ptr));
  552. }
  553. else{
  554. this->get_segment_manager()->deallocate((void*)ipcdetail::to_raw_pointer(ptr));
  555. }
  556. }
  557. //!Allocates just one object. Memory allocated with this function
  558. //!must be deallocated only with deallocate_one().
  559. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  560. pointer allocate_one()
  561. { return pointer(static_cast<value_type*>(this->m_cache.cached_allocation())); }
  562. //!Allocates many elements of size == 1 in a contiguous block
  563. //!of memory. The minimum number to be allocated is min_elements,
  564. //!the preferred and maximum number is
  565. //!preferred_elements. The number of actually allocated elements is
  566. //!will be assigned to received_size. Memory allocated with this function
  567. //!must be deallocated only with deallocate_one().
  568. void allocate_individual(size_type num_elements, multiallocation_chain &chain)
  569. { this->m_cache.cached_allocation(num_elements, chain); }
  570. //!Deallocates memory previously allocated with allocate_one().
  571. //!You should never use deallocate_one to deallocate memory allocated
  572. //!with other functions different from allocate_one(). Never throws
  573. void deallocate_one(const pointer &p)
  574. { this->m_cache.cached_deallocation(ipcdetail::to_raw_pointer(p)); }
  575. //!Allocates many elements of size == 1 in a contiguous block
  576. //!of memory. The minimum number to be allocated is min_elements,
  577. //!the preferred and maximum number is
  578. //!preferred_elements. The number of actually allocated elements is
  579. //!will be assigned to received_size. Memory allocated with this function
  580. //!must be deallocated only with deallocate_one().
  581. void deallocate_individual(multiallocation_chain &chain)
  582. { m_cache.cached_deallocation(chain); }
  583. //!Deallocates all free blocks of the pool
  584. void deallocate_free_blocks()
  585. { m_cache.get_node_pool()->deallocate_free_blocks(); }
  586. //!Swaps allocators. Does not throw. If each allocator is placed in a
  587. //!different shared memory segments, the result is undefined.
  588. friend void swap(cached_allocator_impl &alloc1, cached_allocator_impl &alloc2)
  589. { ::boost::adl_move_swap(alloc1.m_cache, alloc2.m_cache); }
  590. void deallocate_cache()
  591. { m_cache.deallocate_all_cached_nodes(); }
  592. //!Deprecated use deallocate_free_blocks.
  593. void deallocate_free_chunks()
  594. { m_cache.get_node_pool()->deallocate_free_blocks(); }
  595. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  596. private:
  597. cache_impl<node_pool_t> m_cache;
  598. #endif //!defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  599. };
  600. //!Equality test for same type of
  601. //!cached_allocator_impl
  602. template<class T, class N, unsigned int V> inline
  603. bool operator==(const cached_allocator_impl<T, N, V> &alloc1,
  604. const cached_allocator_impl<T, N, V> &alloc2)
  605. { return alloc1.get_node_pool() == alloc2.get_node_pool(); }
  606. //!Inequality test for same type of
  607. //!cached_allocator_impl
  608. template<class T, class N, unsigned int V> inline
  609. bool operator!=(const cached_allocator_impl<T, N, V> &alloc1,
  610. const cached_allocator_impl<T, N, V> &alloc2)
  611. { return alloc1.get_node_pool() != alloc2.get_node_pool(); }
  612. //!Pooled shared memory allocator using adaptive pool. Includes
  613. //!a reference count but the class does not delete itself, this is
  614. //!responsibility of user classes. Node size (NodeSize) and the number of
  615. //!nodes allocated per block (NodesPerBlock) are known at compile time
  616. template<class private_node_allocator_t>
  617. class shared_pool_impl
  618. : public private_node_allocator_t
  619. {
  620. public:
  621. //!Segment manager typedef
  622. typedef typename private_node_allocator_t::
  623. segment_manager segment_manager;
  624. typedef typename private_node_allocator_t::
  625. multiallocation_chain multiallocation_chain;
  626. typedef typename private_node_allocator_t::
  627. size_type size_type;
  628. private:
  629. typedef typename segment_manager::mutex_family::mutex_type mutex_type;
  630. public:
  631. //!Constructor from a segment manager. Never throws
  632. shared_pool_impl(segment_manager *segment_mngr)
  633. : private_node_allocator_t(segment_mngr)
  634. {}
  635. //!Destructor. Deallocates all allocated blocks. Never throws
  636. ~shared_pool_impl()
  637. {}
  638. //!Allocates array of count elements. Can throw boost::interprocess::bad_alloc
  639. void *allocate_node()
  640. {
  641. //-----------------------
  642. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  643. //-----------------------
  644. return private_node_allocator_t::allocate_node();
  645. }
  646. //!Deallocates an array pointed by ptr. Never throws
  647. void deallocate_node(void *ptr)
  648. {
  649. //-----------------------
  650. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  651. //-----------------------
  652. private_node_allocator_t::deallocate_node(ptr);
  653. }
  654. //!Allocates n nodes.
  655. //!Can throw boost::interprocess::bad_alloc
  656. void allocate_nodes(const size_type n, multiallocation_chain &chain)
  657. {
  658. //-----------------------
  659. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  660. //-----------------------
  661. private_node_allocator_t::allocate_nodes(n, chain);
  662. }
  663. //!Deallocates a linked list of nodes ending in null pointer. Never throws
  664. void deallocate_nodes(multiallocation_chain &nodes, size_type num)
  665. {
  666. //-----------------------
  667. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  668. //-----------------------
  669. private_node_allocator_t::deallocate_nodes(nodes, num);
  670. }
  671. //!Deallocates the nodes pointed by the multiallocation iterator. Never throws
  672. void deallocate_nodes(multiallocation_chain &chain)
  673. {
  674. //-----------------------
  675. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  676. //-----------------------
  677. private_node_allocator_t::deallocate_nodes(chain);
  678. }
  679. //!Deallocates all the free blocks of memory. Never throws
  680. void deallocate_free_blocks()
  681. {
  682. //-----------------------
  683. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  684. //-----------------------
  685. private_node_allocator_t::deallocate_free_blocks();
  686. }
  687. //!Deallocates all used memory from the common pool.
  688. //!Precondition: all nodes allocated from this pool should
  689. //!already be deallocated. Otherwise, undefined behavior. Never throws
  690. void purge_blocks()
  691. {
  692. //-----------------------
  693. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  694. //-----------------------
  695. private_node_allocator_t::purge_blocks();
  696. }
  697. //!Increments internal reference count and returns new count. Never throws
  698. size_type inc_ref_count()
  699. {
  700. //-----------------------
  701. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  702. //-----------------------
  703. return ++m_header.m_usecount;
  704. }
  705. //!Decrements internal reference count and returns new count. Never throws
  706. size_type dec_ref_count()
  707. {
  708. //-----------------------
  709. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  710. //-----------------------
  711. BOOST_ASSERT(m_header.m_usecount > 0);
  712. return --m_header.m_usecount;
  713. }
  714. //!Deprecated, use deallocate_free_blocks.
  715. void deallocate_free_chunks()
  716. {
  717. //-----------------------
  718. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  719. //-----------------------
  720. private_node_allocator_t::deallocate_free_blocks();
  721. }
  722. //!Deprecated, use purge_blocks.
  723. void purge_chunks()
  724. {
  725. //-----------------------
  726. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  727. //-----------------------
  728. private_node_allocator_t::purge_blocks();
  729. }
  730. private:
  731. //!This struct includes needed data and derives from
  732. //!the mutex type to allow EBO when using null_mutex
  733. struct header_t : mutex_type
  734. {
  735. size_type m_usecount; //Number of attached allocators
  736. header_t()
  737. : m_usecount(0) {}
  738. } m_header;
  739. };
  740. } //namespace ipcdetail {
  741. } //namespace interprocess {
  742. } //namespace boost {
  743. #include <boost/interprocess/detail/config_end.hpp>
  744. #endif //#ifndef BOOST_INTERPROCESS_ALLOCATOR_DETAIL_ALLOCATOR_COMMON_HPP