@@ -2852,15 +2852,17 @@ radix_tree<Key, Value, BytesView>::radix_tree_iterator<
28522852 IsConst>::radix_tree_iterator(leaf_ptr leaf_, tree_ptr tree)
28532853 : leaf_(leaf_), tree(tree)
28542854{
2855+ assert (tree);
28552856}
28562857
28572858template <typename Key, typename Value, typename BytesView>
28582859template <bool IsConst>
28592860template <bool C, typename Enable>
28602861radix_tree<Key, Value, BytesView>::radix_tree_iterator<
28612862 IsConst>::radix_tree_iterator(const radix_tree_iterator<false > &rhs)
2862- : leaf_(rhs.leaf_)
2863+ : leaf_(rhs.leaf_), tree(rhs.tree)
28632864{
2865+ assert (tree);
28642866}
28652867
28662868template <typename Key, typename Value, typename BytesView>
@@ -2871,6 +2873,8 @@ typename radix_tree<Key, Value,
28712873 BytesView>::radix_tree_iterator<IsConst>::operator *() const
28722874{
28732875 assert (leaf_);
2876+ assert (tree);
2877+
28742878 return *leaf_;
28752879}
28762880
@@ -2882,6 +2886,8 @@ typename radix_tree<Key, Value,
28822886 BytesView>::radix_tree_iterator<IsConst>::operator ->() const
28832887{
28842888 assert (leaf_);
2889+ assert (tree);
2890+
28852891 return leaf_;
28862892}
28872893
@@ -2903,6 +2909,9 @@ void
29032909radix_tree<Key, Value, BytesView>::radix_tree_iterator<IsConst>::assign_val(
29042910 basic_string_view<typename V::value_type, typename V::traits_type> rhs)
29052911{
2912+ assert (leaf_);
2913+ assert (tree);
2914+
29062915 auto pop = pool_base (pmemobj_pool_by_ptr (leaf_));
29072916
29082917 if (rhs.size () <= leaf_->value ().capacity () && !tree->mt .get (false )) {
@@ -2988,6 +2997,7 @@ bool
29882997radix_tree<Key, Value, BytesView>::radix_tree_iterator<IsConst>::try_increment()
29892998{
29902999 assert (leaf_);
3000+ assert (tree);
29913001
29923002 constexpr auto direction = radix_tree::node::direction::Forward;
29933003 auto parent_ptr = leaf_->parent .load_acquire ();
@@ -3035,6 +3045,7 @@ bool
30353045radix_tree<Key, Value, BytesView>::radix_tree_iterator<IsConst>::try_decrement()
30363046{
30373047 constexpr auto direction = radix_tree::node::direction::Reverse;
3048+ assert (tree);
30383049
30393050 while (true ) {
30403051 if (!leaf_) {
@@ -3198,18 +3209,16 @@ template <typename Key, typename Value, typename BytesView>
31983209Key &
31993210radix_tree<Key, Value, BytesView>::leaf::key()
32003211{
3201- return *reinterpret_cast <Key *>(this + 1 );
3212+ auto &const_key = const_cast <const leaf *>(this )->key ();
3213+ return *const_cast <Key *>(&const_key);
32023214}
32033215
32043216template <typename Key, typename Value, typename BytesView>
32053217Value &
32063218radix_tree<Key, Value, BytesView>::leaf::value()
32073219{
3208- auto key_dst = reinterpret_cast <char *>(this + 1 );
3209- auto val_dst = reinterpret_cast <Value *>(
3210- key_dst + total_sizeof<Key>::value (key ()));
3211-
3212- return *reinterpret_cast <Value *>(val_dst);
3220+ auto &const_value = const_cast <const leaf *>(this )->value ();
3221+ return *const_cast <Value *>(&const_value);
32133222}
32143223
32153224template <typename Key, typename Value, typename BytesView>
@@ -3224,10 +3233,12 @@ const Value &
32243233radix_tree<Key, Value, BytesView>::leaf::value() const
32253234{
32263235 auto key_dst = reinterpret_cast <const char *>(this + 1 );
3227- auto val_dst = reinterpret_cast <const Value *>(
3228- key_dst + total_sizeof<Key>::value (key ()));
3236+ auto key_size = total_sizeof<Key>::value (key ());
3237+ auto padding = detail::align_up (key_size, alignof (Value)) - key_size;
3238+ auto val_dst =
3239+ reinterpret_cast <const Value *>(key_dst + padding + key_size);
32293240
3230- return *reinterpret_cast < const Value *>( val_dst) ;
3241+ return *val_dst;
32313242}
32323243
32333244template <typename Key, typename Value, typename BytesView>
@@ -3348,12 +3359,16 @@ radix_tree<Key, Value, BytesView>::leaf::make(pointer_type parent,
33483359 auto key_size = total_sizeof<Key>::value (std::get<I1>(first_args)...);
33493360 auto val_size =
33503361 total_sizeof<Value>::value (std::get<I2>(second_args)...);
3362+ auto padding = detail::align_up (key_size, alignof (Value)) - key_size;
33513363 auto ptr = static_cast <persistent_ptr<leaf>>(
3352- a.allocate (sizeof (leaf) + key_size + val_size));
3364+ a.allocate (sizeof (leaf) + key_size + padding + val_size));
33533365
33543366 auto key_dst = reinterpret_cast <Key *>(ptr.get () + 1 );
33553367 auto val_dst = reinterpret_cast <Value *>(
3356- reinterpret_cast <char *>(key_dst) + key_size);
3368+ reinterpret_cast <char *>(key_dst) + padding + key_size);
3369+
3370+ assert (reinterpret_cast <uintptr_t >(key_dst) % alignof (Key) == 0 );
3371+ assert (reinterpret_cast <uintptr_t >(val_dst) % alignof (Value) == 0 );
33573372
33583373 new (ptr.get ()) leaf ();
33593374 new (key_dst) Key (std::forward<Args1>(std::get<I1>(first_args))...);
0 commit comments