Skip to content
Snippets Groups Projects
Commit a34f83c6 authored by Markus Blatt's avatar Markus Blatt
Browse files

Implemented flyspray request 1041 with patch by Steffen.

Improved it such that placement new is only called when necessary.

[[Imported from SVN: r1757]]
parent c6019ecf
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@ Copyright holders:
2009--2012 Andreas Lauser
2012 Tobias Malkmus
2007--2009 Sven Marnach
2013 Steffen Müthing
2003--2005 Thimo Neubauer
2010--2012 Rebecca Neumann
2012 Andreas Nüßing
......
......@@ -14,6 +14,7 @@
#include "istlexception.hh"
#include "bvector.hh"
#include "matrixutils.hh"
#include <dune/common/shared_ptr.hh>
#include <dune/common/stdstreams.hh>
#include <dune/common/iteratorfacades.hh>
......@@ -633,9 +634,11 @@ namespace Dune {
// memory is allocated individually per row
// allocate and set row i
B* a = Mat.allocator_.allocate(s);
new (a) B[s];
if(!MatrixHasTrivialConstructor<B>::value)
// use placement new to call constructor that allocates
// additional memory.
new (a) B[s];
size_type* j = Mat.sizeAllocator_.allocate(s);
new (j) size_type[s];
Mat.r[i].set(s,a,j);
}
}else
......@@ -1414,7 +1417,6 @@ namespace Dune {
if(allocateRows) {
if (n>0) {
r = rowAllocator_.allocate(rows);
new (r) row_type[rows];
}else{
r = 0;
}
......@@ -1424,6 +1426,11 @@ namespace Dune {
// allocate a and j array
if (nnz>0) {
a = allocator_.allocate(nnz);
if(!MatrixHasTrivialConstructor<B>::value)
// use placement new to call constructor that allocates
// additional memory.
new (a) B[nnz];
// allocate column indices only if not yet present (enable sharing)
if (!j.get())
j.reset(sizeAllocator_.allocate(nnz),Deallocator(sizeAllocator_));
......
......@@ -480,6 +480,26 @@ namespace Dune
value = true
};
};
//! \brief TMP telling whether a matrix type hase a trivial constructor.
//!
//! We call a constructor trivial if we can already a assign value
//! to a matrix once memory for it is allocated. That is, the
//! constructor may not allocate any additional memory.
//! \tparam The type of the matrix
template<typename M>
struct MatrixHasTrivialConstructor
{
enum {
//! \brief Whether this matrix has a trivial constructor
value=0
};
};
template<typename B, int n, int m>
struct MatrixHasTrivialConstructor<FieldMatrix<B,n,m> >
{
enum {value=1};
};
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment