From 69192ae1f4f1bf0874c0b27de2eb9f1f9278235b Mon Sep 17 00:00:00 2001 From: Danial Khazaei Date: Mon, 8 Jul 2019 19:13:27 +0430 Subject: [PATCH] [port]: re-write List container to suppress [-Walloc-size-larger-than=]. - Changes are ported from OpenFOAM-dev project. --- src/foam/containers/Lists/List/List.C | 114 ++++++------------------- src/foam/containers/Lists/List/List.H | 23 ++++- src/foam/containers/Lists/List/ListI.H | 75 ++++++++++++++++ 3 files changed, 121 insertions(+), 91 deletions(-) diff --git a/src/foam/containers/Lists/List/List.C b/src/foam/containers/Lists/List/List.C index 5c1d5a256..fd5efb256 100644 --- a/src/foam/containers/Lists/List/List.C +++ b/src/foam/containers/Lists/List/List.C @@ -50,15 +50,12 @@ Foam::List::List(const label s) { if (this->size_ < 0) { - FatalErrorIn("List::List(const label size)") + FatalErrorInFunction << "bad size " << this->size_ << abort(FatalError); } - if (this->size_) - { - this->v_ = new T[this->size_]; - } + alloc(); } @@ -70,15 +67,15 @@ Foam::List::List(const label s, const T& a) { if (this->size_ < 0) { - FatalErrorIn("List::List(const label size, const T&)") + FatalErrorInFunction << "bad size " << this->size_ << abort(FatalError); } + alloc(); + if (this->size_) { - this->v_ = new T[this->size_]; - List_ACCESS(T, (*this), vp); List_FOR_ALL((*this), i) List_ELEM((*this), vp, i) = a; @@ -95,15 +92,15 @@ Foam::List::List(const List& a) { if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); -# ifdef USEMEMCPY + #ifdef USEMEMCPY if (contiguous()) { memcpy(this->v_, a.v_, this->byteSize()); } else -# endif + #endif { List_ACCESS(T, (*this), vp); List_CONST_ACCESS(T, a, ap); @@ -125,11 +122,11 @@ Foam::List::List(const Xfer< List >& lst) // Construct as copy or re-use as specified. template -Foam::List::List(List& a, bool reUse) +Foam::List::List(List& a, bool reuse) : UList(nullptr, a.size_) { - if (reUse) + if (reuse) { this->v_ = a.v_; a.v_ = 0; @@ -137,15 +134,15 @@ Foam::List::List(List& a, bool reUse) } else if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); -# ifdef USEMEMCPY + #ifdef USEMEMCPY if (contiguous()) { memcpy(this->v_, a.v_, this->byteSize()); } else -# endif + #endif { List_ACCESS(T, (*this), vp); List_CONST_ACCESS(T, a, ap); @@ -167,11 +164,11 @@ Foam::List::List(const UList& a, const unallocLabelList& map) { // Note:cannot use List_ELEM since third argument has to be index. - this->v_ = new T[this->size_]; + alloc(); forAll(*this, i) { - this->v_[i] = a[map[i]]; + this->operator[](i) = a[map[i]]; } } } @@ -181,32 +178,9 @@ Foam::List::List(const UList& a, const unallocLabelList& map) template template Foam::List::List(InputIterator first, InputIterator last) -{ - label s = 0; - for - ( - InputIterator iter = first; - iter != last; - ++iter - ) - { - s++; - } - - setSize(s); - - s = 0; - - for - ( - InputIterator iter = first; - iter != last; - ++iter - ) - { - this->operator[](s++) = iter(); - } -} +: + List(first, last, std::distance(first, last)) +{} // Construct as copy of FixedList @@ -216,15 +190,7 @@ Foam::List::List(const FixedList& lst) : UList(nullptr, Size) { - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } + allocCopyList(lst); } @@ -234,15 +200,7 @@ Foam::List::List(const PtrList& lst) : UList(nullptr, lst.size()) { - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } + allocCopyList(lst); } @@ -254,7 +212,7 @@ Foam::List::List(const SLList& lst) { if (this->size_) { - this->v_ = new T[this->size_]; + alloc(); label i = 0; for @@ -276,15 +234,7 @@ Foam::List::List(const IndirectList& lst) : UList(nullptr, lst.size()) { - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } + allocCopyList(lst); } @@ -294,15 +244,7 @@ Foam::List::List(const UIndirectList& lst) : UList(nullptr, lst.size()) { - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } + allocCopyList(lst); } @@ -312,15 +254,7 @@ Foam::List::List(const BiIndirectList& lst) : UList(nullptr, lst.size()) { - if (this->size_) - { - this->v_ = new T[this->size_]; - - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } - } + allocCopyList(lst); } diff --git a/src/foam/containers/Lists/List/List.H b/src/foam/containers/Lists/List/List.H index ec667b2a8..2a37e5c35 100644 --- a/src/foam/containers/Lists/List/List.H +++ b/src/foam/containers/Lists/List/List.H @@ -80,6 +80,27 @@ class List public UList { + // Private member functions + + //- Allocate list storage + inline void alloc(); + + //- Reallocate list storage to the given size + inline void reAlloc(const label s); + + //- Copy list of given type + template + inline void copyList(const List2&); + + //- Allocate storage and copy list of given type + template + inline void allocCopyList(const List2&); + + //- Construct given start and end iterators and number of elements + template + inline List(InputIterator first, InputIterator last, const label s); + + protected: //- Override size to be inconsistent with allocated storage. @@ -118,7 +139,7 @@ public: List(const Xfer< List >&); //- Construct as copy or re-use as specified. - List(List&, bool reUse); + List(List&, bool reuse); //- Construct as subset. List(const UList&, const unallocLabelList& mapAddressing); diff --git a/src/foam/containers/Lists/List/ListI.H b/src/foam/containers/Lists/List/ListI.H index 74c501891..05bacc9d0 100644 --- a/src/foam/containers/Lists/List/ListI.H +++ b/src/foam/containers/Lists/List/ListI.H @@ -23,6 +23,81 @@ License \*---------------------------------------------------------------------------*/ +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template +inline void Foam::List::alloc() +{ + if (this->size_ > 0) + { + this->v_ = new T[this->size_]; + } +} + + +template +inline void Foam::List::reAlloc(const label s) +{ + if (this->size_ != s) + { + clear(); + this->size_ = s; + alloc(); + } +} + + +template +template +inline void Foam::List::copyList(const List2& lst) +{ + if (this->size_) + { + forAll(*this, i) + { + this->operator[](i) = lst[i]; + } + } +} + + +template +template +inline void Foam::List::allocCopyList(const List2& lst) +{ + if (this->size_) + { + alloc(); + copyList(lst); + } +} + + +template +template +inline Foam::List::List +( + InputIterator first, + InputIterator last, + const label s +) +: + UList(nullptr, s) +{ + if (this->size_) + { + alloc(); + + InputIterator iter = first; + forAll(*this, i) + { + this->operator[](i) = *iter; + ++iter; + } + } +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template