////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// #ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP #define BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP #if (defined _MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif #include #include #include #include #include #include #include #include #include #include #include #include //std::size_t #include //char_traits #include //std::nothrow #include //std::pair #include //BOOST_ASSERT #include //unary_function #ifndef BOOST_NO_EXCEPTIONS #include #endif //!\file //!Describes the object placed in a memory segment that provides //!named object allocation capabilities. namespace boost{ namespace interprocess{ template class segment_manager_base; //!An integer that describes the type of the //!instance constructed in memory enum instance_type { anonymous_type, named_type, unique_type, max_allocation_type }; namespace ipcdetail{ template class mem_algo_deallocator { void * m_ptr; MemoryAlgorithm & m_algo; public: mem_algo_deallocator(void *ptr, MemoryAlgorithm &algo) : m_ptr(ptr), m_algo(algo) {} void release() { m_ptr = 0; } ~mem_algo_deallocator() { if(m_ptr) m_algo.deallocate(m_ptr); } }; /// @cond template struct block_header { size_type m_value_bytes; unsigned short m_num_char; unsigned char m_value_alignment; unsigned char m_alloc_type_sizeof_char; block_header(size_type val_bytes ,size_type val_alignment ,unsigned char al_type ,std::size_t szof_char ,std::size_t num_char ) : m_value_bytes(val_bytes) , m_num_char((unsigned short)num_char) , m_value_alignment((unsigned char)val_alignment) , m_alloc_type_sizeof_char( (al_type << 5u) | ((unsigned char)szof_char & 0x1F) ) {}; template block_header &operator= (const T& ) { return *this; } size_type total_size() const { if(alloc_type() != anonymous_type){ return name_offset() + (m_num_char+1)*sizeof_char(); } else{ return this->value_offset() + m_value_bytes; } } size_type value_bytes() const { return m_value_bytes; } template size_type total_size_with_header() const { return get_rounded_size ( size_type(sizeof(Header)) , size_type(::boost::alignment_of >::value)) + total_size(); } unsigned char alloc_type() const { return (m_alloc_type_sizeof_char >> 5u)&(unsigned char)0x7; } unsigned char sizeof_char() const { return m_alloc_type_sizeof_char & (unsigned char)0x1F; } template CharType *name() const { return const_cast(reinterpret_cast (reinterpret_cast(this) + name_offset())); } unsigned short name_length() const { return m_num_char; } size_type name_offset() const { return this->value_offset() + get_rounded_size(size_type(m_value_bytes), size_type(sizeof_char())); } void *value() const { return const_cast((reinterpret_cast(this) + this->value_offset())); } size_type value_offset() const { return get_rounded_size(size_type(sizeof(block_header)), size_type(m_value_alignment)); } template bool less_comp(const block_header &b) const { return m_num_char < b.m_num_char || (m_num_char < b.m_num_char && std::char_traits::compare (name(), b.name(), m_num_char) < 0); } template bool equal_comp(const block_header &b) const { return m_num_char == b.m_num_char && std::char_traits::compare (name(), b.name(), m_num_char) == 0; } template static block_header *block_header_from_value(T *value) { return block_header_from_value(value, sizeof(T), ::boost::alignment_of::value); } static block_header *block_header_from_value(const void *value, std::size_t sz, std::size_t algn) { block_header * hdr = const_cast (reinterpret_cast(reinterpret_cast(value) - get_rounded_size(sizeof(block_header), algn))); (void)sz; //Some sanity checks BOOST_ASSERT(hdr->m_value_alignment == algn); BOOST_ASSERT(hdr->m_value_bytes % sz == 0); return hdr; } template static block_header *from_first_header(Header *header) { block_header * hdr = reinterpret_cast*>(reinterpret_cast(header) + get_rounded_size(size_type(sizeof(Header)), size_type(::boost::alignment_of >::value))); //Some sanity checks return hdr; } template static Header *to_first_header(block_header *bheader) { Header * hdr = reinterpret_cast(reinterpret_cast(bheader) - get_rounded_size(size_type(sizeof(Header)), size_type(::boost::alignment_of >::value))); //Some sanity checks return hdr; } }; inline void array_construct(void *mem, std::size_t num, in_place_interface &table) { //Try constructors std::size_t constructed = 0; BOOST_TRY{ table.construct_n(mem, num, constructed); } //If there is an exception call destructors and erase index node BOOST_CATCH(...){ std::size_t destroyed = 0; table.destroy_n(mem, constructed, destroyed); BOOST_RETHROW } BOOST_CATCH_END } template struct intrusive_compare_key { typedef CharT char_type; intrusive_compare_key(const CharT *str, std::size_t len) : mp_str(str), m_len(len) {} const CharT * mp_str; std::size_t m_len; }; //!This struct indicates an anonymous object creation //!allocation template class instance_t { instance_t(){} }; template struct char_if_void { typedef T type; }; template<> struct char_if_void { typedef char type; }; typedef instance_t anonymous_instance_t; typedef instance_t unique_instance_t; template struct intrusive_value_type_impl : public Hook { private: //Non-copyable intrusive_value_type_impl(const intrusive_value_type_impl &); intrusive_value_type_impl& operator=(const intrusive_value_type_impl &); public: typedef CharType char_type; typedef SizeType size_type; intrusive_value_type_impl(){} enum { BlockHdrAlignment = ::boost::alignment_of >::value }; block_header *get_block_header() const { return const_cast*> (reinterpret_cast *>(reinterpret_cast(this) + get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment)))); } bool operator <(const intrusive_value_type_impl & other) const { return (this->get_block_header())->template less_comp(*other.get_block_header()); } bool operator ==(const intrusive_value_type_impl & other) const { return (this->get_block_header())->template equal_comp(*other.get_block_header()); } static intrusive_value_type_impl *get_intrusive_value_type(block_header *hdr) { return reinterpret_cast(reinterpret_cast(hdr) - get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment))); } CharType *name() const { return get_block_header()->template name(); } unsigned short name_length() const { return get_block_header()->name_length(); } void *value() const { return get_block_header()->value(); } }; template class char_ptr_holder { public: char_ptr_holder(const CharType *name) : m_name(name) {} char_ptr_holder(const anonymous_instance_t *) : m_name(static_cast(0)) {} char_ptr_holder(const unique_instance_t *) : m_name(reinterpret_cast(-1)) {} operator const CharType *() { return m_name; } private: const CharType *m_name; }; //!The key of the the named allocation information index. Stores an offset pointer //!to a null terminated string and the length of the string to speed up sorting template struct index_key { typedef typename boost::intrusive:: pointer_traits::template rebind_pointer::type const_char_ptr_t; typedef CharT char_type; typedef typename boost::intrusive::pointer_traits::difference_type difference_type; typedef typename boost::make_unsigned::type size_type; private: //Offset pointer to the object's name const_char_ptr_t mp_str; //Length of the name buffer (null NOT included) size_type m_len; public: //!Constructor of the key index_key (const char_type *nm, size_type length) : mp_str(nm), m_len(length) {} //!Less than function for index ordering bool operator < (const index_key & right) const { return (m_len < right.m_len) || (m_len == right.m_len && std::char_traits::compare (to_raw_pointer(mp_str) ,to_raw_pointer(right.mp_str), m_len) < 0); } //!Equal to function for index ordering bool operator == (const index_key & right) const { return m_len == right.m_len && std::char_traits::compare (to_raw_pointer(mp_str), to_raw_pointer(right.mp_str), m_len) == 0; } void name(const CharT *nm) { mp_str = nm; } void name_length(size_type len) { m_len = len; } const CharT *name() const { return to_raw_pointer(mp_str); } size_type name_length() const { return m_len; } }; //!The index_data stores a pointer to a buffer and the element count needed //!to know how many destructors must be called when calling destroy template struct index_data { typedef VoidPointer void_pointer; void_pointer m_ptr; index_data(void *ptr) : m_ptr(ptr){} void *value() const { return static_cast(to_raw_pointer(m_ptr)); } }; template struct segment_manager_base_type { typedef segment_manager_base type; }; template struct index_config { typedef typename MemoryAlgorithm::void_pointer void_pointer; typedef CharT char_type; typedef index_key key_type; typedef index_data mapped_type; typedef typename segment_manager_base_type ::type segment_manager_base; template struct intrusive_value_type { typedef intrusive_value_type_impl type; }; typedef intrusive_compare_key intrusive_compare_key_type; }; template class segment_manager_iterator_value_adaptor { typedef typename Iterator::value_type iterator_val_t; typedef typename iterator_val_t::char_type char_type; public: segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val) : m_val(&val) {} const char_type *name() const { return m_val->name(); } unsigned short name_length() const { return m_val->name_length(); } const void *value() const { return m_val->value(); } const typename Iterator::value_type *m_val; }; template class segment_manager_iterator_value_adaptor { typedef typename Iterator::value_type iterator_val_t; typedef typename iterator_val_t::first_type first_type; typedef typename iterator_val_t::second_type second_type; typedef typename first_type::char_type char_type; typedef typename first_type::size_type size_type; public: segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val) : m_val(&val) {} const char_type *name() const { return m_val->first.name(); } size_type name_length() const { return m_val->first.name_length(); } const void *value() const { return reinterpret_cast*> (to_raw_pointer(m_val->second.m_ptr))->value(); } const typename Iterator::value_type *m_val; }; template struct segment_manager_iterator_transform : std::unary_function< typename Iterator::value_type , segment_manager_iterator_value_adaptor > { typedef segment_manager_iterator_value_adaptor result_type; result_type operator()(const typename Iterator::value_type &arg) const { return result_type(arg); } }; } //namespace ipcdetail { //These pointers are the ones the user will use to //indicate previous allocation types static const ipcdetail::anonymous_instance_t * anonymous_instance = 0; static const ipcdetail::unique_instance_t * unique_instance = 0; namespace ipcdetail_really_deep_namespace { //Otherwise, gcc issues a warning of previously defined //anonymous_instance and unique_instance struct dummy { dummy() { (void)anonymous_instance; (void)unique_instance; } }; } //detail_really_deep_namespace }} //namespace boost { namespace interprocess #include #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP