11// SPDX-License-Identifier: BSD-3-Clause
2- /* Copyright 2018-2020 , Intel Corporation */
2+ /* Copyright 2018-2021 , Intel Corporation */
33
44/* *
55 * @file
@@ -644,7 +644,7 @@ vector<T>::assign(size_type count, const_reference value)
644644 add_data_to_tx (0 , size_old);
645645
646646 std::fill_n (
647- & _data[ 0 ] ,
647+ _data. get () ,
648648 (std::min)(count,
649649 static_cast <size_type>(size_old)),
650650 value);
@@ -1578,7 +1578,7 @@ vector<T>::insert(const_iterator pos, size_type count, const value_type &value)
15781578 single_element_iterator<value_type>(&value, count));
15791579 });
15801580
1581- return iterator (& _data[ static_cast <difference_type>(idx)] );
1581+ return iterator (_data. get () + static_cast <difference_type>(idx));
15821582}
15831583
15841584/* *
@@ -1821,7 +1821,7 @@ typename vector<T>::iterator
18211821vector<T>::erase(const_iterator first, const_iterator last)
18221822{
18231823 size_type idx = static_cast <size_type>(
1824- std::distance (const_iterator (& _data[ 0 ] ), first));
1824+ std::distance (const_iterator (_data. get () ), first));
18251825 size_type count = static_cast <size_type>(std::distance (first, last));
18261826
18271827 if (count == 0 )
@@ -2293,10 +2293,11 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
22932293 auto count = static_cast <size_type>(std::distance (first, last));
22942294
22952295 if (_capacity >= size () + count) {
2296- pointer dest =
2297- &_data[static_cast <difference_type>(size () + count)];
2298- pointer begin = &_data[static_cast <difference_type>(idx)];
2299- pointer end = &_data[static_cast <difference_type>(size ())];
2296+ pointer dest = _data.get () +
2297+ static_cast <difference_type>(size () + count);
2298+ pointer begin = _data.get () + static_cast <difference_type>(idx);
2299+ pointer end =
2300+ _data.get () + static_cast <difference_type>(size ());
23002301
23012302 add_data_to_tx (idx, size () - idx + count);
23022303
@@ -2314,9 +2315,11 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23142315
23152316 auto old_data = _data;
23162317 auto old_size = _size;
2317- pointer old_begin = &_data[0 ];
2318- pointer old_mid = &_data[static_cast <difference_type>(idx)];
2319- pointer old_end = &_data[static_cast <difference_type>(size ())];
2318+ pointer old_begin = _data.get ();
2319+ pointer old_mid =
2320+ _data.get () + static_cast <difference_type>(idx);
2321+ pointer old_end =
2322+ _data.get () + static_cast <difference_type>(size ());
23202323
23212324 _data = nullptr ;
23222325 _size = _capacity = 0 ;
@@ -2349,7 +2352,8 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23492352 * Private helper function. Must be called during transaction. Allocates new
23502353 * memory for capacity_new number of elements and copies or moves old elements
23512354 * to new memory area. If the current size is greater than capacity_new, the
2352- * container is reduced to its first capacity_new elements.
2355+ * container is reduced to its first capacity_new elements. If was never
2356+ * allocated behaves as an alloc call.
23532357 *
23542358 * param[in] capacity_new new capacity.
23552359 *
@@ -2368,6 +2372,13 @@ vector<T>::realloc(size_type capacity_new)
23682372{
23692373 assert (pmemobj_tx_stage () == TX_STAGE_WORK);
23702374
2375+ /*
2376+ * If _data == nullptr this object has never allocated any memory
2377+ * so we need to behave as alloc instead.
2378+ */
2379+ if (_data == nullptr )
2380+ return alloc (capacity_new);
2381+
23712382 /*
23722383 * XXX: future optimization: we don't have to snapshot data
23732384 * which we will not overwrite
@@ -2376,7 +2387,7 @@ vector<T>::realloc(size_type capacity_new)
23762387
23772388 auto old_data = _data;
23782389 auto old_size = _size;
2379- pointer old_begin = & _data[ 0 ] ;
2390+ pointer old_begin = _data. get () ;
23802391 pointer old_end = capacity_new < _size
23812392 ? &_data[static_cast <difference_type>(capacity_new)]
23822393 : &_data[static_cast <difference_type>(size ())];
0 commit comments