11// SPDX-License-Identifier: BSD-3-Clause
2- /* Copyright 2018-2020 , Intel Corporation */
2+ /* Copyright 2018-2021 , Intel Corporation */
33
44/* *
55 * @file
@@ -643,7 +643,7 @@ vector<T>::assign(size_type count, const_reference value)
643643 add_data_to_tx (0 , size_old);
644644
645645 std::fill_n (
646- & _data[ 0 ] ,
646+ _data. get () ,
647647 (std::min)(count,
648648 static_cast <size_type>(size_old)),
649649 value);
@@ -1577,7 +1577,7 @@ vector<T>::insert(const_iterator pos, size_type count, const value_type &value)
15771577 single_element_iterator<value_type>(&value, count));
15781578 });
15791579
1580- return iterator (& _data[ static_cast <difference_type>(idx)] );
1580+ return iterator (_data. get () + static_cast <difference_type>(idx));
15811581}
15821582
15831583/* *
@@ -1820,7 +1820,7 @@ typename vector<T>::iterator
18201820vector<T>::erase(const_iterator first, const_iterator last)
18211821{
18221822 size_type idx = static_cast <size_type>(
1823- std::distance (const_iterator (& _data[ 0 ] ), first));
1823+ std::distance (const_iterator (_data. get () ), first));
18241824 size_type count = static_cast <size_type>(std::distance (first, last));
18251825
18261826 if (count == 0 )
@@ -2294,10 +2294,11 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
22942294 auto count = static_cast <size_type>(std::distance (first, last));
22952295
22962296 if (_capacity >= size () + count) {
2297- pointer dest =
2298- &_data[static_cast <difference_type>(size () + count)];
2299- pointer begin = &_data[static_cast <difference_type>(idx)];
2300- pointer end = &_data[static_cast <difference_type>(size ())];
2297+ pointer dest = _data.get () +
2298+ static_cast <difference_type>(size () + count);
2299+ pointer begin = _data.get () + static_cast <difference_type>(idx);
2300+ pointer end =
2301+ _data.get () + static_cast <difference_type>(size ());
23012302
23022303 add_data_to_tx (idx, size () - idx + count);
23032304
@@ -2315,9 +2316,11 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23152316
23162317 auto old_data = _data;
23172318 auto old_size = _size;
2318- pointer old_begin = &_data[0 ];
2319- pointer old_mid = &_data[static_cast <difference_type>(idx)];
2320- pointer old_end = &_data[static_cast <difference_type>(size ())];
2319+ pointer old_begin = _data.get ();
2320+ pointer old_mid =
2321+ _data.get () + static_cast <difference_type>(idx);
2322+ pointer old_end =
2323+ _data.get () + static_cast <difference_type>(size ());
23212324
23222325 _data = nullptr ;
23232326 _size = _capacity = 0 ;
@@ -2350,7 +2353,8 @@ vector<T>::internal_insert(size_type idx, InputIt first, InputIt last)
23502353 * Private helper function. Must be called during transaction. Allocates new
23512354 * memory for capacity_new number of elements and copies or moves old elements
23522355 * to new memory area. If the current size is greater than capacity_new, the
2353- * container is reduced to its first capacity_new elements.
2356+ * container is reduced to its first capacity_new elements. If was never
2357+ * allocated behaves as an alloc call.
23542358 *
23552359 * param[in] capacity_new new capacity.
23562360 *
@@ -2369,6 +2373,13 @@ vector<T>::realloc(size_type capacity_new)
23692373{
23702374 assert (pmemobj_tx_stage () == TX_STAGE_WORK);
23712375
2376+ /*
2377+ * If _data == nullptr this object has never allocated any memory
2378+ * so we need to behave as alloc instead.
2379+ */
2380+ if (_data == nullptr )
2381+ return alloc (capacity_new);
2382+
23722383 /*
23732384 * XXX: future optimization: we don't have to snapshot data
23742385 * which we will not overwrite
@@ -2377,7 +2388,7 @@ vector<T>::realloc(size_type capacity_new)
23772388
23782389 auto old_data = _data;
23792390 auto old_size = _size;
2380- pointer old_begin = & _data[ 0 ] ;
2391+ pointer old_begin = _data. get () ;
23812392 pointer old_end = capacity_new < _size
23822393 ? &_data[static_cast <difference_type>(capacity_new)]
23832394 : &_data[static_cast <difference_type>(size ())];
0 commit comments