diff --git a/applications/test/DynamicList/DynamicListTest.C b/applications/test/DynamicList/DynamicListTest.C index 6cdbb84890..1c882b6ff3 100644 --- a/applications/test/DynamicList/DynamicListTest.C +++ b/applications/test/DynamicList/DynamicListTest.C @@ -43,9 +43,14 @@ int main(int argc, char *argv[]) ldl[0](3) = 3; ldl[0](1) = 1; - ldl[0].setSize(5); // increase allocated size - ldl[1].setSize(10); // increase allocated size - ldl[1](2) = 2; + ldl[0].allocSize(5); // increase allocated size + ldl[1].allocSize(10); // increase allocated size + ldl[0].reserve(15); // should increase allocated size + ldl[1].reserve(5); // should not decrease allocated size + ldl[1](3) = 2; // allocates space and sets value + + // this works without a segfault, but doesn't change the list size + ldl[0][4] = 4; ldl[1] = 3; @@ -78,7 +83,7 @@ int main(int argc, char *argv[]) { dlA.append(i); } - dlA.setSize(10); + dlA.allocSize(10); Info<< "" << dlA << "" << nl << "sizes: " << " " << dlA.size() << "/" << dlA.allocSize() << endl; @@ -95,7 +100,6 @@ int main(int argc, char *argv[]) Info<< "" << dlB << "" << nl << "sizes: " << " " << dlB.size() << "/" << dlB.allocSize() << endl; - return 0; } diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 214c780620..4574127d3e 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -118,18 +118,30 @@ public: //- Size of the underlying storage. inline label allocSize() const; - // Edit - //- Alter the list size. - // When the new size is greater than the addressed list size, the + //- Alter the size of the underlying storage. + // The addressed size will be truncated if needed to fit, but will + // otherwise remain untouched. + inline void allocSize(const label); + + // CURRENT BEHAVIOUR + //- When the new size is greater than the addressed list size, the // allocated list sizes is adjusted and the // addressed size does not change. // Otherwise the addressed list size is just reduced and the // allocated size does not change. + // + // PROPOSED BEHAVIOUR + //- Alter the addressed list size. + // New space will be allocated if required. inline void setSize(const label); - //- Clear the list, i.e. set the size to zero. + //- Reserve allocation space for at least this size. + // Never shrinks the allocated size, use allocSize() for that. + inline void reserve(const label); + + //- Clear the addressed list, i.e. set the size to zero. // Allocated size does not change inline void clear(); diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 1a40bdd0ac..d67a9b5347 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -70,12 +70,61 @@ const } +template +inline void Foam::DynamicList::allocSize +( + const label s +) +{ + label nextFree = List::size(); + allocSize_ = s; + + // truncate addressed size too? + if (nextFree > allocSize_) + { + nextFree = allocSize_; + } + + // adjust allocated size, and addressed size if necessary + List::setSize(allocSize_); + List::size(nextFree); +} + + +template +inline void Foam::DynamicList::reserve +( + const label s +) +{ + if (s > allocSize_) + { + allocSize_ = max + ( + s, + label(SizeMult*allocSize_/SizeDiv + SizeInc) + ); + + // adjust allocated size, leave addressed size untouched + label nextFree = List::size(); + List::setSize(allocSize_); + List::size(nextFree); + } +} + + template inline void Foam::DynamicList::setSize ( const label s ) { +#if 1 + // CURRENT BEHAVIOUR: + // slightly ambiguous about what size the list will actually get + // cannot increase the size of the addressed list (for compatibility + // with List), without automatically adjusting the allocated space! + label nextFree = List::size(); if (s <= nextFree) { @@ -89,6 +138,22 @@ inline void Foam::DynamicList::setSize List::setSize(allocSize_); } List::size(nextFree); +#else + // allocate more space? + if (s > allocSize_) + { + allocSize_ = max + ( + s, + label(SizeMult*allocSize_/SizeDiv + SizeInc) + ); + + List::setSize(allocSize_); + } + + // adjust addressed size + List::size(s); +#endif } @@ -153,21 +218,10 @@ inline void Foam::DynamicList::append(const T& e) // Work on copy free index since gets overwritten by setSize label nextFree = List::size(); - nextFree++; + reserve(nextFree+1); + List::size(nextFree+1); - if (nextFree > allocSize_) - { - allocSize_ = max - ( - nextFree, - label(SizeMult*allocSize_/SizeDiv + SizeInc) - ); - List::setSize(allocSize_); - } - - List::size(nextFree); - - this->operator[](nextFree - 1) = e; + this->operator[](nextFree) = e; } @@ -201,19 +255,9 @@ inline T& Foam::DynamicList::operator() ) { label nextFree = List::size(); - nextFree = max(nextFree, i + 1); - if (nextFree > allocSize_) - { - allocSize_ = max - ( - nextFree, - label(SizeMult*allocSize_/SizeDiv + SizeInc) - ); - List::setSize(allocSize_); - } - + reserve(nextFree); List::size(nextFree); return this->operator[](i);