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);
427 const std::size_t void_ptr_alignment = boost::move_detail::alignment_of<void_pointer>::value; (void)void_ptr_alignment;
428 BOOST_ASSERT((0 == (std::size_t)this_addr % boost::move_detail::alignment_of<segment_manager>::value));
431 //!Tries to find a previous named/unique allocation. Returns the address
432 //!and the object count. On failure the first member of the
433 //!returned pair is 0.
435 std::pair<T*, size_type> find (char_ptr_holder_t name)
436 { return this->priv_find_impl<T>(name, true); }
438 //!Tries to find a previous named/unique allocation. Returns the address
439 //!and the object count. On failure the first member of the
440 //!returned pair is 0. This search is not mutex-protected!
441 //!Use it only inside atomic_func() calls, where the internal mutex
442 //!is guaranteed to be locked.
444 std::pair<T*, size_type> find_no_lock (char_ptr_holder_t name)
445 { return this->priv_find_impl<T>(name, false); }
447 //!Returns throwing "construct" proxy
450 typename construct_proxy<T>::type
451 construct(char_ptr_holder_t name)
452 { return typename construct_proxy<T>::type (this, name, false, true); }
454 //!Returns throwing "search or construct" proxy
457 typename construct_proxy<T>::type find_or_construct(char_ptr_holder_t name)
458 { return typename construct_proxy<T>::type (this, name, true, true); }
460 //!Returns no throwing "construct" proxy
463 typename construct_proxy<T>::type
464 construct(char_ptr_holder_t name, const std::nothrow_t &)
465 { return typename construct_proxy<T>::type (this, name, false, false); }
467 //!Returns no throwing "search or construct"
470 typename construct_proxy<T>::type
471 find_or_construct(char_ptr_holder_t name, const std::nothrow_t &)
472 { return typename construct_proxy<T>::type (this, name, true, false); }
474 //!Returns throwing "construct from iterators" proxy object
476 typename construct_iter_proxy<T>::type
477 construct_it(char_ptr_holder_t name)
478 { return typename construct_iter_proxy<T>::type (this, name, false, true); }
480 //!Returns throwing "search or construct from iterators"
483 typename construct_iter_proxy<T>::type
484 find_or_construct_it(char_ptr_holder_t name)
485 { return typename construct_iter_proxy<T>::type (this, name, true, true); }
487 //!Returns no throwing "construct from iterators"
490 typename construct_iter_proxy<T>::type
491 construct_it(char_ptr_holder_t name, const std::nothrow_t &)
492 { return typename construct_iter_proxy<T>::type (this, name, false, false); }
494 //!Returns no throwing "search or construct from iterators"
497 typename construct_iter_proxy<T>::type
498 find_or_construct_it(char_ptr_holder_t name, const std::nothrow_t &)
499 { return typename construct_iter_proxy<T>::type (this, name, true, false); }
501 //!Calls object function blocking recursive interprocess_mutex and guarantees that
502 //!no new named_alloc or destroy will be executed by any process while
503 //!executing the object function call
504 template <class Func>
505 void atomic_func(Func &f)
506 { scoped_lock<rmutex> guard(m_header); f(); }
508 //!Tries to calls a functor guaranteeing that no new construction, search or
509 //!destruction will be executed by any process while executing the object
510 //!function call. If the atomic function can't be immediatelly executed
511 //!because the internal mutex is already locked, returns false.
512 //!If the functor throws, this function throws.
513 template <class Func>
514 bool try_atomic_func(Func &f)
516 scoped_lock<rmutex> guard(m_header, try_to_lock);
526 //!Destroys a previously created named/unique instance.
527 //!Returns false if the object was not present.
529 bool destroy(char_ptr_holder_t name)
531 BOOST_ASSERT(!name.is_anonymous());
532 ipcdetail::placement_destroy<T> dtor;
534 if(name.is_unique()){
535 return this->priv_generic_named_destroy<char>
536 ( typeid(T).name(), m_header.m_unique_index , dtor, is_intrusive_t());
539 return this->priv_generic_named_destroy<CharType>
540 ( name.get(), m_header.m_named_index, dtor, is_intrusive_t());
544 //!Destroys an anonymous, unique or named object
547 void destroy_ptr(const T *p)
549 //If T is void transform it to char
550 typedef typename ipcdetail::char_if_void<T>::type data_t;
551 ipcdetail::placement_destroy<data_t> dtor;
552 priv_destroy_ptr(p, dtor);
555 //!Returns the name of an object created with construct/find_or_construct
556 //!functions. Does not throw
558 static const CharType *get_instance_name(const T *ptr)
559 { return priv_get_instance_name(block_header_t::block_header_from_value(ptr)); }
561 //!Returns the length of an object created with construct/find_or_construct
562 //!functions. Does not throw.
564 static size_type get_instance_length(const T *ptr)
565 { return priv_get_instance_length(block_header_t::block_header_from_value(ptr), sizeof(T)); }
567 //!Returns is the the name of an object created with construct/find_or_construct
568 //!functions. Does not throw
570 static instance_type get_instance_type(const T *ptr)
571 { return priv_get_instance_type(block_header_t::block_header_from_value(ptr)); }
573 //!Preallocates needed index resources to optimize the
574 //!creation of "num" named objects in the managed memory segment.
575 //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
576 void reserve_named_objects(size_type num)
578 //-------------------------------
579 scoped_lock<rmutex> guard(m_header);
580 //-------------------------------
581 m_header.m_named_index.reserve(num);
584 //!Preallocates needed index resources to optimize the
585 //!creation of "num" unique objects in the managed memory segment.
586 //!Can throw boost::interprocess::bad_alloc if there is no enough memory.
587 void reserve_unique_objects(size_type num)
589 //-------------------------------
590 scoped_lock<rmutex> guard(m_header);
591 //-------------------------------
592 m_header.m_unique_index.reserve(num);
595 //!Calls shrink_to_fit in both named and unique object indexes
596 //!to try to free unused memory from those indexes.
597 void shrink_to_fit_indexes()
599 //-------------------------------
600 scoped_lock<rmutex> guard(m_header);
601 //-------------------------------
602 m_header.m_named_index.shrink_to_fit();
603 m_header.m_unique_index.shrink_to_fit();
606 //!Returns the number of named objects stored in
608 size_type get_num_named_objects()
610 //-------------------------------
611 scoped_lock<rmutex> guard(m_header);
612 //-------------------------------
613 return m_header.m_named_index.size();
616 //!Returns the number of unique objects stored in
618 size_type get_num_unique_objects()
620 //-------------------------------
621 scoped_lock<rmutex> guard(m_header);
622 //-------------------------------
623 return m_header.m_unique_index.size();
626 //!Obtains the minimum size needed by the
628 static size_type get_min_size()
629 { return segment_manager_base_t::get_min_size(priv_get_reserved_bytes()); }
631 //!Returns a constant iterator to the beginning of the information about
632 //!the named allocations performed in this segment manager
633 const_named_iterator named_begin() const
635 return (make_transform_iterator)
636 (m_header.m_named_index.begin(), named_transform());
639 //!Returns a constant iterator to the end of the information about
640 //!the named allocations performed in this segment manager
641 const_named_iterator named_end() const
643 return (make_transform_iterator)
644 (m_header.m_named_index.end(), named_transform());
647 //!Returns a constant iterator to the beginning of the information about
648 //!the unique allocations performed in this segment manager
649 const_unique_iterator unique_begin() const
651 return (make_transform_iterator)
652 (m_header.m_unique_index.begin(), unique_transform());
655 //!Returns a constant iterator to the end of the information about
656 //!the unique allocations performed in this segment manager
657 const_unique_iterator unique_end() const
659 return (make_transform_iterator)
660 (m_header.m_unique_index.end(), unique_transform());
663 //!This is the default allocator to allocate types T
664 //!from this managed segment
668 typedef boost::interprocess::allocator<T, segment_manager> type;
671 //!Returns an instance of the default allocator for type T
672 //!initialized that allocates memory from this segment manager.
674 typename allocator<T>::type
676 { return typename allocator<T>::type(this); }
678 //!This is the default deleter to delete types T
679 //!from this managed segment.
683 typedef boost::interprocess::deleter<T, segment_manager> type;
686 //!Returns an instance of the default deleter for type T
687 //!that will delete an object constructed in this segment manager.
689 typename deleter<T>::type
691 { return typename deleter<T>::type(this); }
693 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
695 //!Generic named/anonymous new function. Offers all the possibilities,
696 //!such as throwing, search before creating, and the constructor is
697 //!encapsulated in an object function.
699 T *generic_construct(const CharType *name,
703 ipcdetail::in_place_interface &table)
705 return static_cast<T*>
706 (priv_generic_construct(name, num, try2find, dothrow, table));
710 //!Tries to find a previous named allocation. Returns the address
711 //!and the object count. On failure the first member of the
712 //!returned pair is 0.
714 std::pair<T*, size_type> priv_find_impl (const CharType* name, bool lock)
716 //The name can't be null, no anonymous object can be found by name
717 BOOST_ASSERT(name != 0);
718 ipcdetail::placement_destroy<T> table;
722 if(name == reinterpret_cast<const CharType*>(-1)){
723 ret = priv_generic_find<char> (typeid(T).name(), m_header.m_unique_index, table, sz, is_intrusive_t(), lock);
726 ret = priv_generic_find<CharType> (name, m_header.m_named_index, table, sz, is_intrusive_t(), lock);
728 return std::pair<T*, size_type>(static_cast<T*>(ret), sz);
731 //!Tries to find a previous unique allocation. Returns the address
732 //!and the object count. On failure the first member of the
733 //!returned pair is 0.
735 std::pair<T*, size_type> priv_find_impl (const ipcdetail::unique_instance_t* name, bool lock)
737 ipcdetail::placement_destroy<T> table;
739 void *ret = priv_generic_find<char>(name, m_header.m_unique_index, table, size, is_intrusive_t(), lock);
740 return std::pair<T*, size_type>(static_cast<T*>(ret), size);
743 void *priv_generic_construct
744 (const CharType *name, size_type num, bool try2find, bool dothrow, ipcdetail::in_place_interface &table)
747 //Security overflow check
748 if(num > ((std::size_t)-1)/table.size){
755 ret = this->prot_anonymous_construct(num, dothrow, table);
757 else if(name == reinterpret_cast<const CharType*>(-1)){
758 ret = this->priv_generic_named_construct<char>
759 (unique_type, table.type_name, num, try2find, dothrow, table, m_header.m_unique_index, is_intrusive_t());
762 ret = this->priv_generic_named_construct<CharType>
763 (named_type, name, num, try2find, dothrow, table, m_header.m_named_index, is_intrusive_t());
768 void priv_destroy_ptr(const void *ptr, ipcdetail::in_place_interface &dtor)
770 block_header_t *ctrl_data = block_header_t::block_header_from_value(ptr, dtor.size, dtor.alignment);
771 switch(ctrl_data->alloc_type()){
773 this->prot_anonymous_destroy(ptr, dtor);
777 this->priv_generic_named_destroy<CharType>
778 (ctrl_data, m_header.m_named_index, dtor, is_node_index_t());
782 this->priv_generic_named_destroy<char>
783 (ctrl_data, m_header.m_unique_index, dtor, is_node_index_t());
787 //This type is unknown, bad pointer passed to this function!
793 //!Returns the name of an object created with construct/find_or_construct
794 //!functions. Does not throw
795 static const CharType *priv_get_instance_name(block_header_t *ctrl_data)
797 boost::interprocess::allocation_type type = ctrl_data->alloc_type();
798 if(type == anonymous_type){
799 BOOST_ASSERT((type == anonymous_type && ctrl_data->m_num_char == 0) ||
800 (type == unique_type && ctrl_data->m_num_char != 0) );
803 CharType *name = static_cast<CharType*>(ctrl_data->template name<CharType>());
806 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharType));
807 BOOST_ASSERT(ctrl_data->m_num_char == std::char_traits<CharType>::length(name));
811 static size_type priv_get_instance_length(block_header_t *ctrl_data, size_type sizeofvalue)
814 BOOST_ASSERT((ctrl_data->value_bytes() %sizeofvalue) == 0);
815 return ctrl_data->value_bytes()/sizeofvalue;
818 //!Returns is the the name of an object created with construct/find_or_construct
819 //!functions. Does not throw
820 static instance_type priv_get_instance_type(block_header_t *ctrl_data)
823 BOOST_ASSERT((instance_type)ctrl_data->alloc_type() < max_allocation_type);
824 return (instance_type)ctrl_data->alloc_type();
827 static size_type priv_get_reserved_bytes()
829 //Get the number of bytes until the end of (*this)
830 //beginning in the end of the segment_manager_base_t base.
831 return sizeof(segment_manager) - sizeof(segment_manager_base_t);
834 template <class CharT>
835 void *priv_generic_find
837 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
838 ipcdetail::in_place_interface &table,
839 size_type &length, ipcdetail::true_ is_intrusive, bool use_lock)
842 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
843 typedef typename index_type::iterator index_it;
845 //-------------------------------
846 scoped_lock<rmutex> guard(priv_get_lock(use_lock));
847 //-------------------------------
849 ipcdetail::intrusive_compare_key<CharT> key
850 (name, std::char_traits<CharT>::length(name));
851 index_it it = index.find(key);
853 //Initialize return values
857 //If found, assign values
858 if(it != index.end()){
860 block_header_t *ctrl_data = it->get_block_header();
863 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
864 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharT));
865 ret_ptr = ctrl_data->value();
866 length = ctrl_data->m_value_bytes/table.size;
871 template <class CharT>
872 void *priv_generic_find
874 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
875 ipcdetail::in_place_interface &table,
876 size_type &length, ipcdetail::false_ is_intrusive, bool use_lock)
879 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
880 typedef typename index_type::key_type key_type;
881 typedef typename index_type::iterator index_it;
883 //-------------------------------
884 scoped_lock<rmutex> guard(priv_get_lock(use_lock));
885 //-------------------------------
887 index_it it = index.find(key_type(name, std::char_traits<CharT>::length(name)));
889 //Initialize return values
893 //If found, assign values
894 if(it != index.end()){
896 block_header_t *ctrl_data = reinterpret_cast<block_header_t*>
897 (ipcdetail::to_raw_pointer(it->second.m_ptr));
900 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
901 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharT));
902 ret_ptr = ctrl_data->value();
903 length = ctrl_data->m_value_bytes/table.size;
908 template <class CharT>
909 bool priv_generic_named_destroy
910 (block_header_t *block_header,
911 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
912 ipcdetail::in_place_interface &table, ipcdetail::true_ is_node_index)
915 typedef typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator index_it;
917 index_it *ihdr = block_header_t::template to_first_header<index_it>(block_header);
918 return this->priv_generic_named_destroy_impl<CharT>(*ihdr, index, table);
921 template <class CharT>
922 bool priv_generic_named_destroy
923 (block_header_t *block_header,
924 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
925 ipcdetail::in_place_interface &table,
926 ipcdetail::false_ is_node_index)
929 CharT *name = static_cast<CharT*>(block_header->template name<CharT>());
930 return this->priv_generic_named_destroy<CharT>(name, index, table, is_intrusive_t());
933 template <class CharT>
934 bool priv_generic_named_destroy(const CharT *name,
935 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
936 ipcdetail::in_place_interface &table, ipcdetail::true_ is_intrusive_index)
938 (void)is_intrusive_index;
939 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
940 typedef typename index_type::iterator index_it;
941 typedef typename index_type::value_type intrusive_value_type;
943 //-------------------------------
944 scoped_lock<rmutex> guard(m_header);
945 //-------------------------------
947 ipcdetail::intrusive_compare_key<CharT> key
948 (name, std::char_traits<CharT>::length(name));
949 index_it it = index.find(key);
951 //If not found, return false
952 if(it == index.end()){
953 //This name is not present in the index, wrong pointer or name!
958 block_header_t *ctrl_data = it->get_block_header();
959 intrusive_value_type *iv = intrusive_value_type::get_intrusive_value_type(ctrl_data);
961 void *values = ctrl_data->value();
962 std::size_t num = ctrl_data->m_value_bytes/table.size;
965 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
966 BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char());
968 //Erase node from index
971 //Destroy the headers
972 ctrl_data->~block_header_t();
973 iv->~intrusive_value_type();
975 //Call destructors and free memory
976 std::size_t destroyed;
977 table.destroy_n(values, num, destroyed);
978 this->deallocate(memory);
982 template <class CharT>
983 bool priv_generic_named_destroy(const CharT *name,
984 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
985 ipcdetail::in_place_interface &table,
986 ipcdetail::false_ is_intrusive_index)
988 (void)is_intrusive_index;
989 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
990 typedef typename index_type::iterator index_it;
991 typedef typename index_type::key_type key_type;
993 //-------------------------------
994 scoped_lock<rmutex> guard(m_header);
995 //-------------------------------
996 //Try to find the name in the index
997 index_it it = index.find(key_type (name,
998 std::char_traits<CharT>::length(name)));
1000 //If not found, return false
1001 if(it == index.end()){
1002 //This name is not present in the index, wrong pointer or name!
1006 return this->priv_generic_named_destroy_impl<CharT>(it, index, table);
1009 template <class CharT>
1010 bool priv_generic_named_destroy_impl
1011 (const typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator &it,
1012 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index,
1013 ipcdetail::in_place_interface &table)
1015 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
1016 typedef typename index_type::iterator index_it;
1018 //Get allocation parameters
1019 block_header_t *ctrl_data = reinterpret_cast<block_header_t*>
1020 (ipcdetail::to_raw_pointer(it->second.m_ptr));
1021 char *stored_name = static_cast<char*>(static_cast<void*>(const_cast<CharT*>(it->first.name())));
1024 //Check if the distance between the name pointer and the memory pointer
1025 //is correct (this can detect incorrect type in destruction)
1026 std::size_t num = ctrl_data->m_value_bytes/table.size;
1027 void *values = ctrl_data->value();
1030 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0);
1031 BOOST_ASSERT(static_cast<void*>(stored_name) == static_cast<void*>(ctrl_data->template name<CharT>()));
1032 BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char());
1034 //Erase node from index
1037 //Destroy the header
1038 ctrl_data->~block_header_t();
1041 if(is_node_index_t::value){
1042 index_it *ihdr = block_header_t::template
1043 to_first_header<index_it>(ctrl_data);
1051 //Call destructors and free memory
1052 std::size_t destroyed;
1053 table.destroy_n(values, num, destroyed);
1054 this->deallocate(memory);
1058 template<class CharT>
1059 void * priv_generic_named_construct
1060 (unsigned char type, const CharT *name, size_type num, bool try2find,
1061 bool dothrow, ipcdetail::in_place_interface &table,
1062 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::true_ is_intrusive)
1065 std::size_t namelen = std::char_traits<CharT>::length(name);
1067 block_header_t block_info ( size_type(table.size*num)
1068 , size_type(table.alignment)
1073 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
1074 typedef typename index_type::iterator index_it;
1075 typedef std::pair<index_it, bool> index_ib;
1077 //-------------------------------
1078 scoped_lock<rmutex> guard(m_header);
1079 //-------------------------------
1080 //Insert the node. This can throw.
1081 //First, we want to know if the key is already present before
1082 //we allocate any memory, and if the key is not present, we
1083 //want to allocate all memory in a single buffer that will
1084 //contain the name and the user buffer.
1086 //Since equal_range(key) + insert(hint, value) approach is
1087 //quite inefficient in container implementations
1088 //(they re-test if the position is correct), I've chosen
1089 //to insert the node, do an ugly un-const cast and modify
1090 //the key (which is a smart pointer) to an equivalent one
1091 index_ib insert_ret;
1093 typename index_type::insert_commit_data commit_data;
1094 typedef typename index_type::value_type intrusive_value_type;
1097 ipcdetail::intrusive_compare_key<CharT> key(name, namelen);
1098 insert_ret = index.insert_check(key, commit_data);
1108 index_it it = insert_ret.first;
1110 //If found and this is find or construct, return data
1112 if(!insert_ret.second){
1114 return it->get_block_header()->value();
1117 throw interprocess_exception(already_exists_error);
1124 //Allocates buffer for name + data, this can throw (it hurts)
1127 //Check if there is enough memory
1129 buffer_ptr = this->allocate
1130 (block_info.template total_size_with_header<intrusive_value_type>());
1133 buffer_ptr = this->allocate
1134 (block_info.template total_size_with_header<intrusive_value_type>(), nothrow<>::get());
1139 //Now construct the intrusive hook plus the header
1140 intrusive_value_type * intrusive_hdr = ::new(buffer_ptr, boost_container_new_t()) intrusive_value_type();
1141 block_header_t * hdr = ::new(intrusive_hdr->get_block_header(), boost_container_new_t())block_header_t(block_info);
1142 void *ptr = 0; //avoid gcc warning
1145 //Copy name to memory segment and insert data
1146 CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
1147 std::char_traits<CharT>::copy(name_ptr, name, namelen+1);
1150 //Now commit the insertion using previous context data
1151 it = index.insert_commit(*intrusive_hdr, commit_data);
1161 //Avoid constructions if constructor is trivial
1162 //Build scoped ptr to avoid leaks with constructor exception
1163 ipcdetail::mem_algo_deallocator<segment_manager_base_type> mem
1164 (buffer_ptr, *static_cast<segment_manager_base_type*>(this));
1166 //Initialize the node value_eraser to erase inserted node
1167 //if something goes wrong. This will be executed *before*
1168 //the memory allocation as the intrusive value is built in that
1170 value_eraser<index_type> v_eraser(index, it);
1172 //Construct array, this can throw
1173 ipcdetail::array_construct(ptr, num, table);
1175 //Release rollbacks since construction was successful
1181 //!Generic named new function for
1183 template<class CharT>
1184 void * priv_generic_named_construct
1185 (unsigned char type, const CharT *name, size_type num, bool try2find, bool dothrow,
1186 ipcdetail::in_place_interface &table,
1187 IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::false_ is_intrusive)
1190 std::size_t namelen = std::char_traits<CharT>::length(name);
1192 block_header_t block_info ( size_type(table.size*num)
1193 , size_type(table.alignment)
1198 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
1199 typedef typename index_type::key_type key_type;
1200 typedef typename index_type::mapped_type mapped_type;
1201 typedef typename index_type::value_type value_type;
1202 typedef typename index_type::iterator index_it;
1203 typedef std::pair<index_it, bool> index_ib;
1205 //-------------------------------
1206 scoped_lock<rmutex> guard(m_header);
1207 //-------------------------------
1208 //Insert the node. This can throw.
1209 //First, we want to know if the key is already present before
1210 //we allocate any memory, and if the key is not present, we
1211 //want to allocate all memory in a single buffer that will
1212 //contain the name and the user buffer.
1214 //Since equal_range(key) + insert(hint, value) approach is
1215 //quite inefficient in container implementations
1216 //(they re-test if the position is correct), I've chosen
1217 //to insert the node, do an ugly un-const cast and modify
1218 //the key (which is a smart pointer) to an equivalent one
1219 index_ib insert_ret;
1221 insert_ret = index.insert(value_type(key_type (name, namelen), mapped_type(0)));
1231 index_it it = insert_ret.first;
1233 //If found and this is find or construct, return data
1235 if(!insert_ret.second){
1237 block_header_t *hdr = static_cast<block_header_t*>
1238 (ipcdetail::to_raw_pointer(it->second.m_ptr));
1239 return hdr->value();
1243 //Initialize the node value_eraser to erase inserted node
1244 //if something goes wrong
1245 value_eraser<index_type> v_eraser(index, it);
1247 //Allocates buffer for name + data, this can throw (it hurts)
1249 block_header_t * hdr;
1251 //Allocate and construct the headers
1252 if(is_node_index_t::value){
1253 size_type total_size = block_info.template total_size_with_header<index_it>();
1255 buffer_ptr = this->allocate(total_size);
1258 buffer_ptr = this->allocate(total_size, nothrow<>::get());
1262 index_it *idr = ::new(buffer_ptr, boost_container_new_t()) index_it(it);
1263 hdr = block_header_t::template from_first_header<index_it>(idr);
1267 buffer_ptr = this->allocate(block_info.total_size());
1270 buffer_ptr = this->allocate(block_info.total_size(), nothrow<>::get());
1274 hdr = static_cast<block_header_t*>(buffer_ptr);
1277 hdr = ::new(hdr, boost_container_new_t())block_header_t(block_info);
1278 void *ptr = 0; //avoid gcc warning
1281 //Copy name to memory segment and insert data
1282 CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
1283 std::char_traits<CharT>::copy(name_ptr, name, namelen+1);
1285 //Do the ugly cast, please mama, forgive me!
1286 //This new key points to an identical string, so it must have the
1287 //same position than the overwritten key according to the predicate
1288 const_cast<key_type &>(it->first).name(name_ptr);
1289 it->second.m_ptr = hdr;
1291 //Build scoped ptr to avoid leaks with constructor exception
1292 ipcdetail::mem_algo_deallocator<segment_manager_base_type> mem
1293 (buffer_ptr, *static_cast<segment_manager_base_type*>(this));
1295 //Construct array, this can throw
1296 ipcdetail::array_construct(ptr, num, table);
1298 //All constructors successful, we don't want to release memory
1301 //Release node v_eraser since construction was successful
1307 //!Returns the this pointer
1308 segment_manager *get_this_pointer()
1311 typedef typename MemoryAlgorithm::mutex_family::recursive_mutex_type rmutex;
1313 scoped_lock<rmutex> priv_get_lock(bool use_lock)
1315 scoped_lock<rmutex> local(m_header, defer_lock);
1319 return scoped_lock<rmutex>(boost::move(local));
1322 //!This struct includes needed data and derives from
1323 //!rmutex to allow EBO when using null interprocess_mutex
1327 named_index_t m_named_index;
1328 unique_index_t m_unique_index;
1330 header_t(segment_manager_base_t *segment_mngr_base)
1331 : m_named_index (segment_mngr_base)
1332 , m_unique_index(segment_mngr_base)
1336 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
1340 }} //namespace boost { namespace interprocess
1342 #include <boost/interprocess/detail/config_end.hpp>
1344 #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP