Skip to content
Permalink
Browse files

Merge pull request #12949 from permcody/moose_array

Less Leaky MooseArray Experiment
  • Loading branch information...
friedmud committed Mar 5, 2019
2 parents 20526db + 6eb448c commit d8adc409946101526c713f42f7490341e3896162
@@ -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.
@@ -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;
}
}
@@ -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.
@@ -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
@@ -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.
@@ -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;
}

@@ -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);
@@ -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();
@@ -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();
@@ -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

0 comments on commit d8adc40

Please sign in to comment.
You can’t perform that action at this time.