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 11 #ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP 12 #define BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP 13 14 #ifndef BOOST_CONFIG_HPP 15 # include <boost/config.hpp> 16 #endif 17 # 18 #if defined(BOOST_HAS_PRAGMA_ONCE) 19 # pragma once 20 #endif 21 22 #include <boost/interprocess/detail/config_begin.hpp> 23 #include <boost/interprocess/detail/workaround.hpp> 24 25 #include <boost/core/no_exceptions_support.hpp> 26 #include <boost/interprocess/detail/type_traits.hpp> 27 28 #include <boost/interprocess/detail/transform_iterator.hpp> 29 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> 42 // container/detail 43 #include <boost/container/detail/minimal_char_traits_header.hpp> 44 #include <boost/container/detail/placement_new.hpp> 45 // std 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 50 #include <exception> 51 #endif 52 53 //!\file 54 //!Describes the object placed in a memory segment that provides 55 //!named object allocation capabilities for single-segment and 56 //!multi-segment allocations. 57 58 namespace boost{ 59 namespace interprocess{ 60 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. 65 //! 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 73 { 74 public: 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; 79 80 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 81 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; 86 87 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 88 89 //!This constant indicates the payload size 90 //!associated with each allocation of the memory algorithm 91 static const size_type PayloadPerAllocation = MemoryAlgorithm::PayloadPerAllocation; 92 93 //!Constructor of the segment_manager_base 94 //! 95 //!"size" is the size of the memory segment where 96 //!the basic segment manager is being constructed. 97 //! 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 102 //! 103 //!Can throw segment_manager_base(size_type sz,size_type reserved_bytes)104 segment_manager_base(size_type sz, size_type reserved_bytes) 105 : MemoryAlgorithm(sz, reserved_bytes) 106 { 107 BOOST_ASSERT((sizeof(segment_manager_base<MemoryAlgorithm>) == sizeof(MemoryAlgorithm))); 108 } 109 110 //!Returns the size of the memory 111 //!segment get_size() const112 size_type get_size() const 113 { return MemoryAlgorithm::get_size(); } 114 115 //!Returns the number of free bytes of the memory 116 //!segment get_free_memory() const117 size_type get_free_memory() const 118 { return MemoryAlgorithm::get_free_memory(); } 119 120 //!Obtains the minimum size needed by 121 //!the segment manager get_min_size(size_type size)122 static size_type get_min_size (size_type size) 123 { return MemoryAlgorithm::get_min_size(size); } 124 125 //!Allocates nbytes bytes. This function is only used in 126 //!single-segment management. Never throws allocate(size_type nbytes,const std::nothrow_t &)127 void * allocate (size_type nbytes, const std::nothrow_t &) 128 { return MemoryAlgorithm::allocate(nbytes); } 129 130 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 131 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. allocate_many(size_type elem_bytes,size_type n_elements,multiallocation_chain & chain)135 void allocate_many(size_type elem_bytes, size_type n_elements, multiallocation_chain &chain) 136 { 137 size_type prev_size = chain.size(); 138 MemoryAlgorithm::allocate_many(elem_bytes, n_elements, chain); 139 if(!elem_bytes || chain.size() == prev_size){ 140 throw bad_alloc(); 141 } 142 } 143 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. allocate_many(const size_type * element_lengths,size_type n_elements,size_type sizeof_element,multiallocation_chain & chain)146 void allocate_many(const size_type *element_lengths, size_type n_elements, size_type sizeof_element, multiallocation_chain &chain) 147 { 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){ 151 throw bad_alloc(); 152 } 153 } 154 155 //!Allocates n_elements of elem_bytes bytes. 156 //!Non-throwing version. chain.size() is not increased on failure. allocate_many(const std::nothrow_t &,size_type elem_bytes,size_type n_elements,multiallocation_chain & chain)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); } 159 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. allocate_many(const std::nothrow_t &,const size_type * elem_sizes,size_type n_elements,size_type sizeof_element,multiallocation_chain & chain)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); } 165 166 //!Deallocates all elements contained in chain. 167 //!Never throws. deallocate_many(multiallocation_chain & chain)168 void deallocate_many(multiallocation_chain &chain) 169 { MemoryAlgorithm::deallocate_many(chain); } 170 171 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 172 173 //!Allocates nbytes bytes. Throws boost::interprocess::bad_alloc 174 //!on failure allocate(size_type nbytes)175 void * allocate(size_type nbytes) 176 { 177 void * ret = MemoryAlgorithm::allocate(nbytes); 178 if(!ret) 179 throw bad_alloc(); 180 return ret; 181 } 182 183 //!Allocates nbytes bytes. This function is only used in 184 //!single-segment management. Never throws allocate_aligned(size_type nbytes,size_type alignment,const std::nothrow_t &)185 void * allocate_aligned (size_type nbytes, size_type alignment, const std::nothrow_t &) 186 { return MemoryAlgorithm::allocate_aligned(nbytes, alignment); } 187 188 //!Allocates nbytes bytes. This function is only used in 189 //!single-segment management. Throws bad_alloc when fails allocate_aligned(size_type nbytes,size_type alignment)190 void * allocate_aligned(size_type nbytes, size_type alignment) 191 { 192 void * ret = MemoryAlgorithm::allocate_aligned(nbytes, alignment); 193 if(!ret) 194 throw bad_alloc(); 195 return ret; 196 } 197 198 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 199 200 template<class T> allocation_command(boost::interprocess::allocation_type command,size_type limit_size,size_type & prefer_in_recvd_out_size,T * & reuse)201 T *allocation_command (boost::interprocess::allocation_type command, size_type limit_size, 202 size_type &prefer_in_recvd_out_size, T *&reuse) 203 { 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) 207 throw bad_alloc(); 208 return ret; 209 } 210 raw_allocation_command(boost::interprocess::allocation_type command,size_type limit_objects,size_type & prefer_in_recvd_out_size,void * & reuse,size_type sizeof_object=1)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) 213 { 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) 218 throw bad_alloc(); 219 return ret; 220 } 221 222 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 223 224 //!Deallocates the bytes allocated with allocate/allocate_many() 225 //!pointed by addr deallocate(void * addr)226 void deallocate (void *addr) 227 { MemoryAlgorithm::deallocate(addr); } 228 229 //!Increases managed memory in extra_size bytes more. This only works 230 //!with single-segment management. grow(size_type extra_size)231 void grow(size_type extra_size) 232 { MemoryAlgorithm::grow(extra_size); } 233 234 //!Decreases managed memory to the minimum. This only works 235 //!with single-segment management. shrink_to_fit()236 void shrink_to_fit() 237 { MemoryAlgorithm::shrink_to_fit(); } 238 239 //!Returns the result of "all_memory_deallocated()" function 240 //!of the used memory algorithm all_memory_deallocated()241 bool all_memory_deallocated() 242 { return MemoryAlgorithm::all_memory_deallocated(); } 243 244 //!Returns the result of "check_sanity()" function 245 //!of the used memory algorithm check_sanity()246 bool check_sanity() 247 { return MemoryAlgorithm::check_sanity(); } 248 249 //!Writes to zero free memory (memory not yet allocated) 250 //!of the memory algorithm zero_free_memory()251 void zero_free_memory() 252 { MemoryAlgorithm::zero_free_memory(); } 253 254 //!Returns the size of the buffer previously allocated pointed by ptr size(const void * ptr) const255 size_type size(const void *ptr) const 256 { return MemoryAlgorithm::size(ptr); } 257 258 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 259 protected: prot_anonymous_construct(size_type num,bool dothrow,ipcdetail::in_place_interface & table)260 void * prot_anonymous_construct 261 (size_type num, bool dothrow, ipcdetail::in_place_interface &table) 262 { 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) 266 , anonymous_type 267 , 1 268 , 0); 269 270 //Allocate memory 271 void *ptr_struct = this->allocate(block_info.total_size(), nothrow<>::get()); 272 273 //Check if there is enough memory 274 if(!ptr_struct){ 275 if(dothrow){ 276 throw bad_alloc(); 277 } 278 else{ 279 return 0; 280 } 281 } 282 283 //Build scoped ptr to avoid leaks with constructor exception 284 ipcdetail::mem_algo_deallocator<MemoryAlgorithm> mem(ptr_struct, *this); 285 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 289 ptr = hdr->value(); 290 291 //Now call constructors 292 ipcdetail::array_construct(ptr, num, table); 293 294 //All constructors successful, we don't want erase memory 295 mem.release(); 296 return ptr; 297 } 298 299 //!Calls the destructor and makes an anonymous deallocate prot_anonymous_destroy(const void * object,ipcdetail::in_place_interface & table)300 void prot_anonymous_destroy(const void *object, ipcdetail::in_place_interface &table) 301 { 302 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); 306 307 //------------------------------- 308 //scoped_lock<rmutex> guard(m_header); 309 //------------------------------- 310 311 if(ctrl_data->alloc_type() != anonymous_type){ 312 //This is not an anonymous object, the pointer is wrong! 313 BOOST_ASSERT(0); 314 } 315 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); 321 } 322 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 323 }; 324 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. 329 //! 330 //!The first index contains the mappings for normal named objects using the 331 //!char type specified in the template parameter. 332 //! 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. 336 //! 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> 346 { 347 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 348 //Non-copyable 349 segment_manager(); 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 354 355 public: 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; 361 362 typedef segment_manager_base<MemoryAlgorithm> segment_manager_base_type; 363 364 static const size_type PayloadPerAllocation = segment_manager_base_t::PayloadPerAllocation; 365 366 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 367 private: 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; 374 375 public: 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; 382 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 387 388 typedef typename segment_manager_base_t::mutex_family mutex_family; 389 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; 394 395 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 396 397 //!Constructor proxy object definition helper class 398 template<class T> 399 struct construct_proxy 400 { 401 typedef ipcdetail::named_proxy<segment_manager, T, false> type; 402 }; 403 404 //!Constructor proxy object definition helper class 405 template<class T> 406 struct construct_iter_proxy 407 { 408 typedef ipcdetail::named_proxy<segment_manager, T, true> type; 409 }; 410 411 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 412 413 //!Constructor of the segment manager 414 //!"size" is the size of the memory segment where 415 //!the segment manager is being constructed. 416 //!Can throw segment_manager(size_type segment_size)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())) 420 { 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 } 428 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. 432 template <class T> find(char_ptr_holder_t name)433 std::pair<T*, size_type> find (char_ptr_holder_t name) 434 { return this->priv_find_impl<T>(name, true); } 435 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. 441 template <class T> find_no_lock(char_ptr_holder_t name)442 std::pair<T*, size_type> find_no_lock (char_ptr_holder_t name) 443 { return this->priv_find_impl<T>(name, false); } 444 445 //!Returns throwing "construct" proxy 446 //!object 447 template <class T> 448 typename construct_proxy<T>::type construct(char_ptr_holder_t name)449 construct(char_ptr_holder_t name) 450 { return typename construct_proxy<T>::type (this, name, false, true); } 451 452 //!Returns throwing "search or construct" proxy 453 //!object 454 template <class T> find_or_construct(char_ptr_holder_t name)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); } 457 458 //!Returns no throwing "construct" proxy 459 //!object 460 template <class T> 461 typename construct_proxy<T>::type construct(char_ptr_holder_t name,const std::nothrow_t &)462 construct(char_ptr_holder_t name, const std::nothrow_t &) 463 { return typename construct_proxy<T>::type (this, name, false, false); } 464 465 //!Returns no throwing "search or construct" 466 //!proxy object 467 template <class T> 468 typename construct_proxy<T>::type find_or_construct(char_ptr_holder_t name,const std::nothrow_t &)469 find_or_construct(char_ptr_holder_t name, const std::nothrow_t &) 470 { return typename construct_proxy<T>::type (this, name, true, false); } 471 472 //!Returns throwing "construct from iterators" proxy object 473 template <class T> 474 typename construct_iter_proxy<T>::type construct_it(char_ptr_holder_t name)475 construct_it(char_ptr_holder_t name) 476 { return typename construct_iter_proxy<T>::type (this, name, false, true); } 477 478 //!Returns throwing "search or construct from iterators" 479 //!proxy object 480 template <class T> 481 typename construct_iter_proxy<T>::type find_or_construct_it(char_ptr_holder_t name)482 find_or_construct_it(char_ptr_holder_t name) 483 { return typename construct_iter_proxy<T>::type (this, name, true, true); } 484 485 //!Returns no throwing "construct from iterators" 486 //!proxy object 487 template <class T> 488 typename construct_iter_proxy<T>::type construct_it(char_ptr_holder_t name,const std::nothrow_t &)489 construct_it(char_ptr_holder_t name, const std::nothrow_t &) 490 { return typename construct_iter_proxy<T>::type (this, name, false, false); } 491 492 //!Returns no throwing "search or construct from iterators" 493 //!proxy object 494 template <class T> 495 typename construct_iter_proxy<T>::type find_or_construct_it(char_ptr_holder_t name,const std::nothrow_t &)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); } 498 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> atomic_func(Func & f)503 void atomic_func(Func &f) 504 { scoped_lock<rmutex> guard(m_header); f(); } 505 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> try_atomic_func(Func & f)512 bool try_atomic_func(Func &f) 513 { 514 scoped_lock<rmutex> guard(m_header, try_to_lock); 515 if(guard){ 516 f(); 517 return true; 518 } 519 else{ 520 return false; 521 } 522 } 523 524 //!Destroys a previously created named/unique instance. 525 //!Returns false if the object was not present. 526 template <class T> destroy(char_ptr_holder_t name)527 bool destroy(char_ptr_holder_t name) 528 { 529 BOOST_ASSERT(!name.is_anonymous()); 530 ipcdetail::placement_destroy<T> dtor; 531 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()); 535 } 536 else{ 537 return this->priv_generic_named_destroy<CharType> 538 ( name.get(), m_header.m_named_index, dtor, is_intrusive_t()); 539 } 540 } 541 542 //!Destroys an anonymous, unique or named object 543 //!using its address 544 template <class T> destroy_ptr(const T * p)545 void destroy_ptr(const T *p) 546 { 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); 551 } 552 553 //!Returns the name of an object created with construct/find_or_construct 554 //!functions. Does not throw 555 template<class T> get_instance_name(const T * ptr)556 static const CharType *get_instance_name(const T *ptr) 557 { return priv_get_instance_name(block_header_t::block_header_from_value(ptr)); } 558 559 //!Returns the length of an object created with construct/find_or_construct 560 //!functions. Does not throw. 561 template<class T> get_instance_length(const T * ptr)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)); } 564 565 //!Returns is the the name of an object created with construct/find_or_construct 566 //!functions. Does not throw 567 template<class T> get_instance_type(const T * ptr)568 static instance_type get_instance_type(const T *ptr) 569 { return priv_get_instance_type(block_header_t::block_header_from_value(ptr)); } 570 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. reserve_named_objects(size_type num)574 void reserve_named_objects(size_type num) 575 { 576 //------------------------------- 577 scoped_lock<rmutex> guard(m_header); 578 //------------------------------- 579 m_header.m_named_index.reserve(num); 580 } 581 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. reserve_unique_objects(size_type num)585 void reserve_unique_objects(size_type num) 586 { 587 //------------------------------- 588 scoped_lock<rmutex> guard(m_header); 589 //------------------------------- 590 m_header.m_unique_index.reserve(num); 591 } 592 593 //!Calls shrink_to_fit in both named and unique object indexes 594 //!to try to free unused memory from those indexes. shrink_to_fit_indexes()595 void shrink_to_fit_indexes() 596 { 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(); 602 } 603 604 //!Returns the number of named objects stored in 605 //!the segment. get_num_named_objects()606 size_type get_num_named_objects() 607 { 608 //------------------------------- 609 scoped_lock<rmutex> guard(m_header); 610 //------------------------------- 611 return m_header.m_named_index.size(); 612 } 613 614 //!Returns the number of unique objects stored in 615 //!the segment. get_num_unique_objects()616 size_type get_num_unique_objects() 617 { 618 //------------------------------- 619 scoped_lock<rmutex> guard(m_header); 620 //------------------------------- 621 return m_header.m_unique_index.size(); 622 } 623 624 //!Obtains the minimum size needed by the 625 //!segment manager get_min_size()626 static size_type get_min_size() 627 { return segment_manager_base_t::get_min_size(priv_get_reserved_bytes()); } 628 629 //!Returns a constant iterator to the beginning of the information about 630 //!the named allocations performed in this segment manager named_begin() const631 const_named_iterator named_begin() const 632 { 633 return (make_transform_iterator) 634 (m_header.m_named_index.begin(), named_transform()); 635 } 636 637 //!Returns a constant iterator to the end of the information about 638 //!the named allocations performed in this segment manager named_end() const639 const_named_iterator named_end() const 640 { 641 return (make_transform_iterator) 642 (m_header.m_named_index.end(), named_transform()); 643 } 644 645 //!Returns a constant iterator to the beginning of the information about 646 //!the unique allocations performed in this segment manager unique_begin() const647 const_unique_iterator unique_begin() const 648 { 649 return (make_transform_iterator) 650 (m_header.m_unique_index.begin(), unique_transform()); 651 } 652 653 //!Returns a constant iterator to the end of the information about 654 //!the unique allocations performed in this segment manager unique_end() const655 const_unique_iterator unique_end() const 656 { 657 return (make_transform_iterator) 658 (m_header.m_unique_index.end(), unique_transform()); 659 } 660 661 //!This is the default allocator to allocate types T 662 //!from this managed segment 663 template<class T> 664 struct allocator 665 { 666 typedef boost::interprocess::allocator<T, segment_manager> type; 667 }; 668 669 //!Returns an instance of the default allocator for type T 670 //!initialized that allocates memory from this segment manager. 671 template<class T> 672 typename allocator<T>::type get_allocator()673 get_allocator() 674 { return typename allocator<T>::type(this); } 675 676 //!This is the default deleter to delete types T 677 //!from this managed segment. 678 template<class T> 679 struct deleter 680 { 681 typedef boost::interprocess::deleter<T, segment_manager> type; 682 }; 683 684 //!Returns an instance of the default deleter for type T 685 //!that will delete an object constructed in this segment manager. 686 template<class T> 687 typename deleter<T>::type get_deleter()688 get_deleter() 689 { return typename deleter<T>::type(this); } 690 691 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 692 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. 696 template<class T> generic_construct(const CharType * name,size_type num,bool try2find,bool dothrow,ipcdetail::in_place_interface & table)697 T *generic_construct(const CharType *name, 698 size_type num, 699 bool try2find, 700 bool dothrow, 701 ipcdetail::in_place_interface &table) 702 { 703 return static_cast<T*> 704 (priv_generic_construct(name, num, try2find, dothrow, table)); 705 } 706 707 private: 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. 711 template <class T> priv_find_impl(const CharType * name,bool lock)712 std::pair<T*, size_type> priv_find_impl (const CharType* name, bool lock) 713 { 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; 717 size_type sz; 718 void *ret; 719 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); 722 } 723 else{ 724 ret = priv_generic_find<CharType> (name, m_header.m_named_index, table, sz, is_intrusive_t(), lock); 725 } 726 return std::pair<T*, size_type>(static_cast<T*>(ret), sz); 727 } 728 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. 732 template <class T> priv_find_impl(const ipcdetail::unique_instance_t * name,bool lock)733 std::pair<T*, size_type> priv_find_impl (const ipcdetail::unique_instance_t* name, bool lock) 734 { 735 ipcdetail::placement_destroy<T> table; 736 size_type size; 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); 739 } 740 priv_generic_construct(const CharType * name,size_type num,bool try2find,bool dothrow,ipcdetail::in_place_interface & table)741 void *priv_generic_construct 742 (const CharType *name, size_type num, bool try2find, bool dothrow, ipcdetail::in_place_interface &table) 743 { 744 void *ret; 745 //Security overflow check 746 if(num > ((std::size_t)-1)/table.size){ 747 if(dothrow) 748 throw bad_alloc(); 749 else 750 return 0; 751 } 752 if(name == 0){ 753 ret = this->prot_anonymous_construct(num, dothrow, table); 754 } 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()); 758 } 759 else{ 760 ret = this->priv_generic_named_construct<CharType> 761 (named_type, name, num, try2find, dothrow, table, m_header.m_named_index, is_intrusive_t()); 762 } 763 return ret; 764 } 765 priv_destroy_ptr(const void * ptr,ipcdetail::in_place_interface & dtor)766 void priv_destroy_ptr(const void *ptr, ipcdetail::in_place_interface &dtor) 767 { 768 block_header_t *ctrl_data = block_header_t::block_header_from_value(ptr, dtor.size, dtor.alignment); 769 switch(ctrl_data->alloc_type()){ 770 case anonymous_type: 771 this->prot_anonymous_destroy(ptr, dtor); 772 break; 773 774 case named_type: 775 this->priv_generic_named_destroy<CharType> 776 (ctrl_data, m_header.m_named_index, dtor, is_node_index_t()); 777 break; 778 779 case unique_type: 780 this->priv_generic_named_destroy<char> 781 (ctrl_data, m_header.m_unique_index, dtor, is_node_index_t()); 782 break; 783 784 default: 785 //This type is unknown, bad pointer passed to this function! 786 BOOST_ASSERT(0); 787 break; 788 } 789 } 790 791 //!Returns the name of an object created with construct/find_or_construct 792 //!functions. Does not throw priv_get_instance_name(block_header_t * ctrl_data)793 static const CharType *priv_get_instance_name(block_header_t *ctrl_data) 794 { 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) ); 799 return 0; 800 } 801 CharType *name = static_cast<CharType*>(ctrl_data->template name<CharType>()); 802 803 //Sanity checks 804 BOOST_ASSERT(ctrl_data->sizeof_char() == sizeof(CharType)); 805 BOOST_ASSERT(ctrl_data->m_num_char == std::char_traits<CharType>::length(name)); 806 return name; 807 } 808 priv_get_instance_length(block_header_t * ctrl_data,size_type sizeofvalue)809 static size_type priv_get_instance_length(block_header_t *ctrl_data, size_type sizeofvalue) 810 { 811 //Get header 812 BOOST_ASSERT((ctrl_data->value_bytes() %sizeofvalue) == 0); 813 return ctrl_data->value_bytes()/sizeofvalue; 814 } 815 816 //!Returns is the the name of an object created with construct/find_or_construct 817 //!functions. Does not throw priv_get_instance_type(block_header_t * ctrl_data)818 static instance_type priv_get_instance_type(block_header_t *ctrl_data) 819 { 820 //Get header 821 BOOST_ASSERT((instance_type)ctrl_data->alloc_type() < max_allocation_type); 822 return (instance_type)ctrl_data->alloc_type(); 823 } 824 priv_get_reserved_bytes()825 static size_type priv_get_reserved_bytes() 826 { 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); 830 } 831 832 template <class CharT> priv_generic_find(const CharT * name,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table,size_type & length,ipcdetail::true_ is_intrusive,bool use_lock)833 void *priv_generic_find 834 (const CharT* name, 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) 838 { 839 (void)is_intrusive; 840 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type; 841 typedef typename index_type::iterator index_it; 842 843 //------------------------------- 844 scoped_lock<rmutex> guard(priv_get_lock(use_lock)); 845 //------------------------------- 846 //Find name in index 847 ipcdetail::intrusive_compare_key<CharT> key 848 (name, std::char_traits<CharT>::length(name)); 849 index_it it = index.find(key); 850 851 //Initialize return values 852 void *ret_ptr = 0; 853 length = 0; 854 855 //If found, assign values 856 if(it != index.end()){ 857 //Get header 858 block_header_t *ctrl_data = it->get_block_header(); 859 860 //Sanity check 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; 865 } 866 return ret_ptr; 867 } 868 869 template <class CharT> priv_generic_find(const CharT * name,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table,size_type & length,ipcdetail::false_ is_intrusive,bool use_lock)870 void *priv_generic_find 871 (const CharT* name, 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) 875 { 876 (void)is_intrusive; 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; 880 881 //------------------------------- 882 scoped_lock<rmutex> guard(priv_get_lock(use_lock)); 883 //------------------------------- 884 //Find name in index 885 index_it it = index.find(key_type(name, std::char_traits<CharT>::length(name))); 886 887 //Initialize return values 888 void *ret_ptr = 0; 889 length = 0; 890 891 //If found, assign values 892 if(it != index.end()){ 893 //Get header 894 block_header_t *ctrl_data = reinterpret_cast<block_header_t*> 895 (ipcdetail::to_raw_pointer(it->second.m_ptr)); 896 897 //Sanity check 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; 902 } 903 return ret_ptr; 904 } 905 906 template <class CharT> priv_generic_named_destroy(block_header_t * block_header,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table,ipcdetail::true_ is_node_index)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) 911 { 912 (void)is_node_index; 913 typedef typename IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> >::iterator index_it; 914 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); 917 } 918 919 template <class CharT> priv_generic_named_destroy(block_header_t * block_header,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table,ipcdetail::false_ is_node_index)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) 925 { 926 (void)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()); 929 } 930 931 template <class CharT> priv_generic_named_destroy(const CharT * name,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table,ipcdetail::true_ is_intrusive_index)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) 935 { 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; 940 941 //------------------------------- 942 scoped_lock<rmutex> guard(m_header); 943 //------------------------------- 944 //Find name in index 945 ipcdetail::intrusive_compare_key<CharT> key 946 (name, std::char_traits<CharT>::length(name)); 947 index_it it = index.find(key); 948 949 //If not found, return false 950 if(it == index.end()){ 951 //This name is not present in the index, wrong pointer or name! 952 //BOOST_ASSERT(0); 953 return false; 954 } 955 956 block_header_t *ctrl_data = it->get_block_header(); 957 intrusive_value_type *iv = intrusive_value_type::get_intrusive_value_type(ctrl_data); 958 void *memory = iv; 959 void *values = ctrl_data->value(); 960 std::size_t num = ctrl_data->m_value_bytes/table.size; 961 962 //Sanity check 963 BOOST_ASSERT((ctrl_data->m_value_bytes % table.size) == 0); 964 BOOST_ASSERT(sizeof(CharT) == ctrl_data->sizeof_char()); 965 966 //Erase node from index 967 index.erase(it); 968 969 //Destroy the headers 970 ctrl_data->~block_header_t(); 971 iv->~intrusive_value_type(); 972 973 //Call destructors and free memory 974 std::size_t destroyed; 975 table.destroy_n(values, num, destroyed); 976 this->deallocate(memory); 977 return true; 978 } 979 980 template <class CharT> priv_generic_named_destroy(const CharT * name,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table,ipcdetail::false_ is_intrusive_index)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) 985 { 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; 990 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))); 997 998 //If not found, return false 999 if(it == index.end()){ 1000 //This name is not present in the index, wrong pointer or name! 1001 //BOOST_ASSERT(0); 1002 return false; 1003 } 1004 return this->priv_generic_named_destroy_impl<CharT>(it, index, table); 1005 } 1006 1007 template <class CharT> priv_generic_named_destroy_impl(const typename IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>>::iterator & it,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::in_place_interface & table)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) 1012 { 1013 typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type; 1014 typedef typename index_type::iterator index_it; 1015 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()))); 1020 (void)stored_name; 1021 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(); 1026 1027 //Sanity check 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()); 1031 1032 //Erase node from index 1033 index.erase(it); 1034 1035 //Destroy the header 1036 ctrl_data->~block_header_t(); 1037 1038 void *memory; 1039 if(is_node_index_t::value){ 1040 index_it *ihdr = block_header_t::template 1041 to_first_header<index_it>(ctrl_data); 1042 ihdr->~index_it(); 1043 memory = ihdr; 1044 } 1045 else{ 1046 memory = ctrl_data; 1047 } 1048 1049 //Call destructors and free memory 1050 std::size_t destroyed; 1051 table.destroy_n(values, num, destroyed); 1052 this->deallocate(memory); 1053 return true; 1054 } 1055 1056 template<class CharT> priv_generic_named_construct(unsigned char type,const CharT * name,size_type num,bool try2find,bool dothrow,ipcdetail::in_place_interface & table,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::true_ is_intrusive)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) 1061 { 1062 (void)is_intrusive; 1063 std::size_t namelen = std::char_traits<CharT>::length(name); 1064 1065 block_header_t block_info ( size_type(table.size*num) 1066 , size_type(table.alignment) 1067 , type 1068 , sizeof(CharT) 1069 , namelen); 1070 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; 1074 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. 1083 // 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; 1090 1091 typename index_type::insert_commit_data commit_data; 1092 typedef typename index_type::value_type intrusive_value_type; 1093 1094 BOOST_TRY{ 1095 ipcdetail::intrusive_compare_key<CharT> key(name, namelen); 1096 insert_ret = index.insert_check(key, commit_data); 1097 } 1098 //Ignore exceptions 1099 BOOST_CATCH(...){ 1100 if(dothrow) 1101 BOOST_RETHROW 1102 return 0; 1103 } 1104 BOOST_CATCH_END 1105 1106 index_it it = insert_ret.first; 1107 1108 //If found and this is find or construct, return data 1109 //else return null 1110 if(!insert_ret.second){ 1111 if(try2find){ 1112 return it->get_block_header()->value(); 1113 } 1114 if(dothrow){ 1115 throw interprocess_exception(already_exists_error); 1116 } 1117 else{ 1118 return 0; 1119 } 1120 } 1121 1122 //Allocates buffer for name + data, this can throw (it hurts) 1123 void *buffer_ptr; 1124 1125 //Check if there is enough memory 1126 if(dothrow){ 1127 buffer_ptr = this->allocate 1128 (block_info.template total_size_with_header<intrusive_value_type>()); 1129 } 1130 else{ 1131 buffer_ptr = this->allocate 1132 (block_info.template total_size_with_header<intrusive_value_type>(), nothrow<>::get()); 1133 if(!buffer_ptr) 1134 return 0; 1135 } 1136 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 1141 ptr = hdr->value(); 1142 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); 1146 1147 BOOST_TRY{ 1148 //Now commit the insertion using previous context data 1149 it = index.insert_commit(*intrusive_hdr, commit_data); 1150 } 1151 //Ignore exceptions 1152 BOOST_CATCH(...){ 1153 if(dothrow) 1154 BOOST_RETHROW 1155 return 0; 1156 } 1157 BOOST_CATCH_END 1158 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)); 1163 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 1167 //memory 1168 value_eraser<index_type> v_eraser(index, it); 1169 1170 //Construct array, this can throw 1171 ipcdetail::array_construct(ptr, num, table); 1172 1173 //Release rollbacks since construction was successful 1174 v_eraser.release(); 1175 mem.release(); 1176 return ptr; 1177 } 1178 1179 //!Generic named new function for 1180 //!named functions 1181 template<class CharT> priv_generic_named_construct(unsigned char type,const CharT * name,size_type num,bool try2find,bool dothrow,ipcdetail::in_place_interface & table,IndexType<ipcdetail::index_config<CharT,MemoryAlgorithm>> & index,ipcdetail::false_ is_intrusive)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) 1186 { 1187 (void)is_intrusive; 1188 std::size_t namelen = std::char_traits<CharT>::length(name); 1189 1190 block_header_t block_info ( size_type(table.size*num) 1191 , size_type(table.alignment) 1192 , type 1193 , sizeof(CharT) 1194 , namelen); 1195 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; 1202 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. 1211 // 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; 1218 BOOST_TRY{ 1219 insert_ret = index.insert(value_type(key_type (name, namelen), mapped_type(0))); 1220 } 1221 //Ignore exceptions 1222 BOOST_CATCH(...){ 1223 if(dothrow) 1224 BOOST_RETHROW; 1225 return 0; 1226 } 1227 BOOST_CATCH_END 1228 1229 index_it it = insert_ret.first; 1230 1231 //If found and this is find or construct, return data 1232 //else return null 1233 if(!insert_ret.second){ 1234 if(try2find){ 1235 block_header_t *hdr = static_cast<block_header_t*> 1236 (ipcdetail::to_raw_pointer(it->second.m_ptr)); 1237 return hdr->value(); 1238 } 1239 return 0; 1240 } 1241 //Initialize the node value_eraser to erase inserted node 1242 //if something goes wrong 1243 value_eraser<index_type> v_eraser(index, it); 1244 1245 //Allocates buffer for name + data, this can throw (it hurts) 1246 void *buffer_ptr; 1247 block_header_t * hdr; 1248 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>(); 1252 if(dothrow){ 1253 buffer_ptr = this->allocate(total_size); 1254 } 1255 else{ 1256 buffer_ptr = this->allocate(total_size, nothrow<>::get()); 1257 if(!buffer_ptr) 1258 return 0; 1259 } 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); 1262 } 1263 else{ 1264 if(dothrow){ 1265 buffer_ptr = this->allocate(block_info.total_size()); 1266 } 1267 else{ 1268 buffer_ptr = this->allocate(block_info.total_size(), nothrow<>::get()); 1269 if(!buffer_ptr) 1270 return 0; 1271 } 1272 hdr = static_cast<block_header_t*>(buffer_ptr); 1273 } 1274 1275 hdr = ::new(hdr, boost_container_new_t())block_header_t(block_info); 1276 void *ptr = 0; //avoid gcc warning 1277 ptr = hdr->value(); 1278 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); 1282 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; 1288 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)); 1292 1293 //Construct array, this can throw 1294 ipcdetail::array_construct(ptr, num, table); 1295 1296 //All constructors successful, we don't want to release memory 1297 mem.release(); 1298 1299 //Release node v_eraser since construction was successful 1300 v_eraser.release(); 1301 return ptr; 1302 } 1303 1304 private: 1305 //!Returns the this pointer get_this_pointer()1306 segment_manager *get_this_pointer() 1307 { return this; } 1308 1309 typedef typename MemoryAlgorithm::mutex_family::recursive_mutex_type rmutex; 1310 priv_get_lock(bool use_lock)1311 scoped_lock<rmutex> priv_get_lock(bool use_lock) 1312 { 1313 scoped_lock<rmutex> local(m_header, defer_lock); 1314 if(use_lock){ 1315 local.lock(); 1316 } 1317 return scoped_lock<rmutex>(boost::move(local)); 1318 } 1319 1320 //!This struct includes needed data and derives from 1321 //!rmutex to allow EBO when using null interprocess_mutex 1322 struct header_t 1323 : public rmutex 1324 { 1325 named_index_t m_named_index; 1326 unique_index_t m_unique_index; 1327 header_tboost::interprocess::segment_manager::header_t1328 header_t(segment_manager_base_t *segment_mngr_base) 1329 : m_named_index (segment_mngr_base) 1330 , m_unique_index(segment_mngr_base) 1331 {} 1332 } m_header; 1333 1334 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 1335 }; 1336 1337 1338 }} //namespace boost { namespace interprocess 1339 1340 #include <boost/interprocess/detail/config_end.hpp> 1341 1342 #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_HPP 1343 1344