allocator_common.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. }
  191. BOOST_CATCH_END
  192. }
  193. void cached_deallocation(void *ptr)
  194. {
  195. //Check if cache is full
  196. if(m_cached_nodes.size() >= m_max_cached_nodes){
  197. //This only occurs if this allocator deallocate memory allocated
  198. //with other equal allocator. Since the cache is full, and more
  199. //deallocations are probably coming, we'll make some room in cache
  200. //in a single, efficient multi node deallocation.
  201. this->priv_deallocate_n_nodes(m_cached_nodes.size() - m_max_cached_nodes/2);
  202. }
  203. m_cached_nodes.push_front(ptr);
  204. }
  205. void cached_deallocation(multiallocation_chain &chain)
  206. {
  207. m_cached_nodes.splice_after(m_cached_nodes.before_begin(), chain);
  208. //Check if cache is full
  209. if(m_cached_nodes.size() >= m_max_cached_nodes){
  210. //This only occurs if this allocator deallocate memory allocated
  211. //with other equal allocator. Since the cache is full, and more
  212. //deallocations are probably coming, we'll make some room in cache
  213. //in a single, efficient multi node deallocation.
  214. this->priv_deallocate_n_nodes(m_cached_nodes.size() - m_max_cached_nodes/2);
  215. }
  216. }
  217. //!Sets the new max cached nodes value. This can provoke deallocations
  218. //!if "newmax" is less than current cached nodes. Never throws
  219. void set_max_cached_nodes(size_type newmax)
  220. {
  221. m_max_cached_nodes = newmax;
  222. this->priv_deallocate_remaining_nodes();
  223. }
  224. //!Frees all cached nodes.
  225. //!Never throws
  226. void deallocate_all_cached_nodes()
  227. {
  228. if(m_cached_nodes.empty()) return;
  229. mp_node_pool->deallocate_nodes(m_cached_nodes);
  230. }
  231. private:
  232. //!Frees all cached nodes at once.
  233. //!Never throws
  234. void priv_deallocate_remaining_nodes()
  235. {
  236. if(m_cached_nodes.size() > m_max_cached_nodes){
  237. priv_deallocate_n_nodes(m_cached_nodes.size()-m_max_cached_nodes);
  238. }
  239. }
  240. //!Frees n cached nodes at once. Never throws
  241. void priv_deallocate_n_nodes(size_type n)
  242. {
  243. //This only occurs if this allocator deallocate memory allocated
  244. //with other equal allocator. Since the cache is full, and more
  245. //deallocations are probably coming, we'll make some room in cache
  246. //in a single, efficient multi node deallocation.
  247. size_type count(n);
  248. typename multiallocation_chain::iterator it(m_cached_nodes.before_begin());
  249. while(count--){
  250. ++it;
  251. }
  252. multiallocation_chain chain;
  253. chain.splice_after(chain.before_begin(), m_cached_nodes, m_cached_nodes.before_begin(), it, n);
  254. //Deallocate all new linked list at once
  255. mp_node_pool->deallocate_nodes(chain);
  256. }
  257. public:
  258. void swap(cache_impl &other)
  259. {
  260. ::boost::adl_move_swap(mp_node_pool, other.mp_node_pool);
  261. ::boost::adl_move_swap(m_cached_nodes, other.m_cached_nodes);
  262. ::boost::adl_move_swap(m_max_cached_nodes, other.m_max_cached_nodes);
  263. }
  264. };
  265. template<class Derived, class T, class SegmentManager>
  266. class array_allocation_impl
  267. {
  268. const Derived *derived() const
  269. { return static_cast<const Derived*>(this); }
  270. Derived *derived()
  271. { return static_cast<Derived*>(this); }
  272. typedef typename SegmentManager::void_pointer void_pointer;
  273. public:
  274. typedef typename boost::intrusive::
  275. pointer_traits<void_pointer>::template
  276. rebind_pointer<T>::type pointer;
  277. typedef typename boost::intrusive::
  278. pointer_traits<void_pointer>::template
  279. rebind_pointer<const T>::type const_pointer;
  280. typedef T value_type;
  281. typedef typename ipcdetail::add_reference
  282. <value_type>::type reference;
  283. typedef typename ipcdetail::add_reference
  284. <const value_type>::type const_reference;
  285. typedef typename SegmentManager::size_type size_type;
  286. typedef typename SegmentManager::difference_type difference_type;
  287. typedef boost::container::dtl::transform_multiallocation_chain
  288. <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
  289. public:
  290. //!Returns maximum the number of objects the previously allocated memory
  291. //!pointed by p can hold. This size only works for memory allocated with
  292. //!allocate, allocation_command and allocate_many.
  293. size_type size(const pointer &p) const
  294. {
  295. return (size_type)this->derived()->get_segment_manager()->size(ipcdetail::to_raw_pointer(p))/sizeof(T);
  296. }
  297. pointer allocation_command(boost::interprocess::allocation_type command,
  298. size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
  299. {
  300. value_type *reuse_raw = ipcdetail::to_raw_pointer(reuse);
  301. pointer const p = this->derived()->get_segment_manager()->allocation_command
  302. (command, limit_size, prefer_in_recvd_out_size, reuse_raw);
  303. reuse = reuse_raw;
  304. return p;
  305. }
  306. //!Allocates many elements of size elem_size in a contiguous block
  307. //!of memory. The minimum number to be allocated is min_elements,
  308. //!the preferred and maximum number is
  309. //!preferred_elements. The number of actually allocated elements is
  310. //!will be assigned to received_size. The elements must be deallocated
  311. //!with deallocate(...)
  312. void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain)
  313. {
  314. if(size_overflows<sizeof(T)>(elem_size)){
  315. throw bad_alloc();
  316. }
  317. this->derived()->get_segment_manager()->allocate_many(elem_size*sizeof(T), num_elements, chain);
  318. }
  319. //!Allocates n_elements elements, each one of size elem_sizes[i]in a
  320. //!contiguous block
  321. //!of memory. The elements must be deallocated
  322. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
  323. {
  324. this->derived()->get_segment_manager()->allocate_many(elem_sizes, n_elements, sizeof(T), chain);
  325. }
  326. //!Allocates many elements of size elem_size in a contiguous block
  327. //!of memory. The minimum number to be allocated is min_elements,
  328. //!the preferred and maximum number is
  329. //!preferred_elements. The number of actually allocated elements is
  330. //!will be assigned to received_size. The elements must be deallocated
  331. //!with deallocate(...)
  332. void deallocate_many(multiallocation_chain &chain)
  333. { this->derived()->get_segment_manager()->deallocate_many(chain); }
  334. //!Returns the number of elements that could be
  335. //!allocated. Never throws
  336. size_type max_size() const
  337. { return this->derived()->get_segment_manager()->get_size()/sizeof(T); }
  338. //!Returns address of mutable object.
  339. //!Never throws
  340. pointer address(reference value) const
  341. { return pointer(boost::addressof(value)); }
  342. //!Returns address of non mutable object.
  343. //!Never throws
  344. const_pointer address(const_reference value) const
  345. { return const_pointer(boost::addressof(value)); }
  346. //!Constructs an object
  347. //!Throws if T's constructor throws
  348. //!For backwards compatibility with libraries using C++03 allocators
  349. template<class P>
  350. void construct(const pointer &ptr, BOOST_FWD_REF(P) p)
  351. { ::new((void*)ipcdetail::to_raw_pointer(ptr), boost_container_new_t()) value_type(::boost::forward<P>(p)); }
  352. //!Destroys object. Throws if object's
  353. //!destructor throws
  354. void destroy(const pointer &ptr)
  355. { BOOST_ASSERT(ptr != 0); (*ptr).~value_type(); }
  356. };
  357. template<class Derived, unsigned int Version, class T, class SegmentManager>
  358. class node_pool_allocation_impl
  359. : public array_allocation_impl
  360. < Derived
  361. , T
  362. , SegmentManager>
  363. {
  364. const Derived *derived() const
  365. { return static_cast<const Derived*>(this); }
  366. Derived *derived()
  367. { return static_cast<Derived*>(this); }
  368. typedef typename SegmentManager::void_pointer void_pointer;
  369. typedef typename boost::intrusive::
  370. pointer_traits<void_pointer>::template
  371. rebind_pointer<const void>::type cvoid_pointer;
  372. public:
  373. typedef typename boost::intrusive::
  374. pointer_traits<void_pointer>::template
  375. rebind_pointer<T>::type pointer;
  376. typedef typename boost::intrusive::
  377. pointer_traits<void_pointer>::template
  378. rebind_pointer<const T>::type const_pointer;
  379. typedef T value_type;
  380. typedef typename ipcdetail::add_reference
  381. <value_type>::type reference;
  382. typedef typename ipcdetail::add_reference
  383. <const value_type>::type const_reference;
  384. typedef typename SegmentManager::size_type size_type;
  385. typedef typename SegmentManager::difference_type difference_type;
  386. typedef boost::container::dtl::transform_multiallocation_chain
  387. <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
  388. template <int Dummy>
  389. struct node_pool
  390. {
  391. typedef typename Derived::template node_pool<0>::type type;
  392. static type *get(void *p)
  393. { return static_cast<type*>(p); }
  394. };
  395. public:
  396. //!Allocate memory for an array of count elements.
  397. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  398. pointer allocate(size_type count, cvoid_pointer hint = 0)
  399. {
  400. (void)hint;
  401. typedef typename node_pool<0>::type node_pool_t;
  402. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  403. if(size_overflows<sizeof(T)>(count)){
  404. throw bad_alloc();
  405. }
  406. else if(Version == 1 && count == 1){
  407. return pointer(static_cast<value_type*>
  408. (pool->allocate_node()));
  409. }
  410. else{
  411. return pointer(static_cast<value_type*>
  412. (pool->get_segment_manager()->allocate(count*sizeof(T))));
  413. }
  414. }
  415. //!Deallocate allocated memory. Never throws
  416. void deallocate(const pointer &ptr, size_type count)
  417. {
  418. (void)count;
  419. typedef typename node_pool<0>::type node_pool_t;
  420. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  421. if(Version == 1 && count == 1)
  422. pool->deallocate_node(ipcdetail::to_raw_pointer(ptr));
  423. else
  424. pool->get_segment_manager()->deallocate((void*)ipcdetail::to_raw_pointer(ptr));
  425. }
  426. //!Allocates just one object. Memory allocated with this function
  427. //!must be deallocated only with deallocate_one().
  428. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  429. pointer allocate_one()
  430. {
  431. typedef typename node_pool<0>::type node_pool_t;
  432. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  433. return pointer(static_cast<value_type*>(pool->allocate_node()));
  434. }
  435. //!Allocates many elements of size == 1 in a contiguous block
  436. //!of memory. The minimum number to be allocated is min_elements,
  437. //!the preferred and maximum number is
  438. //!preferred_elements. The number of actually allocated elements is
  439. //!will be assigned to received_size. Memory allocated with this function
  440. //!must be deallocated only with deallocate_one().
  441. void allocate_individual(size_type num_elements, multiallocation_chain &chain)
  442. {
  443. typedef typename node_pool<0>::type node_pool_t;
  444. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  445. pool->allocate_nodes(num_elements, chain);
  446. }
  447. //!Deallocates memory previously allocated with allocate_one().
  448. //!You should never use deallocate_one to deallocate memory allocated
  449. //!with other functions different from allocate_one(). Never throws
  450. void deallocate_one(const pointer &p)
  451. {
  452. typedef typename node_pool<0>::type node_pool_t;
  453. node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());
  454. pool->deallocate_node(ipcdetail::to_raw_pointer(p));
  455. }
  456. //!Allocates many elements of size == 1 in a contiguous block
  457. //!of memory. The minimum number to be allocated is min_elements,
  458. //!the preferred and maximum number is
  459. //!preferred_elements. The number of actually allocated elements is
  460. //!will be assigned to received_size. Memory allocated with this function
  461. //!must be deallocated only with deallocate_one().
  462. void deallocate_individual(multiallocation_chain &chain)
  463. {
  464. node_pool<0>::get(this->derived()->get_node_pool())->deallocate_nodes
  465. (chain);
  466. }
  467. //!Deallocates all free blocks of the pool
  468. void deallocate_free_blocks()
  469. { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_free_blocks(); }
  470. //!Deprecated, use deallocate_free_blocks.
  471. //!Deallocates all free chunks of the pool.
  472. void deallocate_free_chunks()
  473. { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_free_blocks(); }
  474. };
  475. template<class T, class NodePool, unsigned int Version>
  476. class cached_allocator_impl
  477. : public array_allocation_impl
  478. <cached_allocator_impl<T, NodePool, Version>, T, typename NodePool::segment_manager>
  479. {
  480. cached_allocator_impl & operator=(const cached_allocator_impl& other);
  481. typedef array_allocation_impl
  482. < cached_allocator_impl
  483. <T, NodePool, Version>
  484. , T
  485. , typename NodePool::segment_manager> base_t;
  486. public:
  487. typedef NodePool node_pool_t;
  488. typedef typename NodePool::segment_manager segment_manager;
  489. typedef typename segment_manager::void_pointer void_pointer;
  490. typedef typename boost::intrusive::
  491. pointer_traits<void_pointer>::template
  492. rebind_pointer<const void>::type cvoid_pointer;
  493. typedef typename base_t::pointer pointer;
  494. typedef typename base_t::size_type size_type;
  495. typedef typename base_t::multiallocation_chain multiallocation_chain;
  496. typedef typename base_t::value_type value_type;
  497. public:
  498. static const std::size_t DEFAULT_MAX_CACHED_NODES = 64;
  499. cached_allocator_impl(segment_manager *segment_mngr, size_type max_cached_nodes)
  500. : m_cache(segment_mngr, max_cached_nodes)
  501. {}
  502. cached_allocator_impl(const cached_allocator_impl &other)
  503. : m_cache(other.m_cache)
  504. {}
  505. //!Copy constructor from related cached_adaptive_pool_base. If not present, constructs
  506. //!a node pool. Increments the reference count of the associated node pool.
  507. //!Can throw boost::interprocess::bad_alloc
  508. template<class T2, class NodePool2>
  509. cached_allocator_impl
  510. (const cached_allocator_impl
  511. <T2, NodePool2, Version> &other)
  512. : m_cache(other.get_segment_manager(), other.get_max_cached_nodes())
  513. {}
  514. //!Returns a pointer to the node pool.
  515. //!Never throws
  516. node_pool_t* get_node_pool() const
  517. { return m_cache.get_node_pool(); }
  518. //!Returns the segment manager.
  519. //!Never throws
  520. segment_manager* get_segment_manager()const
  521. { return m_cache.get_segment_manager(); }
  522. //!Sets the new max cached nodes value. This can provoke deallocations
  523. //!if "newmax" is less than current cached nodes. Never throws
  524. void set_max_cached_nodes(size_type newmax)
  525. { m_cache.set_max_cached_nodes(newmax); }
  526. //!Returns the max cached nodes parameter.
  527. //!Never throws
  528. size_type get_max_cached_nodes() const
  529. { return m_cache.get_max_cached_nodes(); }
  530. //!Allocate memory for an array of count elements.
  531. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  532. pointer allocate(size_type count, cvoid_pointer hint = 0)
  533. {
  534. (void)hint;
  535. void * ret;
  536. if(size_overflows<sizeof(T)>(count)){
  537. throw bad_alloc();
  538. }
  539. else if(Version == 1 && count == 1){
  540. ret = m_cache.cached_allocation();
  541. }
  542. else{
  543. ret = this->get_segment_manager()->allocate(count*sizeof(T));
  544. }
  545. return pointer(static_cast<T*>(ret));
  546. }
  547. //!Deallocate allocated memory. Never throws
  548. void deallocate(const pointer &ptr, size_type count)
  549. {
  550. (void)count;
  551. if(Version == 1 && count == 1){
  552. m_cache.cached_deallocation(ipcdetail::to_raw_pointer(ptr));
  553. }
  554. else{
  555. this->get_segment_manager()->deallocate((void*)ipcdetail::to_raw_pointer(ptr));
  556. }
  557. }
  558. //!Allocates just one object. Memory allocated with this function
  559. //!must be deallocated only with deallocate_one().
  560. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  561. pointer allocate_one()
  562. { return pointer(static_cast<value_type*>(this->m_cache.cached_allocation())); }
  563. //!Allocates many elements of size == 1 in a contiguous block
  564. //!of memory. The minimum number to be allocated is min_elements,
  565. //!the preferred and maximum number is
  566. //!preferred_elements. The number of actually allocated elements is
  567. //!will be assigned to received_size. Memory allocated with this function
  568. //!must be deallocated only with deallocate_one().
  569. void allocate_individual(size_type num_elements, multiallocation_chain &chain)
  570. { this->m_cache.cached_allocation(num_elements, chain); }
  571. //!Deallocates memory previously allocated with allocate_one().
  572. //!You should never use deallocate_one to deallocate memory allocated
  573. //!with other functions different from allocate_one(). Never throws
  574. void deallocate_one(const pointer &p)
  575. { this->m_cache.cached_deallocation(ipcdetail::to_raw_pointer(p)); }
  576. //!Allocates many elements of size == 1 in a contiguous block
  577. //!of memory. The minimum number to be allocated is min_elements,
  578. //!the preferred and maximum number is
  579. //!preferred_elements. The number of actually allocated elements is
  580. //!will be assigned to received_size. Memory allocated with this function
  581. //!must be deallocated only with deallocate_one().
  582. void deallocate_individual(multiallocation_chain &chain)
  583. { m_cache.cached_deallocation(chain); }
  584. //!Deallocates all free blocks of the pool
  585. void deallocate_free_blocks()
  586. { m_cache.get_node_pool()->deallocate_free_blocks(); }
  587. //!Swaps allocators. Does not throw. If each allocator is placed in a
  588. //!different shared memory segments, the result is undefined.
  589. friend void swap(cached_allocator_impl &alloc1, cached_allocator_impl &alloc2)
  590. { ::boost::adl_move_swap(alloc1.m_cache, alloc2.m_cache); }
  591. void deallocate_cache()
  592. { m_cache.deallocate_all_cached_nodes(); }
  593. //!Deprecated use deallocate_free_blocks.
  594. void deallocate_free_chunks()
  595. { m_cache.get_node_pool()->deallocate_free_blocks(); }
  596. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  597. private:
  598. cache_impl<node_pool_t> m_cache;
  599. #endif //!defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  600. };
  601. //!Equality test for same type of
  602. //!cached_allocator_impl
  603. template<class T, class N, unsigned int V> inline
  604. bool operator==(const cached_allocator_impl<T, N, V> &alloc1,
  605. const cached_allocator_impl<T, N, V> &alloc2)
  606. { return alloc1.get_node_pool() == alloc2.get_node_pool(); }
  607. //!Inequality test for same type of
  608. //!cached_allocator_impl
  609. template<class T, class N, unsigned int V> inline
  610. bool operator!=(const cached_allocator_impl<T, N, V> &alloc1,
  611. const cached_allocator_impl<T, N, V> &alloc2)
  612. { return alloc1.get_node_pool() != alloc2.get_node_pool(); }
  613. //!Pooled shared memory allocator using adaptive pool. Includes
  614. //!a reference count but the class does not delete itself, this is
  615. //!responsibility of user classes. Node size (NodeSize) and the number of
  616. //!nodes allocated per block (NodesPerBlock) are known at compile time
  617. template<class private_node_allocator_t>
  618. class shared_pool_impl
  619. : public private_node_allocator_t
  620. {
  621. public:
  622. //!Segment manager typedef
  623. typedef typename private_node_allocator_t::
  624. segment_manager segment_manager;
  625. typedef typename private_node_allocator_t::
  626. multiallocation_chain multiallocation_chain;
  627. typedef typename private_node_allocator_t::
  628. size_type size_type;
  629. private:
  630. typedef typename segment_manager::mutex_family::mutex_type mutex_type;
  631. public:
  632. //!Constructor from a segment manager. Never throws
  633. shared_pool_impl(segment_manager *segment_mngr)
  634. : private_node_allocator_t(segment_mngr)
  635. {}
  636. //!Destructor. Deallocates all allocated blocks. Never throws
  637. ~shared_pool_impl()
  638. {}
  639. //!Allocates array of count elements. Can throw boost::interprocess::bad_alloc
  640. void *allocate_node()
  641. {
  642. //-----------------------
  643. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  644. //-----------------------
  645. return private_node_allocator_t::allocate_node();
  646. }
  647. //!Deallocates an array pointed by ptr. Never throws
  648. void deallocate_node(void *ptr)
  649. {
  650. //-----------------------
  651. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  652. //-----------------------
  653. private_node_allocator_t::deallocate_node(ptr);
  654. }
  655. //!Allocates n nodes.
  656. //!Can throw boost::interprocess::bad_alloc
  657. void allocate_nodes(const size_type n, multiallocation_chain &chain)
  658. {
  659. //-----------------------
  660. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  661. //-----------------------
  662. private_node_allocator_t::allocate_nodes(n, chain);
  663. }
  664. //!Deallocates a linked list of nodes ending in null pointer. Never throws
  665. void deallocate_nodes(multiallocation_chain &nodes, size_type num)
  666. {
  667. //-----------------------
  668. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  669. //-----------------------
  670. private_node_allocator_t::deallocate_nodes(nodes, num);
  671. }
  672. //!Deallocates the nodes pointed by the multiallocation iterator. Never throws
  673. void deallocate_nodes(multiallocation_chain &chain)
  674. {
  675. //-----------------------
  676. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  677. //-----------------------
  678. private_node_allocator_t::deallocate_nodes(chain);
  679. }
  680. //!Deallocates all the free blocks of memory. Never throws
  681. void deallocate_free_blocks()
  682. {
  683. //-----------------------
  684. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  685. //-----------------------
  686. private_node_allocator_t::deallocate_free_blocks();
  687. }
  688. //!Deallocates all used memory from the common pool.
  689. //!Precondition: all nodes allocated from this pool should
  690. //!already be deallocated. Otherwise, undefined behavior. Never throws
  691. void purge_blocks()
  692. {
  693. //-----------------------
  694. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  695. //-----------------------
  696. private_node_allocator_t::purge_blocks();
  697. }
  698. //!Increments internal reference count and returns new count. Never throws
  699. size_type inc_ref_count()
  700. {
  701. //-----------------------
  702. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  703. //-----------------------
  704. return ++m_header.m_usecount;
  705. }
  706. //!Decrements internal reference count and returns new count. Never throws
  707. size_type dec_ref_count()
  708. {
  709. //-----------------------
  710. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  711. //-----------------------
  712. BOOST_ASSERT(m_header.m_usecount > 0);
  713. return --m_header.m_usecount;
  714. }
  715. //!Deprecated, use deallocate_free_blocks.
  716. void deallocate_free_chunks()
  717. {
  718. //-----------------------
  719. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  720. //-----------------------
  721. private_node_allocator_t::deallocate_free_blocks();
  722. }
  723. //!Deprecated, use purge_blocks.
  724. void purge_chunks()
  725. {
  726. //-----------------------
  727. boost::interprocess::scoped_lock<mutex_type> guard(m_header);
  728. //-----------------------
  729. private_node_allocator_t::purge_blocks();
  730. }
  731. private:
  732. //!This struct includes needed data and derives from
  733. //!the mutex type to allow EBO when using null_mutex
  734. struct header_t : mutex_type
  735. {
  736. size_type m_usecount; //Number of attached allocators
  737. header_t()
  738. : m_usecount(0) {}
  739. } m_header;
  740. };
  741. } //namespace ipcdetail {
  742. } //namespace interprocess {
  743. } //namespace boost {
  744. #include <boost/interprocess/detail/config_end.hpp>
  745. #endif //#ifndef BOOST_INTERPROCESS_ALLOCATOR_DETAIL_ALLOCATOR_COMMON_HPP