managed_memory_impl.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. #ifndef BOOST_INTERPROCESS_DETAIL_MANAGED_MEMORY_IMPL_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_MANAGED_MEMORY_IMPL_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/interprocess/interprocess_fwd.hpp>
  22. #include <boost/interprocess/detail/utilities.hpp>
  23. #include <boost/interprocess/detail/os_file_functions.hpp>
  24. #include <boost/interprocess/creation_tags.hpp>
  25. #include <boost/interprocess/exceptions.hpp>
  26. #include <boost/interprocess/segment_manager.hpp>
  27. #include <boost/interprocess/sync/scoped_lock.hpp>
  28. #include <boost/interprocess/detail/nothrow.hpp>
  29. #include <boost/interprocess/detail/simple_swap.hpp>
  30. //
  31. #include <boost/core/no_exceptions_support.hpp>
  32. //
  33. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  34. #include <boost/assert.hpp>
  35. //!\file
  36. //!Describes a named shared memory allocation user class.
  37. //!
  38. namespace boost {
  39. namespace interprocess {
  40. namespace ipcdetail {
  41. template<class BasicManagedMemoryImpl>
  42. class create_open_func;
  43. template<
  44. class CharType,
  45. class MemoryAlgorithm,
  46. template<class IndexConfig> class IndexType
  47. >
  48. struct segment_manager_type
  49. {
  50. typedef segment_manager<CharType, MemoryAlgorithm, IndexType> type;
  51. };
  52. //!This class is designed to be a base class to classes that manage
  53. //!creation of objects in a fixed size memory buffer. Apart
  54. //!from allocating raw memory, the user can construct named objects. To
  55. //!achieve this, this class uses the reserved space provided by the allocation
  56. //!algorithm to place a named_allocator_algo, who takes care of name mappings.
  57. //!The class can be customized with the char type used for object names
  58. //!and the memory allocation algorithm to be used.*/
  59. template < class CharType
  60. , class MemoryAlgorithm
  61. , template<class IndexConfig> class IndexType
  62. , std::size_t Offset = 0
  63. >
  64. class basic_managed_memory_impl
  65. {
  66. //Non-copyable
  67. basic_managed_memory_impl(const basic_managed_memory_impl &);
  68. basic_managed_memory_impl &operator=(const basic_managed_memory_impl &);
  69. template<class BasicManagedMemoryImpl>
  70. friend class create_open_func;
  71. public:
  72. typedef typename segment_manager_type
  73. <CharType, MemoryAlgorithm, IndexType>::type segment_manager;
  74. typedef CharType char_type;
  75. typedef MemoryAlgorithm memory_algorithm;
  76. typedef typename MemoryAlgorithm::mutex_family mutex_family;
  77. typedef CharType char_t;
  78. typedef typename MemoryAlgorithm::size_type size_type;
  79. typedef typename MemoryAlgorithm::difference_type difference_type;
  80. typedef difference_type handle_t;
  81. typedef typename segment_manager::
  82. const_named_iterator const_named_iterator;
  83. typedef typename segment_manager::
  84. const_unique_iterator const_unique_iterator;
  85. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  86. typedef typename
  87. segment_manager::char_ptr_holder_t char_ptr_holder_t;
  88. //Experimental. Don't use.
  89. typedef typename segment_manager::multiallocation_chain multiallocation_chain;
  90. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  91. static const size_type PayloadPerAllocation = segment_manager::PayloadPerAllocation;
  92. private:
  93. typedef basic_managed_memory_impl
  94. <CharType, MemoryAlgorithm, IndexType, Offset> self_t;
  95. protected:
  96. template<class ManagedMemory>
  97. static bool grow(const char *filename, size_type extra_bytes)
  98. {
  99. typedef typename ManagedMemory::device_type device_type;
  100. //Increase file size
  101. try{
  102. offset_t old_size;
  103. {
  104. device_type f(open_or_create, filename, read_write);
  105. if(!f.get_size(old_size))
  106. return false;
  107. f.truncate(old_size + extra_bytes);
  108. }
  109. ManagedMemory managed_memory(open_only, filename);
  110. //Grow always works
  111. managed_memory.self_t::grow(extra_bytes);
  112. }
  113. catch(...){
  114. return false;
  115. }
  116. return true;
  117. }
  118. template<class ManagedMemory>
  119. static bool shrink_to_fit(const char *filename)
  120. {
  121. typedef typename ManagedMemory::device_type device_type;
  122. size_type new_size;
  123. try{
  124. ManagedMemory managed_memory(open_only, filename);
  125. managed_memory.get_size();
  126. managed_memory.self_t::shrink_to_fit();
  127. new_size = managed_memory.get_size();
  128. }
  129. catch(...){
  130. return false;
  131. }
  132. //Decrease file size
  133. {
  134. device_type f(open_or_create, filename, read_write);
  135. f.truncate(new_size);
  136. }
  137. return true;
  138. }
  139. //!Constructor. Allocates basic resources. Never throws.
  140. basic_managed_memory_impl()
  141. : mp_header(0){}
  142. //!Destructor. Calls close. Never throws.
  143. ~basic_managed_memory_impl()
  144. { this->close_impl(); }
  145. //!Places segment manager in the reserved space. This can throw.
  146. bool create_impl (void *addr, size_type size)
  147. {
  148. if(mp_header) return false;
  149. //Check if there is enough space
  150. if(size < segment_manager::get_min_size())
  151. return false;
  152. //This function should not throw. The index construction can
  153. //throw if constructor allocates memory. So we must catch it.
  154. BOOST_TRY{
  155. //Let's construct the allocator in memory
  156. mp_header = ::new(addr, boost_container_new_t()) segment_manager(size);
  157. }
  158. BOOST_CATCH(...){
  159. return false;
  160. }
  161. BOOST_CATCH_END
  162. return true;
  163. }
  164. //!Connects to a segment manager in the reserved buffer. Never throws.
  165. bool open_impl (void *addr, size_type)
  166. {
  167. if(mp_header) return false;
  168. mp_header = static_cast<segment_manager*>(addr);
  169. return true;
  170. }
  171. //!Frees resources. Never throws.
  172. bool close_impl()
  173. {
  174. bool ret = mp_header != 0;
  175. mp_header = 0;
  176. return ret;
  177. }
  178. //!Frees resources and destroys common resources. Never throws.
  179. bool destroy_impl()
  180. {
  181. if(mp_header == 0)
  182. return false;
  183. mp_header->~segment_manager();
  184. this->close_impl();
  185. return true;
  186. }
  187. //!
  188. void grow(size_type extra_bytes)
  189. { mp_header->grow(extra_bytes); }
  190. void shrink_to_fit()
  191. { mp_header->shrink_to_fit(); }
  192. public:
  193. //!Returns segment manager. Never throws.
  194. segment_manager *get_segment_manager() const
  195. { return mp_header; }
  196. //!Returns the base address of the memory in this process. Never throws.
  197. void * get_address () const
  198. { return reinterpret_cast<char*>(mp_header) - Offset; }
  199. //!Returns the size of memory segment. Never throws.
  200. size_type get_size () const
  201. { return mp_header->get_size() + Offset; }
  202. //!Returns the number of free bytes of the memory
  203. //!segment
  204. size_type get_free_memory() const
  205. { return mp_header->get_free_memory(); }
  206. //!Returns the result of "all_memory_deallocated()" function
  207. //!of the used memory algorithm
  208. bool all_memory_deallocated()
  209. { return mp_header->all_memory_deallocated(); }
  210. //!Returns the result of "check_sanity()" function
  211. //!of the used memory algorithm
  212. bool check_sanity()
  213. { return mp_header->check_sanity(); }
  214. //!Writes to zero free memory (memory not yet allocated) of
  215. //!the memory algorithm
  216. void zero_free_memory()
  217. { mp_header->zero_free_memory(); }
  218. //!Transforms an absolute address into an offset from base address.
  219. //!The address must belong to the memory segment. Never throws.
  220. handle_t get_handle_from_address (const void *ptr) const
  221. {
  222. return (handle_t)(reinterpret_cast<const char*>(ptr) -
  223. reinterpret_cast<const char*>(this->get_address()));
  224. }
  225. //!Returns true if the address belongs to the managed memory segment
  226. bool belongs_to_segment (const void *ptr) const
  227. {
  228. return ptr >= this->get_address() &&
  229. ptr < (reinterpret_cast<const char*>(this->get_address()) + this->get_size());
  230. }
  231. //!Transforms previously obtained offset into an absolute address in the
  232. //!process space of the current process. Never throws.*/
  233. void * get_address_from_handle (handle_t offset) const
  234. { return reinterpret_cast<char*>(this->get_address()) + offset; }
  235. //!Searches for nbytes of free memory in the segment, marks the
  236. //!memory as used and return the pointer to the memory. If no
  237. //!memory is available throws a boost::interprocess::bad_alloc exception
  238. void* allocate (size_type nbytes)
  239. { return mp_header->allocate(nbytes); }
  240. //!Searches for nbytes of free memory in the segment, marks the
  241. //!memory as used and return the pointer to the memory. If no memory
  242. //!is available returns 0. Never throws.
  243. void* allocate (size_type nbytes, const std::nothrow_t &tag)
  244. { return mp_header->allocate(nbytes, tag); }
  245. //!Allocates nbytes bytes aligned to "alignment" bytes. "alignment"
  246. //!must be power of two. If no memory
  247. //!is available returns 0. Never throws.
  248. void * allocate_aligned (size_type nbytes, size_type alignment, const std::nothrow_t &tag)
  249. { return mp_header->allocate_aligned(nbytes, alignment, tag); }
  250. template<class T>
  251. T * allocation_command (boost::interprocess::allocation_type command, size_type limit_size,
  252. size_type &prefer_in_recvd_out_size, T *&reuse)
  253. { return mp_header->allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); }
  254. //!Allocates nbytes bytes aligned to "alignment" bytes. "alignment"
  255. //!must be power of two. If no
  256. //!memory is available throws a boost::interprocess::bad_alloc exception
  257. void * allocate_aligned(size_type nbytes, size_type alignment)
  258. { return mp_header->allocate_aligned(nbytes, alignment); }
  259. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  260. //Experimental. Don't use.
  261. //!Allocates n_elements of elem_bytes bytes.
  262. //!Throws bad_alloc on failure. chain.size() is not increased on failure.
  263. void allocate_many(size_type elem_bytes, size_type n_elements, multiallocation_chain &chain)
  264. { mp_header->allocate_many(elem_bytes, n_elements, chain); }
  265. //!Allocates n_elements, each one of element_lengths[i]*sizeof_element bytes.
  266. //!Throws bad_alloc on failure. chain.size() is not increased on failure.
  267. void allocate_many(const size_type *element_lengths, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
  268. { mp_header->allocate_many(element_lengths, n_elements, sizeof_element, chain); }
  269. //!Allocates n_elements of elem_bytes bytes.
  270. //!Non-throwing version. chain.size() is not increased on failure.
  271. void allocate_many(const std::nothrow_t &tag, size_type elem_bytes, size_type n_elements, multiallocation_chain &chain)
  272. { mp_header->allocate_many(tag, elem_bytes, n_elements, chain); }
  273. //!Allocates n_elements, each one of
  274. //!element_lengths[i]*sizeof_element bytes.
  275. //!Non-throwing version. chain.size() is not increased on failure.
  276. void allocate_many(const std::nothrow_t &tag, const size_type *elem_sizes, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
  277. { mp_header->allocate_many(tag, elem_sizes, n_elements, sizeof_element, chain); }
  278. //!Deallocates all elements contained in chain.
  279. //!Never throws.
  280. void deallocate_many(multiallocation_chain &chain)
  281. { mp_header->deallocate_many(chain); }
  282. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  283. //!Marks previously allocated memory as free. Never throws.
  284. void deallocate (void *addr)
  285. { if (mp_header) mp_header->deallocate(addr); }
  286. //!Tries to find a previous named allocation address. Returns a memory
  287. //!buffer and the object count. If not found returned pointer is 0.
  288. //!Never throws.
  289. template <class T>
  290. std::pair<T*, size_type> find (char_ptr_holder_t name)
  291. { return mp_header->template find<T>(name); }
  292. //!Creates a named object or array in memory
  293. //!
  294. //!Allocates and constructs a T object or an array of T in memory,
  295. //!associates this with the given name and returns a pointer to the
  296. //!created object. If an array is being constructed all objects are
  297. //!created using the same parameters given to this function.
  298. //!
  299. //!-> If the name was previously used, returns 0.
  300. //!
  301. //!-> Throws boost::interprocess::bad_alloc if there is no available memory
  302. //!
  303. //!-> If T's constructor throws, the function throws that exception.
  304. //!
  305. //!Memory is freed automatically if T's constructor throws and if an
  306. //!array was being constructed, destructors of created objects are called
  307. //!before freeing the memory.
  308. template <class T>
  309. typename segment_manager::template construct_proxy<T>::type
  310. construct(char_ptr_holder_t name)
  311. { return mp_header->template construct<T>(name); }
  312. //!Finds or creates a named object or array in memory
  313. //!
  314. //!Tries to find an object with the given name in memory. If
  315. //!found, returns the pointer to this pointer. If the object is not found,
  316. //!allocates and constructs a T object or an array of T in memory,
  317. //!associates this with the given name and returns a pointer to the
  318. //!created object. If an array is being constructed all objects are
  319. //!created using the same parameters given to this function.
  320. //!
  321. //!-> Throws boost::interprocess::bad_alloc if there is no available memory
  322. //!
  323. //!-> If T's constructor throws, the function throws that exception.
  324. //!
  325. //!Memory is freed automatically if T's constructor throws and if an
  326. //!array was being constructed, destructors of created objects are called
  327. //!before freeing the memory.
  328. template <class T>
  329. typename segment_manager::template construct_proxy<T>::type
  330. find_or_construct(char_ptr_holder_t name)
  331. { return mp_header->template find_or_construct<T>(name); }
  332. //!Creates a named object or array in memory
  333. //!
  334. //!Allocates and constructs a T object or an array of T in memory,
  335. //!associates this with the given name and returns a pointer to the
  336. //!created object. If an array is being constructed all objects are
  337. //!created using the same parameters given to this function.
  338. //!
  339. //!-> If the name was previously used, returns 0.
  340. //!
  341. //!-> Returns 0 if there is no available memory
  342. //!
  343. //!-> If T's constructor throws, the function throws that exception.
  344. //!
  345. //!Memory is freed automatically if T's constructor throws and if an
  346. //!array was being constructed, destructors of created objects are called
  347. //!before freeing the memory.
  348. template <class T>
  349. typename segment_manager::template construct_proxy<T>::type
  350. construct(char_ptr_holder_t name, const std::nothrow_t &tag)
  351. { return mp_header->template construct<T>(name, tag); }
  352. //!Finds or creates a named object or array in memory
  353. //!
  354. //!Tries to find an object with the given name in memory. If
  355. //!found, returns the pointer to this pointer. If the object is not found,
  356. //!allocates and constructs a T object or an array of T in memory,
  357. //!associates this with the given name and returns a pointer to the
  358. //!created object. If an array is being constructed all objects are
  359. //!created using the same parameters given to this function.
  360. //!
  361. //!-> Returns 0 if there is no available memory
  362. //!
  363. //!-> If T's constructor throws, the function throws that exception.
  364. //!
  365. //!Memory is freed automatically if T's constructor throws and if an
  366. //!array was being constructed, destructors of created objects are called
  367. //!before freeing the memory.
  368. template <class T>
  369. typename segment_manager::template construct_proxy<T>::type
  370. find_or_construct(char_ptr_holder_t name, const std::nothrow_t &tag)
  371. { return mp_header->template find_or_construct<T>(name, tag); }
  372. //!Creates a named array from iterators in memory
  373. //!
  374. //!Allocates and constructs an array of T in memory,
  375. //!associates this with the given name and returns a pointer to the
  376. //!created object. Each element in the array is created using the
  377. //!objects returned when dereferencing iterators as parameters
  378. //!and incrementing all iterators for each element.
  379. //!
  380. //!-> If the name was previously used, returns 0.
  381. //!
  382. //!-> Throws boost::interprocess::bad_alloc if there is no available memory
  383. //!
  384. //!-> If T's constructor throws, the function throws that exception.
  385. //!
  386. //!Memory is freed automatically if T's constructor throws and
  387. //!destructors of created objects are called before freeing the memory.
  388. template <class T>
  389. typename segment_manager::template construct_iter_proxy<T>::type
  390. construct_it(char_ptr_holder_t name)
  391. { return mp_header->template construct_it<T>(name); }
  392. //!Finds or creates a named array from iterators in memory
  393. //!
  394. //!Tries to find an object with the given name in memory. If
  395. //!found, returns the pointer to this pointer. If the object is not found,
  396. //!allocates and constructs an array of T in memory,
  397. //!associates this with the given name and returns a pointer to the
  398. //!created object. Each element in the array is created using the
  399. //!objects returned when dereferencing iterators as parameters
  400. //!and incrementing all iterators for each element.
  401. //!
  402. //!-> If the name was previously used, returns 0.
  403. //!
  404. //!-> Throws boost::interprocess::bad_alloc if there is no available memory
  405. //!
  406. //!-> If T's constructor throws, the function throws that exception.
  407. //!
  408. //!Memory is freed automatically if T's constructor throws and
  409. //!destructors of created objects are called before freeing the memory.
  410. template <class T>
  411. typename segment_manager::template construct_iter_proxy<T>::type
  412. find_or_construct_it(char_ptr_holder_t name)
  413. { return mp_header->template find_or_construct_it<T>(name); }
  414. //!Creates a named array from iterators in memory
  415. //!
  416. //!Allocates and constructs an array of T in memory,
  417. //!associates this with the given name and returns a pointer to the
  418. //!created object. Each element in the array is created using the
  419. //!objects returned when dereferencing iterators as parameters
  420. //!and incrementing all iterators for each element.
  421. //!
  422. //!-> If the name was previously used, returns 0.
  423. //!
  424. //!-> If there is no available memory, returns 0.
  425. //!
  426. //!-> If T's constructor throws, the function throws that exception.
  427. //!
  428. //!Memory is freed automatically if T's constructor throws and
  429. //!destructors of created objects are called before freeing the memory.*/
  430. template <class T>
  431. typename segment_manager::template construct_iter_proxy<T>::type
  432. construct_it(char_ptr_holder_t name, const std::nothrow_t &tag)
  433. { return mp_header->template construct_it<T>(name, tag); }
  434. //!Finds or creates a named array from iterators in memory
  435. //!
  436. //!Tries to find an object with the given name in memory. If
  437. //!found, returns the pointer to this pointer. If the object is not found,
  438. //!allocates and constructs an array of T in memory,
  439. //!associates this with the given name and returns a pointer to the
  440. //!created object. Each element in the array is created using the
  441. //!objects returned when dereferencing iterators as parameters
  442. //!and incrementing all iterators for each element.
  443. //!
  444. //!-> If the name was previously used, returns 0.
  445. //!
  446. //!-> If there is no available memory, returns 0.
  447. //!
  448. //!-> If T's constructor throws, the function throws that exception.
  449. //!
  450. //!Memory is freed automatically if T's constructor throws and
  451. //!destructors of created objects are called before freeing the memory.*/
  452. template <class T>
  453. typename segment_manager::template construct_iter_proxy<T>::type
  454. find_or_construct_it(char_ptr_holder_t name, const std::nothrow_t &tag)
  455. { return mp_header->template find_or_construct_it<T>(name, tag); }
  456. //!Calls a functor and guarantees that no new construction, search or
  457. //!destruction will be executed by any process while executing the object
  458. //!function call. If the functor throws, this function throws.
  459. template <class Func>
  460. void atomic_func(Func &f)
  461. { mp_header->atomic_func(f); }
  462. //!Tries to call a functor guaranteeing that no new construction, search or
  463. //!destruction will be executed by any process while executing the object
  464. //!function call. If the atomic function can't be immediatelly executed
  465. //!because the internal mutex is already locked, returns false.
  466. //!If the functor throws, this function throws.
  467. template <class Func>
  468. bool try_atomic_func(Func &f)
  469. { return mp_header->try_atomic_func(f); }
  470. //!Destroys a named memory object or array.
  471. //!
  472. //!Finds the object with the given name, calls its destructors,
  473. //!frees used memory and returns true.
  474. //!
  475. //!-> If the object is not found, it returns false.
  476. //!
  477. //!Exception Handling:
  478. //!
  479. //!When deleting a dynamically object or array, the Standard
  480. //!does not guarantee that dynamically allocated memory, will be released.
  481. //!Also, when deleting arrays, the Standard doesn't require calling
  482. //!destructors for the rest of the objects if for one of them the destructor
  483. //!terminated with an exception.
  484. //!
  485. //!Destroying an object:
  486. //!
  487. //!If the destructor throws, the memory will be freed and that exception
  488. //!will be thrown.
  489. //!
  490. //!Destroying an array:
  491. //!
  492. //!When destroying an array, if a destructor throws, the rest of
  493. //!destructors are called. If any of these throws, the exceptions are
  494. //!ignored. The name association will be erased, memory will be freed and
  495. //!the first exception will be thrown. This guarantees the unlocking of
  496. //!mutexes and other resources.
  497. //!
  498. //!For all theses reasons, classes with throwing destructors are not
  499. //!recommended.
  500. template <class T>
  501. bool destroy(const CharType *name)
  502. { return mp_header->template destroy<T>(name); }
  503. //!Destroys the unique instance of type T
  504. //!
  505. //!Calls the destructor, frees used memory and returns true.
  506. //!
  507. //!Exception Handling:
  508. //!
  509. //!When deleting a dynamically object, the Standard does not
  510. //!guarantee that dynamically allocated memory will be released.
  511. //!
  512. //!Destroying an object:
  513. //!
  514. //!If the destructor throws, the memory will be freed and that exception
  515. //!will be thrown.
  516. //!
  517. //!For all theses reasons, classes with throwing destructors are not
  518. //!recommended for memory.
  519. template <class T>
  520. bool destroy(const unique_instance_t *const )
  521. { return mp_header->template destroy<T>(unique_instance); }
  522. //!Destroys the object (named, unique, or anonymous)
  523. //!
  524. //!Calls the destructor, frees used memory and returns true.
  525. //!
  526. //!Exception Handling:
  527. //!
  528. //!When deleting a dynamically object, the Standard does not
  529. //!guarantee that dynamically allocated memory will be released.
  530. //!
  531. //!Destroying an object:
  532. //!
  533. //!If the destructor throws, the memory will be freed and that exception
  534. //!will be thrown.
  535. //!
  536. //!For all theses reasons, classes with throwing destructors are not
  537. //!recommended for memory.
  538. template <class T>
  539. void destroy_ptr(const T *ptr)
  540. { mp_header->template destroy_ptr<T>(ptr); }
  541. //!Returns the name of an object created with construct/find_or_construct
  542. //!functions. If ptr points to an unique instance typeid(T).name() is returned.
  543. template<class T>
  544. static const char_type *get_instance_name(const T *ptr)
  545. { return segment_manager::get_instance_name(ptr); }
  546. //!Returns is the type an object created with construct/find_or_construct
  547. //!functions. Does not throw.
  548. template<class T>
  549. static instance_type get_instance_type(const T *ptr)
  550. { return segment_manager::get_instance_type(ptr); }
  551. //!Returns the length of an object created with construct/find_or_construct
  552. //!functions (1 if is a single element, >=1 if it's an array). Does not throw.
  553. template<class T>
  554. static size_type get_instance_length(const T *ptr)
  555. { return segment_manager::get_instance_length(ptr); }
  556. //!Preallocates needed index resources to optimize the
  557. //!creation of "num" named objects in the memory segment.
  558. //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
  559. void reserve_named_objects(size_type num)
  560. { mp_header->reserve_named_objects(num); }
  561. //!Preallocates needed index resources to optimize the
  562. //!creation of "num" unique objects in the memory segment.
  563. //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
  564. void reserve_unique_objects(size_type num)
  565. { mp_header->reserve_unique_objects(num); }
  566. //!Calls shrink_to_fit in both named and unique object indexes
  567. //to try to free unused memory from those indexes.
  568. void shrink_to_fit_indexes()
  569. { mp_header->shrink_to_fit_indexes(); }
  570. //!Returns the number of named objects stored
  571. //!in the managed segment.
  572. size_type get_num_named_objects()
  573. { return mp_header->get_num_named_objects(); }
  574. //!Returns the number of unique objects stored
  575. //!in the managed segment.
  576. size_type get_num_unique_objects()
  577. { return mp_header->get_num_unique_objects(); }
  578. //!Returns a constant iterator to the index storing the
  579. //!named allocations. NOT thread-safe. Never throws.
  580. const_named_iterator named_begin() const
  581. { return mp_header->named_begin(); }
  582. //!Returns a constant iterator to the end of the index
  583. //!storing the named allocations. NOT thread-safe. Never throws.
  584. const_named_iterator named_end() const
  585. { return mp_header->named_end(); }
  586. //!Returns a constant iterator to the index storing the
  587. //!unique allocations. NOT thread-safe. Never throws.
  588. const_unique_iterator unique_begin() const
  589. { return mp_header->unique_begin(); }
  590. //!Returns a constant iterator to the end of the index
  591. //!storing the unique allocations. NOT thread-safe. Never throws.
  592. const_unique_iterator unique_end() const
  593. { return mp_header->unique_end(); }
  594. //!This is the default allocator to allocate types T
  595. //!from this managed segment
  596. template<class T>
  597. struct allocator
  598. {
  599. typedef typename segment_manager::template allocator<T>::type type;
  600. };
  601. //!Returns an instance of the default allocator for type T
  602. //!initialized that allocates memory from this segment manager.
  603. template<class T>
  604. typename allocator<T>::type
  605. get_allocator()
  606. { return mp_header->template get_allocator<T>(); }
  607. //!This is the default deleter to delete types T
  608. //!from this managed segment.
  609. template<class T>
  610. struct deleter
  611. {
  612. typedef typename segment_manager::template deleter<T>::type type;
  613. };
  614. //!Returns an instance of the default allocator for type T
  615. //!initialized that allocates memory from this segment manager.
  616. template<class T>
  617. typename deleter<T>::type
  618. get_deleter()
  619. { return mp_header->template get_deleter<T>(); }
  620. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  621. //!Tries to find a previous named allocation address. Returns a memory
  622. //!buffer and the object count. If not found returned pointer is 0.
  623. //!Never throws.
  624. template <class T>
  625. std::pair<T*, size_type> find_no_lock (char_ptr_holder_t name)
  626. { return mp_header->template find_no_lock<T>(name); }
  627. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  628. protected:
  629. //!Swaps the segment manager's managed by this managed memory segment.
  630. //!NOT thread-safe. Never throws.
  631. void swap(basic_managed_memory_impl &other)
  632. { (simple_swap)(mp_header, other.mp_header); }
  633. private:
  634. segment_manager *mp_header;
  635. };
  636. template<class BasicManagedMemoryImpl>
  637. class create_open_func
  638. {
  639. typedef typename BasicManagedMemoryImpl::size_type size_type;
  640. public:
  641. create_open_func(BasicManagedMemoryImpl * const frontend, create_enum_t type)
  642. : m_frontend(frontend), m_type(type){}
  643. bool operator()(void *addr, std::size_t size, bool created) const
  644. {
  645. if( ((m_type == DoOpen) && created) ||
  646. ((m_type == DoCreate) && !created) ||
  647. //Check for overflow
  648. size_type(-1) < size ){
  649. return false;
  650. }
  651. else if(created){
  652. return m_frontend->create_impl(addr, static_cast<size_type>(size));
  653. }
  654. else{
  655. return m_frontend->open_impl (addr, static_cast<size_type>(size));
  656. }
  657. }
  658. static std::size_t get_min_size()
  659. {
  660. const size_type sz = BasicManagedMemoryImpl::segment_manager::get_min_size();
  661. if(sz > std::size_t(-1)){
  662. //The minimum size is not representable by std::size_t
  663. BOOST_ASSERT(false);
  664. return std::size_t(-1);
  665. }
  666. else{
  667. return static_cast<std::size_t>(sz);
  668. }
  669. }
  670. private:
  671. BasicManagedMemoryImpl *m_frontend;
  672. create_enum_t m_type;
  673. };
  674. } //namespace ipcdetail {
  675. } //namespace interprocess {
  676. } //namespace boost {
  677. #include <boost/interprocess/detail/config_end.hpp>
  678. #endif //BOOST_INTERPROCESS_DETAIL_MANAGED_MEMORY_IMPL_HPP