[port]: re-write List container to suppress [-Walloc-size-larger-than=].

- Changes are ported from OpenFOAM-dev project.
This commit is contained in:
Danial Khazaei 2019-07-08 19:13:27 +04:30
parent 9e8966483f
commit 69192ae1f4
No known key found for this signature in database
GPG key ID: 8389A6DEEC8D5025
3 changed files with 121 additions and 91 deletions

View file

@ -50,15 +50,12 @@ Foam::List<T>::List(const label s)
{ {
if (this->size_ < 0) if (this->size_ < 0)
{ {
FatalErrorIn("List<T>::List(const label size)") FatalErrorInFunction
<< "bad size " << this->size_ << "bad size " << this->size_
<< abort(FatalError); << abort(FatalError);
} }
if (this->size_) alloc();
{
this->v_ = new T[this->size_];
}
} }
@ -70,15 +67,15 @@ Foam::List<T>::List(const label s, const T& a)
{ {
if (this->size_ < 0) if (this->size_ < 0)
{ {
FatalErrorIn("List<T>::List(const label size, const T&)") FatalErrorInFunction
<< "bad size " << this->size_ << "bad size " << this->size_
<< abort(FatalError); << abort(FatalError);
} }
alloc();
if (this->size_) if (this->size_)
{ {
this->v_ = new T[this->size_];
List_ACCESS(T, (*this), vp); List_ACCESS(T, (*this), vp);
List_FOR_ALL((*this), i) List_FOR_ALL((*this), i)
List_ELEM((*this), vp, i) = a; List_ELEM((*this), vp, i) = a;
@ -95,15 +92,15 @@ Foam::List<T>::List(const List<T>& a)
{ {
if (this->size_) if (this->size_)
{ {
this->v_ = new T[this->size_]; alloc();
# ifdef USEMEMCPY #ifdef USEMEMCPY
if (contiguous<T>()) if (contiguous<T>())
{ {
memcpy(this->v_, a.v_, this->byteSize()); memcpy(this->v_, a.v_, this->byteSize());
} }
else else
# endif #endif
{ {
List_ACCESS(T, (*this), vp); List_ACCESS(T, (*this), vp);
List_CONST_ACCESS(T, a, ap); List_CONST_ACCESS(T, a, ap);
@ -125,11 +122,11 @@ Foam::List<T>::List(const Xfer< List<T> >& lst)
// Construct as copy or re-use as specified. // Construct as copy or re-use as specified.
template<class T> template<class T>
Foam::List<T>::List(List<T>& a, bool reUse) Foam::List<T>::List(List<T>& a, bool reuse)
: :
UList<T>(nullptr, a.size_) UList<T>(nullptr, a.size_)
{ {
if (reUse) if (reuse)
{ {
this->v_ = a.v_; this->v_ = a.v_;
a.v_ = 0; a.v_ = 0;
@ -137,15 +134,15 @@ Foam::List<T>::List(List<T>& a, bool reUse)
} }
else if (this->size_) else if (this->size_)
{ {
this->v_ = new T[this->size_]; alloc();
# ifdef USEMEMCPY #ifdef USEMEMCPY
if (contiguous<T>()) if (contiguous<T>())
{ {
memcpy(this->v_, a.v_, this->byteSize()); memcpy(this->v_, a.v_, this->byteSize());
} }
else else
# endif #endif
{ {
List_ACCESS(T, (*this), vp); List_ACCESS(T, (*this), vp);
List_CONST_ACCESS(T, a, ap); List_CONST_ACCESS(T, a, ap);
@ -167,11 +164,11 @@ Foam::List<T>::List(const UList<T>& a, const unallocLabelList& map)
{ {
// Note:cannot use List_ELEM since third argument has to be index. // Note:cannot use List_ELEM since third argument has to be index.
this->v_ = new T[this->size_]; alloc();
forAll(*this, i) forAll(*this, i)
{ {
this->v_[i] = a[map[i]]; this->operator[](i) = a[map[i]];
} }
} }
} }
@ -181,32 +178,9 @@ Foam::List<T>::List(const UList<T>& a, const unallocLabelList& map)
template<class T> template<class T>
template<class InputIterator> template<class InputIterator>
Foam::List<T>::List(InputIterator first, InputIterator last) Foam::List<T>::List(InputIterator first, InputIterator last)
{ :
label s = 0; List<T>(first, last, std::distance(first, last))
for {}
(
InputIterator iter = first;
iter != last;
++iter
)
{
s++;
}
setSize(s);
s = 0;
for
(
InputIterator iter = first;
iter != last;
++iter
)
{
this->operator[](s++) = iter();
}
}
// Construct as copy of FixedList<T, Size> // Construct as copy of FixedList<T, Size>
@ -216,15 +190,7 @@ Foam::List<T>::List(const FixedList<T, Size>& lst)
: :
UList<T>(nullptr, Size) UList<T>(nullptr, Size)
{ {
if (this->size_) allocCopyList(lst);
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
} }
@ -234,15 +200,7 @@ Foam::List<T>::List(const PtrList<T>& lst)
: :
UList<T>(nullptr, lst.size()) UList<T>(nullptr, lst.size())
{ {
if (this->size_) allocCopyList(lst);
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
} }
@ -254,7 +212,7 @@ Foam::List<T>::List(const SLList<T>& lst)
{ {
if (this->size_) if (this->size_)
{ {
this->v_ = new T[this->size_]; alloc();
label i = 0; label i = 0;
for for
@ -276,15 +234,7 @@ Foam::List<T>::List(const IndirectList<T>& lst)
: :
UList<T>(nullptr, lst.size()) UList<T>(nullptr, lst.size())
{ {
if (this->size_) allocCopyList(lst);
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
} }
@ -294,15 +244,7 @@ Foam::List<T>::List(const UIndirectList<T>& lst)
: :
UList<T>(nullptr, lst.size()) UList<T>(nullptr, lst.size())
{ {
if (this->size_) allocCopyList(lst);
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
} }
@ -312,15 +254,7 @@ Foam::List<T>::List(const BiIndirectList<T>& lst)
: :
UList<T>(nullptr, lst.size()) UList<T>(nullptr, lst.size())
{ {
if (this->size_) allocCopyList(lst);
{
this->v_ = new T[this->size_];
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
} }

View file

@ -80,6 +80,27 @@ class List
public UList<T> public UList<T>
{ {
// 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<class List2>
inline void copyList(const List2&);
//- Allocate storage and copy list of given type
template<class List2>
inline void allocCopyList(const List2&);
//- Construct given start and end iterators and number of elements
template<class InputIterator>
inline List(InputIterator first, InputIterator last, const label s);
protected: protected:
//- Override size to be inconsistent with allocated storage. //- Override size to be inconsistent with allocated storage.
@ -118,7 +139,7 @@ public:
List(const Xfer< List<T> >&); List(const Xfer< List<T> >&);
//- Construct as copy or re-use as specified. //- Construct as copy or re-use as specified.
List(List<T>&, bool reUse); List(List<T>&, bool reuse);
//- Construct as subset. //- Construct as subset.
List(const UList<T>&, const unallocLabelList& mapAddressing); List(const UList<T>&, const unallocLabelList& mapAddressing);

View file

@ -23,6 +23,81 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class T>
inline void Foam::List<T>::alloc()
{
if (this->size_ > 0)
{
this->v_ = new T[this->size_];
}
}
template<class T>
inline void Foam::List<T>::reAlloc(const label s)
{
if (this->size_ != s)
{
clear();
this->size_ = s;
alloc();
}
}
template<class T>
template<class List2>
inline void Foam::List<T>::copyList(const List2& lst)
{
if (this->size_)
{
forAll(*this, i)
{
this->operator[](i) = lst[i];
}
}
}
template<class T>
template<class List2>
inline void Foam::List<T>::allocCopyList(const List2& lst)
{
if (this->size_)
{
alloc();
copyList(lst);
}
}
template<class T>
template<class InputIterator>
inline Foam::List<T>::List
(
InputIterator first,
InputIterator last,
const label s
)
:
UList<T>(nullptr, s)
{
if (this->size_)
{
alloc();
InputIterator iter = first;
forAll(*this, i)
{
this->operator[](i) = *iter;
++iter;
}
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T> template<class T>