1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP
12 #define BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/core/no_exceptions_support.hpp>
26 #include <boost/interprocess/detail/type_traits.hpp>
28 #include <boost/interprocess/detail/transform_iterator.hpp>
30 #include <boost/interprocess/detail/mpl.hpp>
31 #include <boost/interprocess/detail/nothrow.hpp>
32 #include <boost/interprocess/detail/segment_manager_helper.hpp>
33 #include <boost/interprocess/detail/named_proxy.hpp>
34 #include <boost/interprocess/detail/utilities.hpp>
35 #include <boost/interprocess/offset_ptr.hpp>
36 #include <boost/interprocess/indexes/iset_index.hpp>
37 #include <boost/interprocess/exceptions.hpp>
38 #include <boost/interprocess/allocators/allocator.hpp>
39 #include <boost/interprocess/smart_ptr/deleter.hpp>
40 #include <boost/move/utility_core.hpp>
41 #include <boost/interprocess/sync/scoped_lock.hpp>
43 #include <boost/container/detail/minimal_char_traits_header.hpp>
44 #include <boost/container/detail/placement_new.hpp>
46 #include <cstddef> //std::size_t
47 #include <boost/intrusive/detail/minimal_pair_header.hpp>
48 #include <boost/assert.hpp>
49 #ifndef BOOST_NO_EXCEPTIONS
54 //!Describes the object placed in a memory segment that provides
55 //!named object allocation capabilities for single-segment and
56 //!multi-segment allocations.
59 namespace interprocess{
61 //!This object is the public base class of segment manager.
62 //!This class only depends on the memory allocation algorithm
63 //!and implements all the allocation features not related
64 //!to named or unique objects.
66 //!Storing a reference to segment_manager forces
67 //!the holder class to be dependent on index types and character types.
68 //!When such dependence is not desirable and only anonymous and raw
69 //!allocations are needed, segment_manager_base is the correct answer.
70 template<class MemoryAlgorithm>
71 class segment_manager_base
72 : private MemoryAlgorithm
75 typedef segment_manager_base<MemoryAlgorithm> segment_manager_base_type;
76 typedef typename MemoryAlgorithm::void_pointer void_pointer;
77 typedef typename MemoryAlgorithm::mutex_family mutex_family;
78 typedef MemoryAlgorithm memory_algorithm;
80 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
82 //Experimental. Don't use
83 typedef typename MemoryAlgorithm::multiallocation_chain multiallocation_chain;
84 typedef typename MemoryAlgorithm::difference_type difference_type;
85 typedef typename MemoryAlgorithm::size_type size_type;
87 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
89 //!This constant indicates the payload size
90 //!associated with each allocation of the memory algorithm
91 static const size_type PayloadPerAllocation = MemoryAlgorithm::PayloadPerAllocation;
93 //!Constructor of the segment_manager_base
95 //!"size" is the size of the memory segment where
96 //!the basic segment manager is being constructed.
98 //!"reserved_bytes" is the number of bytes
99 //!after the end of the memory algorithm object itself
100 //!that the memory algorithm will exclude from
101 //!dynamic allocation
104 segment_manager_base(size_type sz, size_type reserved_bytes)
105 : MemoryAlgorithm(sz, reserved_bytes)
107 BOOST_ASSERT((sizeof(segment_manager_base<MemoryAlgorithm>) == sizeof(MemoryAlgorithm)));
110 //!Returns the size of the memory
112 size_type get_size() const
113 { return MemoryAlgorithm::get_size(); }
115 //!Returns the number of free bytes of the memory
117 size_type get_free_memory() const
118 { return MemoryAlgorithm::get_free_memory(); }
120 //!Obtains the minimum size needed by
121 //!the segment manager
122 static size_type get_min_size (size_type size)
123 { return MemoryAlgorithm::get_min_size(size); }
125 //!Allocates nbytes bytes. This function is only used in
126 //!single-segment management. Never throws
127 void * allocate (size_type nbytes, const std::nothrow_t &)
128 { return MemoryAlgorithm::allocate(nbytes); }
130 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
132 //Experimental. Dont' use.
133 //!Allocates n_elements of elem_bytes bytes.
134 //!Throws bad_alloc on failure. chain.size() is not increased on failure.
135 void allocate_many(size_type elem_bytes, size_type n_elements, multiallocation_chain &chain)
137 size_type prev_size = chain.size();
138 MemoryAlgorithm::allocate_many(elem_bytes, n_elements, chain);
139 if(!elem_bytes || chain.size() == prev_size){
144 //!Allocates n_elements, each one of element_lengths[i]*sizeof_element bytes.
145 //!Throws bad_alloc on failure. chain.size() is not increased on failure.
146 void allocate_many(const size_type *element_lengths, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
148 size_type prev_size = chain.size();
149 MemoryAlgorithm::allocate_many(element_lengths, n_elements, sizeof_element, chain);
150 if(!sizeof_element || chain.size() == prev_size){
155 //!Allocates n_elements of elem_bytes bytes.
156 //!Non-throwing version. chain.size() is not increased on failure.
157 void allocate_many(const std::nothrow_t &, size_type elem_bytes, size_type n_elements, multiallocation_chain &chain)
158 { MemoryAlgorithm::allocate_many(elem_bytes, n_elements, chain); }
160 //!Allocates n_elements, each one of
161 //!element_lengths[i]*sizeof_element bytes.
162 //!Non-throwing version. chain.size() is not increased on failure.
163 void allocate_many(const std::nothrow_t &, const size_type *elem_sizes, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain)
164 { MemoryAlgorithm::allocate_many(elem_sizes, n_elements, sizeof_element, chain); }
166 //!Deallocates all elements contained in chain.
168 void deallocate_many(multiallocation_chain &chain)
169 { MemoryAlgorithm::deallocate_many(chain); }
171 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
173 //!Allocates nbytes bytes. Throws boost::interprocess::bad_alloc
175 void * allocate(size_type nbytes)
177 void * ret = MemoryAlgorithm::allocate(nbytes);
183 //!Allocates nbytes bytes. This function is only used in
184 //!single-segment management. Never throws
185 void * allocate_aligned (size_type nbytes, size_type alignment, const std::nothrow_t &)
186 { return MemoryAlgorithm::allocate_aligned(nbytes, alignment); }
188 //!Allocates nbytes bytes. This function is only used in
189 //!single-segment management. Throws bad_alloc when fails
190 void * allocate_aligned(size_type nbytes, size_type alignment)
192 void * ret = MemoryAlgorithm::allocate_aligned(nbytes, alignment);
198 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
201 T *allocation_command (boost::interprocess::allocation_type command, size_type limit_size,
202 size_type &prefer_in_recvd_out_size, T *&reuse)
204 T *ret = MemoryAlgorithm::allocation_command
205 (command | boost::interprocess::nothrow_allocation, limit_size, prefer_in_recvd_out_size, reuse);
206 if(!(command & boost::interprocess::nothrow_allocation) && !ret)
211 void *raw_allocation_command (boost::interprocess::allocation_type command, size_type limit_objects,
212 size_type &prefer_in_recvd_out_size, void *&reuse, size_type sizeof_object = 1)
214 void *ret = MemoryAlgorithm::raw_allocation_command
215 ( command | boost::interprocess::nothrow_allocation, limit_objects,
216 prefer_in_recvd_out_size, reuse, sizeof_object);
217 if(!(command & boost::interprocess::nothrow_allocation) && !ret)
222 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
224 //!Deallocates the bytes allocated with allocate/allocate_many()
226 void deallocate (void *addr)
227 { MemoryAlgorithm::deallocate(addr); }
229 //!Increases managed memory in extra_size bytes more. This only works
230 //!with single-segment management.
231 void grow(size_type extra_size)
232 { MemoryAlgorithm::grow(extra_size); }
234 //!Decreases managed memory to the minimum. This only works
235 //!with single-segment management.
237 { MemoryAlgorithm::shrink_to_fit(); }
239 //!Returns the result of "all_memory_deallocated()" function
240 //!of the used memory algorithm
241 bool all_memory_deallocated()
242 { return MemoryAlgorithm::all_memory_deallocated(); }
244 //!Returns the result of "check_sanity()" function
245 //!of the used memory algorithm
247 { return MemoryAlgorithm::check_sanity(); }
249 //!Writes to zero free memory (memory not yet allocated)
250 //!of the memory algorithm
251 void zero_free_memory()
252 { MemoryAlgorithm::zero_free_memory(); }
254 //!Returns the size of the buffer previously allocated pointed by ptr
255 size_type size(const void *ptr) const
256 { return MemoryAlgorithm::size(ptr); }
258 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
260 void * prot_anonymous_construct
261 (size_type num, bool dothrow, ipcdetail::in_place_interface &table)
263 typedef ipcdetail::block_header<size_type> block_header_t;
264 block_header_t block_info ( size_type(table.size*num)
265 , size_type(table.alignment)
271 void *ptr_struct = this->allocate(block_info.total_size(), nothrow<>::get());
273 //Check if there is enough memory
283 //Build scoped ptr to avoid leaks with constructor exception
284 ipcdetail::mem_algo_deallocator<MemoryAlgorithm> mem(ptr_struct, *this);
286 //Now construct the header
287 block_header_t * hdr = ::new(ptr_struct, boost_container_new_t()) block_header_t(block_info);
288 void *ptr = 0; //avoid gcc warning
291 //Now call constructors
292 ipcdetail::array_construct(ptr, num, table);
294 //All constructors successful, we don't want erase memory
299 //!Calls the destructor and makes an anonymous deallocate
300 void prot_anonymous_destroy(const void *object, ipcdetail::in_place_interface &table)
303 //Get control data from associated with this object
304 typedef ipcdetail::block_header<size_type> block_header_t;
305 block_header_t *ctrl_data = block_header_t::block_header_from_value(object, table.size, table.alignment);
307 //-------------------------------
308 //scoped_lock<rmutex> guard(m_header);
309 //-------------------------------
311 if(ctrl_data->alloc_type() != anonymous_type){
312 //This is not an anonymous object, the pointer is wrong!
316 //Call destructors and free memory
317 //Build scoped ptr to avoid leaks with destructor exception
318 std::size_t destroyed = 0;
319 table.destroy_n(const_cast<void*>(object), ctrl_data->m_value_bytes/table.size, destroyed);
320 this->deallocate(ctrl_data);
322 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
325 //!This object is placed in the beginning of memory segment and
326 //!implements the allocation (named or anonymous) of portions
327 //!of the segment. This object contains two indexes that
328 //!maintain an association between a name and a portion of the segment.
330 //!The first index contains the mappings for normal named objects using the
331 //!char type specified in the template parameter.
333 //!The second index contains the association for unique instances. The key will
334 //!be the const char * returned from type_info.name() function for the unique
335 //!type to be constructed.
337 //!segment_manager<CharType, MemoryAlgorithm, IndexType> inherits publicly
338 //!from segment_manager_base<MemoryAlgorithm> and inherits from it
339 //!many public functions related to anonymous object and raw memory allocation.
340 //!See segment_manager_base reference to know about those functions.
341 template<class CharType
342 ,class MemoryAlgorithm
343 ,template<class IndexConfig> class IndexType>
344 class segment_manager
345 : public segment_manager_base<MemoryAlgorithm>
347 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
350 segment_manager(const segment_manager &);
351 segment_manager &operator=(const segment_manager &);
352 typedef segment_manager_base<MemoryAlgorithm> segment_manager_base_t;
353 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
356 typedef MemoryAlgorithm memory_algorithm;
357 typedef typename segment_manager_base_t::void_pointer void_pointer;
358 typedef typename segment_manager_base_t::size_type size_type;
359 typedef typename segment_manager_base_t::difference_type difference_type;
360 typedef CharType char_type;
362 typedef segment_manager_base<MemoryAlgorithm> segment_manager_base_type;
364 static const size_type PayloadPerAllocation = segment_manager_base_t::PayloadPerAllocation;
366 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
368 typedef ipcdetail::block_header<size_type> block_header_t;
369 typedef ipcdetail::index_config<CharType, MemoryAlgorithm> index_config_named;
370 typedef ipcdetail::index_config<char, MemoryAlgorithm> index_config_unique;
371 typedef IndexType<index_config_named> index_type;
372 typedef ipcdetail::bool_<is_intrusive_index<index_type>::value > is_intrusive_t;
373 typedef ipcdetail::bool_<is_node_index<index_type>::value> is_node_index_t;
376 typedef IndexType<index_config_named> named_index_t;
377 typedef IndexType<index_config_unique> unique_index_t;
378 typedef ipcdetail::char_ptr_holder<CharType> char_ptr_holder_t;
379 typedef ipcdetail::segment_manager_iterator_transform
380 <typename named_index_t::const_iterator
381 ,is_intrusive_index<index_type>::value> named_transform;
383 typedef ipcdetail::segment_manager_iterator_transform
384 <typename unique_index_t::const_iterator
385 ,is_intrusive_index<index_type>::value> unique_transform;
386 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
388 typedef typename segment_manager_base_t::mutex_family mutex_family;
390 typedef transform_iterator
391 <typename named_index_t::const_iterator, named_transform> const_named_iterator;
392 typedef transform_iterator
393 <typename unique_index_t::const_iterator, unique_transform> const_unique_iterator;
395 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
397 //!Constructor proxy object definition helper class
399 struct construct_proxy
401 typedef ipcdetail::named_proxy<segment_manager, T, false> type;
404 //!Constructor proxy object definition helper class
406 struct construct_iter_proxy
408 typedef ipcdetail::named_proxy<segment_manager, T, true> type;
411 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
413 //!Constructor of the segment manager
414 //!"size" is the size of the memory segment where
415 //!the segment manager is being constructed.
417 explicit segment_manager(size_type segment_size)
418 : segment_manager_base_t(segment_size, priv_get_reserved_bytes())
419 , m_header(static_cast<segment_manager_base_t*>(get_this_pointer()))
421 (void) anonymous_instance; (void) unique_instance;
422 //Check EBO is applied, it's required
423 const void * const this_addr = this;
424 const void *const segm_addr = static_cast<segment_manager_base_t*>(this);
425 (void)this_addr; (void)segm_addr;
426 BOOST_ASSERT( this_addr == segm_addr);
429 //!Tries to find a previous named/unique allocation. Returns the address
430 //!and the object count. On failure the first member of the
431 //!returned pair is 0.
433 std::pair<T*, size_type> find (char_ptr_holder_t name)
434 { return this->priv_find_impl<T>(name, true); }
436 //!Tries to find a previous named/unique allocation. Returns the address
437 //!and the object count. On failure the first member of the
438 //!returned pair is 0. This search is not mutex-protected!
439 //!Use it only inside atomic_func() calls, where the internal mutex
440 //!is guaranteed to be locked.
442 std::pair<T*, size_type> find_no_lock (char_ptr_holder_t name)
443 { return this->priv_find_impl<T>(name, false); }
445 //!Returns throwing "construct" proxy
448 typename construct_proxy<T>::type
449 construct(char_ptr_holder_t name)
450 { return typename construct_proxy<T>::type (this, name, false, true); }
452 //!Returns throwing "search or construct" proxy
455 typename construct_proxy<T>::type find_or_construct(char_ptr_holder_t name)
456 { return typename construct_proxy<T>::type (this, name, true, true); }
458 //!Returns no throwing "construct" proxy
461 typename construct_proxy<T>::type
462 construct(char_ptr_holder_t name, const std::nothrow_t &)
463 { return typename construct_proxy<T>::type (this, name, false, false); }
465 //!Returns no throwing "search or construct"
468 typename construct_proxy<T>::type
469 find_or_construct(char_ptr_holder_t name, const std::nothrow_t &)
470 { return typename construct_proxy<T>::type (this, name, true, false); }
472 //!Returns throwing "construct from iterators" proxy object
474 typename construct_iter_proxy<T>::type
475 construct_it(char_ptr_holder_t name)
476 { return typename construct_iter_proxy<T>::type (this, name, false, true); }
478 //!Returns throwing "search or construct from iterators"
481 typename construct_iter_proxy<T>::type
482 find_or_construct_it(char_ptr_holder_t name)
483 { return typename construct_iter_proxy<T>::type (this, name, true, true); }
485 //!Returns no throwing "construct from iterators"
488 typename construct_iter_proxy<T>::type
489 construct_it(char_ptr_holder_t name, const std::nothrow_t &)
490 { return typename construct_iter_proxy<T>::type (this, name, false, false); }
492 //!Returns no throwing "search or construct from iterators"
495 typename construct_iter_proxy<T>::type
496 find_or_construct_it(char_ptr_holder_t name, const std::nothrow_t &)
497 { return typename construct_iter_proxy<T>::type (this, name, true, false); }
499 //!Calls object function blocking recursive interprocess_mutex and guarantees that
500 //!no new named_alloc or destroy will be executed by any process while
501 //!executing the object function call
502 template <class Func>
503 void atomic_func(Func &f)
504 { scoped_lock<rmutex> guard(m_header); f(); }
506 //!Tries to calls a functor guaranteeing that no new construction, search or
507 //!destruction will be executed by any process while executing the object
508 //!function call. If the atomic function can't be immediatelly executed
509 //!because the internal mutex is already locked, returns false.
510 //!If the functor throws, this function throws.
511 template <class Func>
512 bool try_atomic_func(Func &f)
514 scoped_lock<rmutex> guard(m_header, try_to_lock);
524 //!Destroys a previously created named/unique instance.
525 //!Returns false if the object was not present.
527 bool destroy(char_ptr_holder_t name)
529 BOOST_ASSERT(!name.is_anonymous());
530 ipcdetail::placement_destroy<T> dtor;
532 if(name.is_unique()){
533 return this->priv_generic_named_destroy<char>
534 ( typeid(T).name(), m_header.m_unique_index , dtor, is_intrusive_t());
537 return this->priv_generic_named_destroy<CharType>
538 ( name.get(), m_header.m_named_index, dtor, is_intrusive_t());
542 //!Destroys an anonymous, unique or named object
545 void destroy_ptr(const T *p)
547 //If T is void transform it to char
548 typedef typename ipcdetail::char_if_void<T>::type data_t;
549 ipcdetail::placement_destroy<data_t> dtor;
550 priv_destroy_ptr(p, dtor);
553 //!Returns the name of an object created with construct/find_or_construct
554 //!functions. Does not throw
556 static const CharType *get_instance_name(const T *ptr)
557 { return priv_get_instance_name(block_header_t::block_header_from_value(ptr)); }
559 //!Returns the length of an object created with construct/find_or_construct
560 //!functions. Does not throw.
562 static size_type get_instance_length(const T *ptr)
563 { return priv_get_instance_length(block_header_t::block_header_from_value(ptr), sizeof(T)); }
565 //!Returns is the the name of an object created with construct/find_or_construct
566 //!functions. Does not throw
568 static instance_type get_instance_type(const T *ptr)
569 { return priv_get_instance_type(block_header_t::block_header_from_value(ptr)); }
571 //!Preallocates needed index resources to optimize the
572 //!creation of "num" named objects in the managed memory segment.
573 //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
574 void reserve_named_objects(size_type num)
576 //-------------------------------
577 scoped_lock<rmutex> guard(m_header);
578 //-------------------------------
579 m_header.m_named_index.reserve(num);
582 //!Preallocates needed index resources to optimize the
583 //!creation of "num" unique objects in the managed memory segment.
584 //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
585 void reserve_unique_objects(size_type num)
587 //-------------------------------
588 scoped_lock<rmutex> guard(m_header);
589 //-------------------------------
590 m_header.m_unique_index.reserve(num);
593 //!Calls shrink_to_fit in both named and unique object indexes
594 //!to try to free unused memory from those indexes.
595 void shrink_to_fit_indexes()
597 //-------------------------------
598 scoped_lock<rmutex> guard(m_header);
599 //-------------------------------
600 m_header.m_named_index.shrink_to_fit();
601 m_header.m_unique_index.shrink_to_fit();
604 //!Returns the number of named objects stored in
606 size_type get_num_named_objects()
608 //-------------------------------
609 scoped_lock<rmutex> guard(m_header);
610 //-------------------------------
611 return m_header.m_named_index.size();
614 //!Returns the number of unique objects stored in
616 size_type get_num_unique_objects()
618 //-------------------------------
619 scoped_lock<rmutex> guard(m_header);
620 //-------------------------------
621 return m_header.m_unique_index.size();
624 //!Obtains the minimum size needed by the
626 static size_type get_min_size()
627 { return segment_manager_base_t::get_min_size(priv_get_reserved_bytes()); }
629 //!Returns a constant iterator to the beginning of the information about
630 //!the named allocations performed in this segment manager
631 const_named_iterator named_begin() const
633 return make_transform_iterator
634 (m_header.m_named_index.begin(), named_transform());
637 //!Returns a constant iterator to the end of the information about
638 //!the named allocations performed in this segment manager
639 const_named_iterator named_end() const
641 return make_transform_iterator
642 (m_header.m_named_index.end(), named_transform());
645 //!Returns a constant iterator to the beginning of the information about
646 //!the unique allocations performed in this segment manager
647 const_unique_iterator unique_begin() const
649 return make_transform_iterator
650 (m_header.m_unique_index.begin(), unique_transform());
653 //!Returns a constant iterator to the end of the information about
654 //!the unique allocations performed in this segment manager
655 const_unique_iterator unique_end() const
657 return make_transform_iterator
658 (m_header.m_unique_index.end(), unique_transform());
661 //!This is the default allocator to allocate types T
662 //!from this managed segment
666 typedef boost::interprocess::allocator<T, segment_manager> type;
669 //!Returns an instance of the default allocator for type T
670 //!initialized that allocates memory from this segment manager.
672 typename allocator<T>::type
674 { return typename allocator<T>::type(this); }
676 //!This is the default deleter to delete types T
677 //!from this managed segment.
681 typedef boost::interprocess::deleter<T, segment_manager> type;
684 //!Returns an instance of the default deleter for type T
685 //!that will delete an object constructed in this segment manager.
687 typename deleter<T>::type
689 { return typename deleter<T>::type(this); }
691 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
693 //!Generic named/anonymous new function. Offers all the possibilities,
694 //!such as throwing, search before creating, and the constructor is
695 //!encapsulated in an object function.
697 T *generic_construct(const CharType *name,
701 ipcdetail::in_place_interface &table)
703 return static_cast<T*>
704 (priv_generic_construct(name, num, try2find, dothrow, table));
708 //!Tries to find a previous named allocation. Returns the address
709 //!and the object count. On failure the first member of the
710 //!returned pair is 0.
712 std::pair<T*, size_type> priv_find_impl (const CharType* name, bool lock)
714 //The name can't be null, no anonymous object can be found by name
715 BOOST_ASSERT(name != 0);
716 ipcdetail::placement_destroy<T> table;
720 if(name == reinterpret_cast<const CharType*>(-1)){
721 ret = priv_generic_find<char> (typeid(T).name(), m_header.m_unique_index, table, sz, is_intrusive_t(), lock);
724 ret = priv_generic_find<CharType> (name, m_header.m_named_index, table, sz, is_intrusive_t(), lock);
726 return std::pair<T*, size_type>(static_cast<T*>(ret), sz);
729 //!Tries to find a previous unique allocation. Returns the address
730 //!and the object count. On failure the first member of the
731 //!returned pair is 0.
733 std::pair<T*, size_type> priv_find_impl (const ipcdetail::unique_instance_t* name, bool lock)
735 ipcdetail::placement_destroy<T> table;
737 void *ret = priv_generic_find<char>(name, m_header.m_unique_index, table, size, is_intrusive_t(), lock);
738 return std::pair<T*, size_type>(static_cast<T*>(ret), size);
741 void *priv_generic_construct
742 (const CharType *name, size_type num, bool try2find, bool dothrow, ipcdetail::in_place_interface &table)
745 //Security overflow check
746 if(num > ((std::size_t)-1)/table.size){
753 ret = this->prot_anonymous_construct(num, dothrow, table);
755 else if(name == reinterpret_cast<const CharType*>(-1)){
756 ret = this->priv_generic_named_construct<char>
757 (unique_type, table.type_name, num, try2find, dothrow, table, m_header.m_unique_index, is_intrusive_t());
760 ret = this->priv_generic_named_construct<CharType>
761 (named_type, name, num, try2find, dothrow, table, m_header.m_named_index, is_intrusive_t());
766 void priv_destroy_ptr(const void *ptr, ipcdetail::in_place_interface &dtor)
768 block_header_t *ctrl_data = block_header_t::block_header_from_value(ptr, dtor.size, dtor.alignment);
769 switch(ctrl_data->alloc_type()){
771 this->prot_anonymous_destroy(ptr, dtor);
775 this->priv_generic_named_destroy<CharType>
776 (ctrl_data, m_header.m_named_index, dtor, is_node_index_t());
780 this->priv_generic_named_destroy<char>
781 (ctrl_data, m_header.m_unique_index, dtor, is_node_index_t());
785 //This type is unknown, bad pointer passed to this function!
791 //!Returns the name of an object created with construct/find_or_construct
792 //!functions. Does not throw
793 static const CharType *priv_get_instance_name(block_header_t *ctrl_data)
795 boost::interprocess::allocation_type type = ctrl_data->alloc_type();
796 if(type == anonymous_type){
797 BOOST_ASSERT((type == anonymous_type && ctrl_data->m_num_char == 0) ||
798 (type == unique_type && ctrl_data->m_num_char != 0) );
801 CharType *name = static_cast<CharType*>(ctrl_data->template name<CharType>());
804 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharType));
805 BOOST_ASSERT(ctrl_data->m_num_char == std::char_traits<CharType>::length(name));
809 static size_type priv_get_instance_length(block_header_t *ctrl_data, size_type sizeofvalue)
812 BOOST_ASSERT((ctrl_data->value_bytes() %sizeofvalue) == 0);
813 return ctrl_data->value_bytes()/sizeofvalue;
816 //!Returns is the the name of an object created with construct/find_or_construct
817 //!functions. Does not throw
818 static instance_type priv_get_instance_type(block_header_t *ctrl_data)
821 BOOST_ASSERT((instance_type)ctrl_data->alloc_type() < max_allocation_type);
822 return (instance_type)ctrl_data->alloc_type();
825 static size_type priv_get_reserved_bytes()
827 //Get the number of bytes until the end of (*this)
828 //beginning in the end of the segment_manager_base_t base.
829 return sizeof(segment_manager) - sizeof(segment_manager_base_t);
832 template <class CharT>
833 void *priv_generic_find
835 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
836 ipcdetail::in_place_interface &table,
837 size_type &length, ipcdetail::true_ is_intrusive, bool use_lock)
840 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
841 typedef typename index_type::iterator index_it;
843 //-------------------------------
844 scoped_lock<rmutex> guard(priv_get_lock(use_lock));
845 //-------------------------------
847 ipcdetail::intrusive_compare_key<CharT> key
848 (name, std::char_traits<CharT>::length(name));
849 index_it it = index.find(key);
851 //Initialize return values
855 //If found, assign values
856 if(it != index.end()){
858 block_header_t *ctrl_data = it->get_block_header();
861 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
862 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharT));
863 ret_ptr = ctrl_data->value();
864 length = ctrl_data->m_value_bytes/table.size;
869 template <class CharT>
870 void *priv_generic_find
872 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
873 ipcdetail::in_place_interface &table,
874 size_type &length, ipcdetail::false_ is_intrusive, bool use_lock)
877 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
878 typedef typename index_type::key_type key_type;
879 typedef typename index_type::iterator index_it;
881 //-------------------------------
882 scoped_lock<rmutex> guard(priv_get_lock(use_lock));
883 //-------------------------------
885 index_it it = index.find(key_type(name, std::char_traits<CharT>::length(name)));
887 //Initialize return values
891 //If found, assign values
892 if(it != index.end()){
894 block_header_t *ctrl_data = reinterpret_cast<block_header_t*>
895 (ipcdetail::to_raw_pointer(it->second.m_ptr));
898 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
899 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharT));
900 ret_ptr = ctrl_data->value();
901 length = ctrl_data->m_value_bytes/table.size;
906 template <class CharT>
907 bool priv_generic_named_destroy
908 (block_header_t *block_header,
909 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
910 ipcdetail::in_place_interface &table, ipcdetail::true_ is_node_index)
913 typedef typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator index_it;
915 index_it *ihdr = block_header_t::template to_first_header<index_it>(block_header);
916 return this->priv_generic_named_destroy_impl<CharT>(*ihdr, index, table);
919 template <class CharT>
920 bool priv_generic_named_destroy
921 (block_header_t *block_header,
922 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
923 ipcdetail::in_place_interface &table,
924 ipcdetail::false_ is_node_index)
927 CharT *name = static_cast<CharT*>(block_header->template name<CharT>());
928 return this->priv_generic_named_destroy<CharT>(name, index, table, is_intrusive_t());
931 template <class CharT>
932 bool priv_generic_named_destroy(const CharT *name,
933 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
934 ipcdetail::in_place_interface &table, ipcdetail::true_ is_intrusive_index)
936 (void)is_intrusive_index;
937 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
938 typedef typename index_type::iterator index_it;
939 typedef typename index_type::value_type intrusive_value_type;
941 //-------------------------------
942 scoped_lock<rmutex> guard(m_header);
943 //-------------------------------
945 ipcdetail::intrusive_compare_key<CharT> key
946 (name, std::char_traits<CharT>::length(name));
947 index_it it = index.find(key);
949 //If not found, return false
950 if(it == index.end()){
951 //This name is not present in the index, wrong pointer or name!
956 block_header_t *ctrl_data = it->get_block_header();
957 intrusive_value_type *iv = intrusive_value_type::get_intrusive_value_type(ctrl_data);
959 void *values = ctrl_data->value();
960 std::size_t num = ctrl_data->m_value_bytes/table.size;
963 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
964 BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char());
966 //Erase node from index
969 //Destroy the headers
970 ctrl_data->~block_header_t();
971 iv->~intrusive_value_type();
973 //Call destructors and free memory
974 std::size_t destroyed;
975 table.destroy_n(values, num, destroyed);
976 this->deallocate(memory);
980 template <class CharT>
981 bool priv_generic_named_destroy(const CharT *name,
982 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
983 ipcdetail::in_place_interface &table,
984 ipcdetail::false_ is_intrusive_index)
986 (void)is_intrusive_index;
987 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
988 typedef typename index_type::iterator index_it;
989 typedef typename index_type::key_type key_type;
991 //-------------------------------
992 scoped_lock<rmutex> guard(m_header);
993 //-------------------------------
994 //Try to find the name in the index
995 index_it it = index.find(key_type (name,
996 std::char_traits<CharT>::length(name)));
998 //If not found, return false
999 if(it == index.end()){
1000 //This name is not present in the index, wrong pointer or name!
1004 return this->priv_generic_named_destroy_impl<CharT>(it, index, table);
1007 template <class CharT>
1008 bool priv_generic_named_destroy_impl
1009 (const typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator &it,
1010 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
1011 ipcdetail::in_place_interface &table)
1013 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
1014 typedef typename index_type::iterator index_it;
1016 //Get allocation parameters
1017 block_header_t *ctrl_data = reinterpret_cast<block_header_t*>
1018 (ipcdetail::to_raw_pointer(it->second.m_ptr));
1019 char *stored_name = static_cast<char*>(static_cast<void*>(const_cast<CharT*>(it->first.name())));
1022 //Check if the distance between the name pointer and the memory pointer
1023 //is correct (this can detect incorrect type in destruction)
1024 std::size_t num = ctrl_data->m_value_bytes/table.size;
1025 void *values = ctrl_data->value();
1028 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
1029 BOOST_ASSERT(static_cast<void*>(stored_name) == static_cast<void*>(ctrl_data->template name<CharT>()));
1030 BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char());
1032 //Erase node from index
1035 //Destroy the header
1036 ctrl_data->~block_header_t();
1039 if(is_node_index_t::value){
1040 index_it *ihdr = block_header_t::template
1041 to_first_header<index_it>(ctrl_data);
1049 //Call destructors and free memory
1050 std::size_t destroyed;
1051 table.destroy_n(values, num, destroyed);
1052 this->deallocate(memory);
1056 template<class CharT>
1057 void * priv_generic_named_construct
1058 (unsigned char type, const CharT *name, size_type num, bool try2find,
1059 bool dothrow, ipcdetail::in_place_interface &table,
1060 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::true_ is_intrusive)
1063 std::size_t namelen = std::char_traits<CharT>::length(name);
1065 block_header_t block_info ( size_type(table.size*num)
1066 , size_type(table.alignment)
1071 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
1072 typedef typename index_type::iterator index_it;
1073 typedef std::pair<index_it, bool> index_ib;
1075 //-------------------------------
1076 scoped_lock<rmutex> guard(m_header);
1077 //-------------------------------
1078 //Insert the node. This can throw.
1079 //First, we want to know if the key is already present before
1080 //we allocate any memory, and if the key is not present, we
1081 //want to allocate all memory in a single buffer that will
1082 //contain the name and the user buffer.
1084 //Since equal_range(key) + insert(hint, value) approach is
1085 //quite inefficient in container implementations
1086 //(they re-test if the position is correct), I've chosen
1087 //to insert the node, do an ugly un-const cast and modify
1088 //the key (which is a smart pointer) to an equivalent one
1089 index_ib insert_ret;
1091 typename index_type::insert_commit_data commit_data;
1092 typedef typename index_type::value_type intrusive_value_type;
1095 ipcdetail::intrusive_compare_key<CharT> key(name, namelen);
1096 insert_ret = index.insert_check(key, commit_data);
1106 index_it it = insert_ret.first;
1108 //If found and this is find or construct, return data
1110 if(!insert_ret.second){
1112 return it->get_block_header()->value();
1115 throw interprocess_exception(already_exists_error);
1122 //Allocates buffer for name + data, this can throw (it hurts)
1125 //Check if there is enough memory
1127 buffer_ptr = this->allocate
1128 (block_info.template total_size_with_header<intrusive_value_type>());
1131 buffer_ptr = this->allocate
1132 (block_info.template total_size_with_header<intrusive_value_type>(), nothrow<>::get());
1137 //Now construct the intrusive hook plus the header
1138 intrusive_value_type * intrusive_hdr = ::new(buffer_ptr, boost_container_new_t()) intrusive_value_type();
1139 block_header_t * hdr = ::new(intrusive_hdr->get_block_header(), boost_container_new_t())block_header_t(block_info);
1140 void *ptr = 0; //avoid gcc warning
1143 //Copy name to memory segment and insert data
1144 CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
1145 std::char_traits<CharT>::copy(name_ptr, name, namelen+1);
1148 //Now commit the insertion using previous context data
1149 it = index.insert_commit(*intrusive_hdr, commit_data);
1159 //Avoid constructions if constructor is trivial
1160 //Build scoped ptr to avoid leaks with constructor exception
1161 ipcdetail::mem_algo_deallocator<segment_manager_base_type> mem
1162 (buffer_ptr, *static_cast<segment_manager_base_type*>(this));
1164 //Initialize the node value_eraser to erase inserted node
1165 //if something goes wrong. This will be executed *before*
1166 //the memory allocation as the intrusive value is built in that
1168 value_eraser<index_type> v_eraser(index, it);
1170 //Construct array, this can throw
1171 ipcdetail::array_construct(ptr, num, table);
1173 //Release rollbacks since construction was successful
1179 //!Generic named new function for
1181 template<class CharT>
1182 void * priv_generic_named_construct
1183 (unsigned char type, const CharT *name, size_type num, bool try2find, bool dothrow,
1184 ipcdetail::in_place_interface &table,
1185 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::false_ is_intrusive)
1188 std::size_t namelen = std::char_traits<CharT>::length(name);
1190 block_header_t block_info ( size_type(table.size*num)
1191 , size_type(table.alignment)
1196 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
1197 typedef typename index_type::key_type key_type;
1198 typedef typename index_type::mapped_type mapped_type;
1199 typedef typename index_type::value_type value_type;
1200 typedef typename index_type::iterator index_it;
1201 typedef std::pair<index_it, bool> index_ib;
1203 //-------------------------------
1204 scoped_lock<rmutex> guard(m_header);
1205 //-------------------------------
1206 //Insert the node. This can throw.
1207 //First, we want to know if the key is already present before
1208 //we allocate any memory, and if the key is not present, we
1209 //want to allocate all memory in a single buffer that will
1210 //contain the name and the user buffer.
1212 //Since equal_range(key) + insert(hint, value) approach is
1213 //quite inefficient in container implementations
1214 //(they re-test if the position is correct), I've chosen
1215 //to insert the node, do an ugly un-const cast and modify
1216 //the key (which is a smart pointer) to an equivalent one
1217 index_ib insert_ret;
1219 insert_ret = index.insert(value_type(key_type (name, namelen), mapped_type(0)));
1229 index_it it = insert_ret.first;
1231 //If found and this is find or construct, return data
1233 if(!insert_ret.second){
1235 block_header_t *hdr = static_cast<block_header_t*>
1236 (ipcdetail::to_raw_pointer(it->second.m_ptr));
1237 return hdr->value();
1241 //Initialize the node value_eraser to erase inserted node
1242 //if something goes wrong
1243 value_eraser<index_type> v_eraser(index, it);
1245 //Allocates buffer for name + data, this can throw (it hurts)
1247 block_header_t * hdr;
1249 //Allocate and construct the headers
1250 if(is_node_index_t::value){
1251 size_type total_size = block_info.template total_size_with_header<index_it>();
1253 buffer_ptr = this->allocate(total_size);
1256 buffer_ptr = this->allocate(total_size, nothrow<>::get());
1260 index_it *idr = ::new(buffer_ptr, boost_container_new_t()) index_it(it);
1261 hdr = block_header_t::template from_first_header<index_it>(idr);
1265 buffer_ptr = this->allocate(block_info.total_size());
1268 buffer_ptr = this->allocate(block_info.total_size(), nothrow<>::get());
1272 hdr = static_cast<block_header_t*>(buffer_ptr);
1275 hdr = ::new(hdr, boost_container_new_t())block_header_t(block_info);
1276 void *ptr = 0; //avoid gcc warning
1279 //Copy name to memory segment and insert data
1280 CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
1281 std::char_traits<CharT>::copy(name_ptr, name, namelen+1);
1283 //Do the ugly cast, please mama, forgive me!
1284 //This new key points to an identical string, so it must have the
1285 //same position than the overwritten key according to the predicate
1286 const_cast<key_type &>(it->first).name(name_ptr);
1287 it->second.m_ptr = hdr;
1289 //Build scoped ptr to avoid leaks with constructor exception
1290 ipcdetail::mem_algo_deallocator<segment_manager_base_type> mem
1291 (buffer_ptr, *static_cast<segment_manager_base_type*>(this));
1293 //Construct array, this can throw
1294 ipcdetail::array_construct(ptr, num, table);
1296 //All constructors successful, we don't want to release memory
1299 //Release node v_eraser since construction was successful
1305 //!Returns the this pointer
1306 segment_manager *get_this_pointer()
1309 typedef typename MemoryAlgorithm::mutex_family::recursive_mutex_type rmutex;
1311 scoped_lock<rmutex> priv_get_lock(bool use_lock)
1313 scoped_lock<rmutex> local(m_header, defer_lock);
1317 return scoped_lock<rmutex>(boost::move(local));
1320 //!This struct includes needed data and derives from
1321 //!rmutex to allow EBO when using null interprocess_mutex
1325 named_index_t m_named_index;
1326 unique_index_t m_unique_index;
1328 header_t(segment_manager_base_t *segment_mngr_base)
1329 : m_named_index (segment_mngr_base)
1330 , m_unique_index(segment_mngr_base)
1334 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
1338 }} //namespace boost { namespace interprocess
1340 #include <boost/interprocess/detail/config_end.hpp>
1342 #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP