Skip to content

Commit

Permalink
Refactor quasi-duplicated priv_generic_named_destroy and priv_generic…
Browse files Browse the repository at this point in the history
…_named_construct implementations.
  • Loading branch information
igaztanaga committed Sep 17, 2024
1 parent b53ffe5 commit 0665fd3
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 295 deletions.
65 changes: 15 additions & 50 deletions include/boost/interprocess/detail/segment_manager_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,6 @@ struct block_header
return get_rounded_size(size_type(sizeof(block_header)), size_type(m_value_alignment));
}

template<class CharType>
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<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) < 0);
}

template<class CharType>
bool equal_comp(const block_header &b) const
{
return m_num_char == b.m_num_char &&
std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) == 0;
}

template<class T>
static block_header *block_header_from_value(T *value)
{
Expand Down Expand Up @@ -186,6 +171,10 @@ struct block_header
return hdr;
}

template<class Header>
static const block_header *from_first_header(const Header *header)
{ return from_first_header(const_cast<Header*>(header)); }

template<class Header>
static Header *to_first_header(block_header *bheader)
{
Expand All @@ -207,29 +196,17 @@ struct intrusive_compare_key
: mp_str(str), m_len(len)
{}

const CharT * mp_str;
std::size_t m_len;
};
const CharT *str() const
{ return mp_str; }

template<class IndexType, bool IsIntrusive>
struct compare_key_impl
{
typedef typename IndexType::compare_key_type type;
};
std::size_t len() const
{ return m_len; }

template<class IndexType>
struct compare_key_impl<IndexType, false>
{
typedef typename IndexType::key_type type;
const CharT * mp_str;
std::size_t m_len;
};


template<class IndexType>
struct compare_key
: compare_key_impl<IndexType, is_intrusive_index<IndexType>::value>
{};


//!This struct indicates an anonymous object creation
//!allocation
template<instance_type type>
Expand Down Expand Up @@ -270,26 +247,14 @@ struct intrusive_value_type_impl

intrusive_value_type_impl(){}

enum { BlockHdrAlignment = ::boost::container::dtl::alignment_of<block_header_t>::value };
block_header_t *get_block_header()
{ return block_header_t::from_first_header(this); }

block_header_t *get_block_header() const
{
return const_cast<block_header_t*>
(move_detail::force_ptr<const block_header_t *>(reinterpret_cast<const char*>(this) +
get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment))));
}

bool operator <(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
{ return (this->get_block_header())->template less_comp<CharType>(*other.get_block_header()); }

bool operator ==(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
{ return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header()); }
const block_header_t *get_block_header() const
{ return block_header_t::from_first_header(this); }

static intrusive_value_type_impl *get_intrusive_value_type(block_header_t *hdr)
{
return move_detail::force_ptr<intrusive_value_type_impl*>(reinterpret_cast<char*>(hdr) -
get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment)));
}
{ return block_header_t::template to_first_header<intrusive_value_type_impl>(hdr); }

CharType *name() const
{ return get_block_header()->template name<CharType>(); }
Expand Down
40 changes: 33 additions & 7 deletions include/boost/interprocess/indexes/flat_map_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,55 @@ struct flat_map_index_aux
template <class MapConfig>
class flat_map_index
//Derive class from flat_map specialization
: public flat_map_index_aux<MapConfig>::index_t
: private flat_map_index_aux<MapConfig>::index_t
{
#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
typedef flat_map_index_aux<MapConfig> index_aux;
typedef typename index_aux::index_t base_type;
typedef typename index_aux::
segment_manager_base segment_manager_base;
typedef typename base_type::key_type key_type;
#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED

public:
using base_type::begin;
using base_type::end;
using base_type::size;
using base_type::erase;
using base_type::shrink_to_fit;
using base_type::reserve;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
typedef typename base_type::value_type value_type;
typedef typename MapConfig::compare_key_type compare_key_type;
typedef iterator insert_commit_data;
typedef iterator index_data_t;

//!Constructor. Takes a pointer to the segment manager. Can throw
flat_map_index(segment_manager_base *segment_mngr)
: base_type(typename index_aux::key_less(),
typename index_aux::allocator_type(segment_mngr))
{}

//!This reserves memory to optimize the insertion of n elements in the index
void reserve(typename segment_manager_base::size_type n)
{ base_type::reserve(n); }
std::pair<iterator, bool> insert_check
(const compare_key_type& key, insert_commit_data&)
{
std::pair<iterator, bool> r;
r.first = this->base_type::find(key_type(key.str(), key.len()));
r.second = r.first == this->base_type::end();
return r;
}

iterator insert_commit
(const compare_key_type &k, void *context, index_data_t&, insert_commit_data& )
{
//Now commit the insertion using previous context data
typedef typename base_type::mapped_type mapped_type;
return this->base_type::insert(value_type(key_type(k.str(), k.len()), mapped_type(context))).first;
}

//!This frees all unnecessary memory
void shrink_to_fit()
{ base_type::shrink_to_fit(); }
iterator find(const compare_key_type& k)
{ return this->base_type::find(key_type(k.str(), k.len())); }
};

}} //namespace boost { namespace interprocess
Expand Down
84 changes: 52 additions & 32 deletions include/boost/interprocess/indexes/iset_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,48 @@ struct iset_index_aux
< bi::void_pointer<void_pointer>
, bi::optimize_size<true>
>::type derivation_hook;

typedef typename MapConfig::char_type char_type;
typedef typename MapConfig::template
intrusive_value_type<derivation_hook>::type value_type;

typedef typename MapConfig::compare_key_type compare_key_type;

struct less_function
{
bool operator()(const compare_key_type&i, const value_type &b) const
{
std::size_t blen = b.name_length();
return (i.m_len < blen) ||
(i.m_len == blen &&
std::char_traits<char_type>::compare
(i.mp_str, b.name(), i.m_len) < 0);
}

bool operator()(const value_type &b, const compare_key_type&i) const
{
std::size_t blen = b.name_length();
return (blen < i.m_len) ||
(blen == i.m_len &&
std::char_traits<char_type>::compare
(b.name(), i.mp_str, i.m_len) < 0);
}

bool operator()(const value_type& a, const value_type& b) const
{
std::size_t alen = a.name_length();
std::size_t blen = b.name_length();
return (alen < blen) ||
(alen == blen &&
std::char_traits<char_type>::compare
(a.name(), b.name(), alen) < 0);
}
};

typedef std::less<value_type> value_compare;
typedef typename bi::make_set
< value_type
, bi::base_hook<derivation_hook>
, bi::compare<less_function>
>::type index_t;
};
#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
Expand All @@ -68,12 +103,13 @@ struct iset_index_aux
template <class MapConfig>
class iset_index
//Derive class from map specialization
: public iset_index_aux<MapConfig>::index_t
: private iset_index_aux<MapConfig>::index_t
{
#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
typedef iset_index_aux<MapConfig> index_aux;
typedef typename index_aux::index_t index_type;
typedef typename MapConfig::char_type char_type;
typedef typename index_aux::less_function less_function;
#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED

public:
Expand All @@ -82,35 +118,15 @@ class iset_index
typedef typename index_type::insert_commit_data insert_commit_data;
typedef typename index_type::value_type value_type;
typedef typename MapConfig::compare_key_type compare_key_type;

#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
private:

struct intrusive_key_value_less
{
bool operator()(const compare_key_type&i, const value_type &b) const
{
std::size_t blen = b.name_length();
return (i.m_len < blen) ||
(i.m_len == blen &&
std::char_traits<char_type>::compare
(i.mp_str, b.name(), i.m_len) < 0);
}

bool operator()(const value_type &b, const compare_key_type&i) const
{
std::size_t blen = b.name_length();
return (blen < i.m_len) ||
(blen == i.m_len &&
std::char_traits<char_type>::compare
(b.name(), i.mp_str, i.m_len) < 0);
}
};

#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
typedef value_type index_data_t;

public:

using index_type::begin;
using index_type::end;
using index_type::size;
using index_type::erase;

//!Constructor. Takes a pointer to the
//!segment manager. Can throw
iset_index(typename MapConfig::segment_manager_base *)
Expand All @@ -127,14 +143,18 @@ class iset_index
{ /*Does nothing, this intrusive index does not allocate memory;*/ }

iterator find(const compare_key_type&key)
{ return index_type::find(key, intrusive_key_value_less()); }
{ return index_type::find(key, less_function()); }

const_iterator find(const compare_key_type&key) const
{ return index_type::find(key, intrusive_key_value_less()); }
{ return index_type::find(key, less_function()); }

std::pair<iterator, bool>insert_check
(const compare_key_type&key, insert_commit_data &commit_data)
{ return index_type::insert_check(key, intrusive_key_value_less(), commit_data); }
(const compare_key_type &key, insert_commit_data &commit_data)
{ return index_type::insert_check(key, less_function(), commit_data); }

iterator insert_commit
(const compare_key_type &, void*, index_data_t&v, insert_commit_data& commit_data)
{ return index_type::insert_commit(v, commit_data); }
};

#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Expand Down
11 changes: 9 additions & 2 deletions include/boost/interprocess/indexes/iunordered_set_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ template <class MapConfig>
class iunordered_set_index
//Derive class from map specialization
: private iunordered_set_index_aux<MapConfig>::allocator_holder
, public iunordered_set_index_aux<MapConfig>::index_t
, private iunordered_set_index_aux<MapConfig>::index_t
{
#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
typedef iunordered_set_index_aux<MapConfig> index_aux;
Expand All @@ -172,6 +172,7 @@ class iunordered_set_index
typedef typename index_type::bucket_traits bucket_traits;
typedef typename index_type::size_type size_type;
typedef typename index_type::difference_type difference_type;
typedef value_type index_data_t;

#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
private:
Expand Down Expand Up @@ -260,6 +261,11 @@ class iunordered_set_index
#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED

public:
using index_type::begin;
using index_type::end;
using index_type::size;
using index_type::erase;

//!Constructor. Takes a pointer to the
//!segment manager. Can throw
iunordered_set_index(segment_manager_base *mngr)
Expand Down Expand Up @@ -346,7 +352,8 @@ class iunordered_set_index
(const compare_key_type&key, insert_commit_data &commit_data)
{ return index_type::insert_check(key, hash_function(), equal_function(), commit_data); }

iterator insert_commit(value_type &val, insert_commit_data &commit_data)
iterator insert_commit
(const compare_key_type &, void *, index_data_t &val, insert_commit_data& commit_data)
{
iterator it = index_type::insert_commit(val, commit_data);
size_type cur_size = this->size();
Expand Down
42 changes: 38 additions & 4 deletions include/boost/interprocess/indexes/map_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,29 @@ struct map_index_aux
template <class MapConfig>
class map_index
//Derive class from map specialization
: public ipcdetail::map_index_aux<MapConfig>::index_t
: private ipcdetail::map_index_aux<MapConfig>::index_t
{
#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
typedef ipcdetail::map_index_aux<MapConfig> index_aux;
typedef typename index_aux::index_t base_type;
typedef ipcdetail::map_index_aux<MapConfig> index_aux;
typedef typename index_aux::index_t base_type;
typedef typename MapConfig::
segment_manager_base segment_manager_base;
segment_manager_base segment_manager_base;
typedef typename base_type::key_type key_type;

#endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED

public:
using base_type::begin;
using base_type::end;
using base_type::size;
using base_type::erase;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
typedef typename base_type::value_type value_type;
typedef typename MapConfig::compare_key_type compare_key_type;
typedef iterator insert_commit_data;
typedef iterator index_data_t;

//!Constructor. Takes a pointer to the
//!segment manager. Can throw
map_index(segment_manager_base *segment_mngr)
Expand All @@ -87,6 +100,27 @@ class map_index
//!unused memory.
void shrink_to_fit()
{ base_type::get_stored_allocator().deallocate_free_blocks(); }

std::pair<iterator, bool> insert_check
(const compare_key_type& key, insert_commit_data& )
{
std::pair<iterator, bool> r;
r.first = this->base_type::find(key_type(key.str(), key.len()));
r.second = r.first == this->base_type::end();
return r;
}

iterator insert_commit
(const compare_key_type &k, void *context, index_data_t &index_data, insert_commit_data& )
{
//Now commit the insertion using previous context data
typedef typename base_type::mapped_type mapped_type;
iterator it = this->base_type::insert(value_type(key_type(k.str(), k.len()), mapped_type(context))).first;
return (index_data = it);
}

iterator find(const compare_key_type& k)
{ return this->base_type::find(key_type(k.str(), k.len())); }
};

#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
Expand Down
Loading

0 comments on commit 0665fd3

Please sign in to comment.