diff --git a/applications/test/List/Test-List.cxx b/applications/test/List/Test-List.cxx index 4aa42d356a..d8a102c608 100644 --- a/applications/test/List/Test-List.cxx +++ b/applications/test/List/Test-List.cxx @@ -106,7 +106,7 @@ void printMyString(const UList& lst) { MyStrings slist2(lst); - Info< ( longLabelList, - [](const label& val){ return val; } + [](label val){ return val; } ); printListOutputType("short") << nl; @@ -567,7 +557,7 @@ int main(int argc, char *argv[]) auto scalars = ListOps::create ( labels, - [](const label& val){ return scalar(1.5*val); } + [](label val){ return scalar(1.5*val); } ); Info<< "scalars: " << flatOutput(scalars) << endl; } @@ -576,7 +566,7 @@ int main(int argc, char *argv[]) auto vectors = ListOps::create ( labels, - [](const label& val){ return vector(1.2*val, -1.2*val, 0); } + [](label val){ return vector(1.2*val, -1.2*val, 0); } ); Info<< "vectors: " << flatOutput(vectors) << endl; } @@ -585,7 +575,7 @@ int main(int argc, char *argv[]) auto longs = ListOps::create ( labels, - [](const label& val){ return val; } + [](label val){ return val; } ); Info<< "longs: " << flatOutput(longs) << endl; } @@ -603,7 +593,7 @@ int main(int argc, char *argv[]) ( labelRange().cbegin(), labelRange(15).cend(), - [](const label& val){ return scalar(-1.125*val); } + [](label val){ return scalar(-1.125*val); } ); Info<< "scalars: " << flatOutput(scalars) << endl; } diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index bf8768d9bc..002610037f 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -203,7 +203,7 @@ public: //- Change the value for the list capacity directly (ADVANCED, UNSAFE) //- Does not perform any memory management or resizing. - void setCapacity_unsafe(const label len) noexcept { capacity_ = len; } + void setCapacity_unsafe(label len) noexcept { capacity_ = len; } //- Reserve allocation space for at least this size, allocating new //- space if required and \em retaining old content. @@ -251,11 +251,6 @@ public: //- Shrink the allocated space to the number of elements used. inline void shrink_to_fit(); - //- Shrink the internal bookkeeping of the allocated space to the - //- number of addressed elements without affecting allocation. - // \note when empty() it will delete any allocated memory. - inline void shrink_unsafe(); - // Edit diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 17e199242d..d452f52d7d 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -68,7 +68,9 @@ inline void Foam::DynamicList::doCapacity // Addressable length, possibly truncated by new capacity const label currLen = Foam::min(List::size(), newCapacity); + // Consistent allocated sizing List::setAddressableSize(capacity_); + if (nocopy) { List::resize_nocopy(newCapacity); @@ -95,6 +97,9 @@ inline void Foam::DynamicList::doReserve // Preserve addressed size const label currLen = List::size(); + // Consistent allocated sizing + List::setAddressableSize(capacity_); + // Increase capacity (eg, doubling) capacity_ = Foam::ListPolicy::reserve_size(len, capacity_); @@ -105,8 +110,10 @@ inline void Foam::DynamicList::doReserve } else { - List::resize(capacity_); + List::resize_copy(currLen, capacity_); } + + capacity_ = List::size(); List::setAddressableSize(currLen); } } @@ -271,7 +278,7 @@ inline Foam::DynamicList::DynamicList List(std::move(static_cast&>(list))), capacity_(list.capacity()) { - list.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept + list.setCapacity_unsafe(0); // All contents moved } @@ -285,7 +292,7 @@ inline Foam::DynamicList::DynamicList List(std::move(static_cast&>(list))), capacity_(list.capacity()) { - list.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept + list.setCapacity_unsafe(0); // All contents moved } @@ -361,8 +368,15 @@ inline void Foam::DynamicList::reserve_exact // Preserve addressed size const label currLen = List::size(); - capacity_ = len; - List::resize(capacity_); + // Consistent allocated sizing + List::setAddressableSize(capacity_); + + // if (!nocopy) + { + List::resize_copy(currLen, len); + } + + capacity_ = List::size(); List::setAddressableSize(currLen); } } @@ -449,18 +463,6 @@ inline void Foam::DynamicList::shrink_to_fit() } -template -inline void Foam::DynamicList::shrink_unsafe() -{ - if (List::empty()) - { - // Delete storage if empty - List::clear(); - } - capacity_ = List::size(); -} - - template inline void Foam::DynamicList::swap(List& list) diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 5c9d7d1c8f..1dcfc62ba4 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -236,13 +236,6 @@ public: // Otherwise the contents will be uninitialized. inline void resize_nocopy(const label len); - //- Change the addressed list size directly without affecting - //- any memory management (advanced usage). - // - // It is left to the caller to avoid \em unsafe lengthening beyond - // the allocated memory region. - inline void resize_unsafe(const label len) noexcept; - //- Alias for resize() void setSize(const label n) { this->resize(n); } diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index b8d292950b..e37a2ea271 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -178,13 +178,6 @@ inline void Foam::List::resize_nocopy(const label len) } -template -inline void Foam::List::resize_unsafe(const label len) noexcept -{ - UList::setAddressableSize(len); -} - - template inline T& Foam::List::newElmt(const label i) { diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H index f55ee7e022..d7f202e838 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H @@ -112,23 +112,26 @@ public: //- Change the value for the list capacity directly (ADVANCED, UNSAFE) //- Does not perform any memory management or resizing. - void setCapacity_unsafe(const label len) noexcept { capacity_ = len; } + void setCapacity_unsafe(label len) noexcept { capacity_ = len; } //- Reserve allocation space for at least this size. + // New entries are initialized to nullptr. inline void reserve(const label len); //- Reserve allocation space for at least this size. //- If allocation is required, uses the specified size //- without any other resizing logic. + // New entries are initialized to nullptr. inline void reserve_exact(const label len); //- Alter the addressed list size. - inline void resize(const label newLen); + // New entries are initialized to nullptr. + inline void resize(const label len); //- Set the addressed list to the given size, //- deleting all existing entries. //- Afterwards the list contains all \c nullptr entries. - inline void resize_null(const label newLen); + inline void resize_null(const label len); //- Clear the addressed list, i.e. set the size to zero. // Allocated size does not change @@ -140,11 +143,6 @@ public: //- Shrink the allocated space to the number of elements used. inline void shrink_to_fit(); - //- Shrink the internal bookkeeping of the allocated space to the - //- number of addressed elements without affecting allocation. - // \note when empty() it will delete any allocated memory. - inline void shrink_unsafe(); - //- Alias for shrink_to_fit() void shrink() { this->shrink_to_fit(); } diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index 79763cf398..03be2398f4 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H @@ -82,10 +82,7 @@ inline Foam::PtrDynList::PtrDynList PtrList(std::move(list)), capacity_(list.capacity()) { - // FUTURE: - // list.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept - - list.clearStorage(); // capacity=0 etc. + list.setCapacity_unsafe(0); // All contents moved } @@ -99,10 +96,7 @@ inline Foam::PtrDynList::PtrDynList PtrList(std::move(list)), capacity_(list.capacity()) { - // FUTURE: - // list.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept - - list.clearStorage(); // capacity=0 etc. + list.setCapacity_unsafe(0); // All contents moved } @@ -135,11 +129,17 @@ inline void Foam::PtrDynList::reserve(const label len) // Preserve addressed size const label currLen = PtrList::size(); + // Consistent allocated sizing + PtrList::setAddressableSize(capacity_); + // Increase capacity (eg, doubling) capacity_ = Foam::ListPolicy::reserve_size(len, capacity_); + // No PtrList::resize_copy(...) -> copying nullptr is cheap PtrList::resize(capacity_); + + capacity_ = PtrList::size(); PtrList::setAddressableSize(currLen); } } @@ -153,8 +153,13 @@ inline void Foam::PtrDynList::reserve_exact(const label len) // Preserve addressed size const label currLen = PtrList::size(); - capacity_ = len; - PtrList::resize(capacity_); + // Consistent allocated sizing + PtrList::setAddressableSize(capacity_); + + // No PtrList::resize_copy(...) -> copying nullptr is cheap + PtrList::resize(len); + + capacity_ = PtrList::size(); PtrList::setAddressableSize(currLen); } } @@ -164,16 +169,12 @@ template inline void Foam::PtrDynList::resize(const label newLen) { auto& ptrs = this->ptrs_; - const label oldLen = ptrs.size(); if (capacity_ < newLen) { - // Increase capacity (eg, doubling) - capacity_ = - Foam::ListPolicy::reserve_size(newLen, capacity_); - - PtrList::resize(capacity_); + // Extend list + this->reserve(newLen); } else if (newLen != oldLen) { @@ -191,13 +192,16 @@ inline void Foam::PtrDynList::resize(const label newLen) template -inline void Foam::PtrDynList::resize_null(const label newLen) +inline void Foam::PtrDynList::resize_null(const label len) { - if (capacity_ < newLen) + if (capacity_ < len) { + // Consistent allocated sizing + PtrList::setAddressableSize(capacity_); + // Increase capacity (eg, doubling) capacity_ = - Foam::ListPolicy::reserve_size(newLen, capacity_); + Foam::ListPolicy::reserve_size(len, capacity_); PtrList::resize_null(capacity_); } @@ -207,7 +211,7 @@ inline void Foam::PtrDynList::resize_null(const label newLen) } // Adjust addressed size - PtrList::setAddressableSize(newLen); + PtrList::setAddressableSize(len); } @@ -240,18 +244,6 @@ inline void Foam::PtrDynList::shrink_to_fit() } -template -inline void Foam::PtrDynList::shrink_unsafe() -{ - if (PtrList::empty()) - { - // Delete empty list - PtrList::clear(); - } - capacity_ = PtrList::size(); -} - - template inline Foam::label Foam::PtrDynList::squeezeNull() { diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H index 7451c5ffde..c161fc327c 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H @@ -217,7 +217,7 @@ public: //- Change the value for the list capacity directly (ADVANCED, UNSAFE) //- Does not perform any memory management or resizing. - void setCapacity_unsafe(const label len) noexcept { capacity_ = len; } + void setCapacity_unsafe(label len) noexcept { capacity_ = len; } //- Reserve allocation space for at least this size, allocating new //- space if required and \em retaining old content. @@ -265,11 +265,6 @@ public: //- Shrink the allocated space to the number of elements used. inline void shrink_to_fit(); - //- Shrink the internal bookkeeping of the allocated space to the - //- number of addressed elements without affecting allocation. - // \note when empty() it will delete any allocated memory. - inline void shrink_unsafe(); - // Edit diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H index 1b5b6fd69d..ec9237be50 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H @@ -66,7 +66,9 @@ inline void Foam::DynamicField::doCapacity // Addressable length, possibly truncated by new capacity const label currLen = Foam::min(List::size(), newCapacity); + // Consistent allocated sizing List::setAddressableSize(capacity_); + if (nocopy) { List::resize_nocopy(newCapacity); @@ -93,6 +95,9 @@ inline void Foam::DynamicField::doReserve // Preserve addressed size const label currLen = List::size(); + // Consistent allocated sizing + List::setAddressableSize(capacity_); + // Increase capacity (eg, doubling) capacity_ = Foam::ListPolicy::reserve_size(len, capacity_); @@ -103,8 +108,10 @@ inline void Foam::DynamicField::doReserve } else { - List::resize(capacity_); + List::resize_copy(currLen, capacity_); } + + capacity_ = List::size(); List::setAddressableSize(currLen); } } @@ -246,7 +253,7 @@ inline Foam::DynamicField::DynamicField Field(std::move(static_cast&>(content))), capacity_(content.capacity()) { - content.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept + content.setCapacity_unsafe(0); // All contents moved } @@ -259,7 +266,7 @@ inline Foam::DynamicField::DynamicField Field(std::move(static_cast&>(content))), capacity_(content.capacity()) { - content.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept + content.setCapacity_unsafe(0); // All contents moved } @@ -273,7 +280,7 @@ inline Foam::DynamicField::DynamicField Field(std::move(static_cast&>(content))), capacity_(content.capacity()) { - content.setCapacity_unsafe(0); // Same as shrink_unsafe() but noexcept + content.setCapacity_unsafe(0); // All contents moved } @@ -292,7 +299,7 @@ inline Foam::DynamicField::DynamicField { Field::transfer(static_cast&>(content)); capacity_ = content.capacity(); - content.setCapacity_unsafe(0); + content.setCapacity_unsafe(0); // All contents moved } else { @@ -317,7 +324,7 @@ inline Foam::DynamicField::DynamicField { Field::transfer(static_cast&>(content)); capacity_ = content.capacity(); - content.setCapacity_unsafe(0); + content.setCapacity_unsafe(0); // All contents moved } else { @@ -462,8 +469,15 @@ inline void Foam::DynamicField::reserve_exact // Preserve addressed size const label currLen = List::size(); - capacity_ = len; - List::resize(capacity_); + // Consistent allocated sizing + List::setAddressableSize(capacity_); + + // if (!nocopy) + { + List::resize_copy(currLen, len); + } + + capacity_ = List::size(); List::setAddressableSize(currLen); } } @@ -551,18 +565,6 @@ inline void Foam::DynamicField::shrink_to_fit() } -template -inline void Foam::DynamicField::shrink_unsafe() -{ - if (List::empty()) - { - // Delete storage if empty - List::clear(); - } - capacity_ = List::size(); -} - - template inline void Foam::DynamicField::swap(List& list)