Skip to content

Commit

Permalink
Less Leaky MooseArray
Browse files Browse the repository at this point in the history
refs #000
  • Loading branch information
permcody committed Feb 22, 2019
1 parent 715a6fd commit 6eb448c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
53 changes: 32 additions & 21 deletions framework/include/utils/MooseArray.h
Expand Up @@ -20,25 +20,36 @@ class MooseArray
/**
* Default constructor. Doesn't initialize anything.
*/
MooseArray() : _data(NULL), _size(0), _allocated_size(0) {}
MooseArray() : _data(nullptr), _size(0), _allocated_size(0) {}

/**
* @param size The initial size of the array.
*/
explicit MooseArray(const unsigned int size) : _data(NULL), _allocated_size(0) { resize(size); }
explicit MooseArray(const unsigned int size) : _data(nullptr), _allocated_size(0)
{
resize(size);
}

/**
* @param size The initial size of the array.
* @param default_value The default value to set.
*/
explicit MooseArray(const unsigned int size, const T & default_value)
: _data(NULL), _allocated_size(0)
: _data(nullptr), _allocated_size(0)
{
resize(size);

setAllValues(default_value);
}

explicit MooseArray(const MooseArray & rhs) : _data(nullptr), _allocated_size(0)
{
resize(rhs._size);

for (unsigned int i = 0; i < _size; i++)
_data[i] = rhs._data[i];
}

/**
* Sets all values of the array to the passed in value
* @param value The value every entry of the array will be set to.
Expand All @@ -50,10 +61,10 @@ class MooseArray
*/
void release()
{
if (_data != NULL)
if (_data_ptr)
{
delete[] _data;
_data = NULL;
_data_ptr.reset();
_data = nullptr;
_allocated_size = _size = 0;
}
}
Expand All @@ -79,7 +90,7 @@ class MooseArray
* Note that this does _not_ free unused memory.
* This is done for speed.
*/
void resize(const unsigned int size);
void resize(unsigned int size);

/**
* Change the number of elements the array can store.
Expand All @@ -94,7 +105,7 @@ class MooseArray
*
* Also note that default_value is only applied to NEW entries.
*/
void resize(const unsigned int size, const T & default_value);
void resize(unsigned int size, const T & default_value);

/**
* The number of elements that can currently
Expand Down Expand Up @@ -175,7 +186,10 @@ class MooseArray
const T * data() const { return _data; }

private:
/// Actual data pointer.
/// Smart pointer storage
std::unique_ptr<T[]> _data_ptr;

// Actual data pointer (from inside of the smart pointer).
T * _data;

/// The current number of elements the array can hold.
Expand All @@ -202,40 +216,36 @@ MooseArray<T>::clear()

template <typename T>
inline void
MooseArray<T>::resize(const unsigned int size)
MooseArray<T>::resize(unsigned int size)
{
if (size <= _allocated_size)
_size = size;
else
{
T * new_pointer = new T[size];
mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
_data_ptr.reset(new T[size]);
mooseAssert(_data_ptr, "Failed to allocate MooseArray memory!");

if (_data != NULL)
delete[] _data;
_data = new_pointer;
_data = _data_ptr.get();
_allocated_size = size;
_size = size;
}
}

template <typename T>
inline void
MooseArray<T>::resize(const unsigned int size, const T & default_value)
MooseArray<T>::resize(unsigned int size, const T & default_value)
{
if (size > _allocated_size)
{
T * new_pointer = new T[size];
mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");

if (_data != NULL)
{
if (_data)
for (unsigned int i = 0; i < _size; i++)
new_pointer[i] = _data[i];
delete[] _data;
}

_data = new_pointer;
_data_ptr.reset(new_pointer);
_data = _data_ptr.get();
_allocated_size = size;
}

Expand Down Expand Up @@ -274,6 +284,7 @@ template <typename T>
inline void
MooseArray<T>::swap(MooseArray & rhs)
{
_data_ptr.swap(rhs._data_ptr);
std::swap(_data, rhs._data);
std::swap(_size, rhs._size);
std::swap(_allocated_size, rhs._allocated_size);
Expand Down
2 changes: 1 addition & 1 deletion framework/src/outputs/Exodus.C
Expand Up @@ -297,7 +297,7 @@ Exodus::outputScalarVariables()

MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
scalar_var.reinit();
VariableValue value = scalar_var.sln();
VariableValue value(scalar_var.sln());

const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
const unsigned int n = dof_indices.size();
Expand Down
2 changes: 1 addition & 1 deletion framework/src/outputs/Nemesis.C
Expand Up @@ -106,7 +106,7 @@ Nemesis::outputScalarVariables()

MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
scalar_var.reinit();
VariableValue value = scalar_var.sln();
VariableValue value(scalar_var.sln());

const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
const unsigned int n = dof_indices.size();
Expand Down
2 changes: 1 addition & 1 deletion framework/src/outputs/TableOutput.C
Expand Up @@ -165,7 +165,7 @@ TableOutput::outputScalarVariables()
// Make a copy rather than taking a reference to the MooseArray,
// because if a processor can't see that scalar variable's values
// then we'll need to do our own communication of them.
VariableValue value = scalar_var.sln();
VariableValue value(scalar_var.sln());
auto value_size = value.size();

// libMesh *does* currently guarantee that all processors can
Expand Down

0 comments on commit 6eb448c

Please sign in to comment.