Skip to content

Commit

Permalink
STYLE: Declare metric "per thread" data members as unique_ptr<T[]>
Browse files Browse the repository at this point in the history
Seven registration metric class templates had a "raw pointer" internally,
pointing to "per thread" data, which was manually managed by `new T[]` and
`delete[]`. This commit declares them as `std::unique_ptr<T[]>` instead, and
"defaults" the destructors of those class templates.
  • Loading branch information
N-Dekker authored and dzenanz committed Sep 14, 2022
1 parent aeab4be commit 2f2b29c
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "itkBSplineDerivativeKernelFunction.h"
#include "itkArray2D.h"

#include <memory> // For unique_ptr.
#include <mutex>


Expand Down Expand Up @@ -259,7 +260,7 @@ class ITK_TEMPLATE_EXPORT MattesMutualInformationImageToImageMetric

protected:
MattesMutualInformationImageToImageMetric();
~MattesMutualInformationImageToImageMetric() override;
~MattesMutualInformationImageToImageMetric() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;

Expand Down Expand Up @@ -360,7 +361,7 @@ class ITK_TEMPLATE_EXPORT MattesMutualInformationImageToImageMetric
// See
// https://thetweaker.wordpress.com/2010/05/05/stdvector-of-aligned-elements/
// https://connect.microsoft.com/VisualStudio/feedback/details/692988
AlignedMMIMetricPerThreadStruct * m_MMIMetricPerThreadVariables;
std::unique_ptr<AlignedMMIMetricPerThreadStruct[]> m_MMIMetricPerThreadVariables;
#endif

bool m_UseExplicitPDFDerivatives{ true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkImageIterator.h"
#include "itkMath.h"
#include "itkStatisticsImageFilter.h"
#include "itkMakeUniqueForOverwrite.h"

#include "vnl/vnl_vector.h"
#include "vnl/vnl_c_vector.h"
Expand All @@ -46,12 +47,6 @@ MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::MattesMutu
this->m_ComputeGradient = false;
}

template <typename TFixedImage, typename TMovingImage>
MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::~MattesMutualInformationImageToImageMetric()
{
delete[] this->m_MMIMetricPerThreadVariables;
}

template <typename TFixedImage, typename TMovingImage>
void
MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::PrintSelf(std::ostream & os, Indent indent) const
Expand Down Expand Up @@ -217,8 +212,8 @@ MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::Initialize
*/
this->m_MovingImageMarginalPDF.resize(m_NumberOfHistogramBins, 0.0F);

delete[] this->m_MMIMetricPerThreadVariables;
this->m_MMIMetricPerThreadVariables = new AlignedMMIMetricPerThreadStruct[this->m_NumberOfWorkUnits];
this->m_MMIMetricPerThreadVariables =
make_unique_for_overwrite<AlignedMMIMetricPerThreadStruct[]>(this->m_NumberOfWorkUnits);

{
const int binRange = this->m_NumberOfHistogramBins / this->m_NumberOfWorkUnits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "itkPoint.h"
#include "itkIndex.h"

#include <memory> // For unique_ptr.


namespace itk
{
Expand Down Expand Up @@ -104,7 +106,7 @@ class ITK_TEMPLATE_EXPORT MeanSquaresImageToImageMetric : public ImageToImageMet

protected:
MeanSquaresImageToImageMetric();
~MeanSquaresImageToImageMetric() override;
~MeanSquaresImageToImageMetric() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;

Expand All @@ -130,7 +132,7 @@ class ITK_TEMPLATE_EXPORT MeanSquaresImageToImageMetric : public ImageToImageMet
};

itkAlignedTypedef(64, PerThreadS, AlignedPerThreadType);
AlignedPerThreadType * m_PerThread;
std::unique_ptr<AlignedPerThreadType[]> m_PerThread;
};
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkImageRegionIterator.h"
#include "itkImageIterator.h"
#include "itkMath.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand All @@ -33,7 +34,6 @@ MeanSquaresImageToImageMetric<TFixedImage, TMovingImage>::MeanSquaresImageToImag
{
this->SetComputeGradient(true);

m_PerThread = nullptr;
this->m_WithinThreadPreProcess = false;
this->m_WithinThreadPostProcess = false;

Expand All @@ -43,13 +43,6 @@ MeanSquaresImageToImageMetric<TFixedImage, TMovingImage>::MeanSquaresImageToImag
this->SetUseAllPixels(true);
}

template <typename TFixedImage, typename TMovingImage>
MeanSquaresImageToImageMetric<TFixedImage, TMovingImage>::~MeanSquaresImageToImageMetric()
{
delete[] m_PerThread;
m_PerThread = nullptr;
}

/**
* Print out internal information about this class
*/
Expand All @@ -70,9 +63,7 @@ MeanSquaresImageToImageMetric<TFixedImage, TMovingImage>::Initialize()
this->Superclass::Initialize();
this->Superclass::MultiThreadingInitialize();

delete[] m_PerThread;

m_PerThread = new AlignedPerThreadType[this->m_NumberOfWorkUnits];
m_PerThread = make_unique_for_overwrite<AlignedPerThreadType[]>(this->m_NumberOfWorkUnits);

for (ThreadIdType workUnitID = 0; workUnitID < this->m_NumberOfWorkUnits; ++workUnitID)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "itkImageToImageMetricv4GetValueAndDerivativeThreader.h"

#include <memory> // For unique_ptr.

namespace itk
{

Expand Down Expand Up @@ -73,7 +75,7 @@ class ITK_TEMPLATE_EXPORT CorrelationImageToImageMetricv4GetValueAndDerivativeTh

protected:
CorrelationImageToImageMetricv4GetValueAndDerivativeThreader();
~CorrelationImageToImageMetricv4GetValueAndDerivativeThreader() override;
~CorrelationImageToImageMetricv4GetValueAndDerivativeThreader() override = default;

/** Overload: Resize and initialize per thread objects:
* number of valid points
Expand Down Expand Up @@ -147,7 +149,8 @@ class ITK_TEMPLATE_EXPORT CorrelationImageToImageMetricv4GetValueAndDerivativeTh
PaddedCorrelationMetricValueDerivativePerThreadStruct,
AlignedCorrelationMetricValueDerivativePerThreadStruct);
/* per thread variables for correlation and its derivatives */
AlignedCorrelationMetricValueDerivativePerThreadStruct * m_CorrelationMetricValueDerivativePerThreadVariables;
std::unique_ptr<AlignedCorrelationMetricValueDerivativePerThreadStruct[]>
m_CorrelationMetricValueDerivativePerThreadVariables;

/** Internal pointer to the metric object in use by this threader.
* This will avoid costly dynamic casting in tight loops. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef itkCorrelationImageToImageMetricv4GetValueAndDerivativeThreader_hxx
#define itkCorrelationImageToImageMetricv4GetValueAndDerivativeThreader_hxx

#include "itkMakeUniqueForOverwrite.h"


namespace itk
{
Expand All @@ -32,16 +34,6 @@ CorrelationImageToImageMetricv4GetValueAndDerivativeThreader<
{}


template <typename TDomainPartitioner, typename TImageToImageMetric, typename TCorrelationMetric>
CorrelationImageToImageMetricv4GetValueAndDerivativeThreader<
TDomainPartitioner,
TImageToImageMetric,
TCorrelationMetric>::~CorrelationImageToImageMetricv4GetValueAndDerivativeThreader()
{
delete[] m_CorrelationMetricValueDerivativePerThreadVariables;
}


template <typename TDomainPartitioner, typename TImageToImageMetric, typename TCorrelationMetric>
void
CorrelationImageToImageMetricv4GetValueAndDerivativeThreader<TDomainPartitioner,
Expand All @@ -62,9 +54,8 @@ CorrelationImageToImageMetricv4GetValueAndDerivativeThreader<TDomainPartitioner,

const ThreadIdType numWorkUnitsUsed = this->GetNumberOfWorkUnitsUsed();
// set size
delete[] m_CorrelationMetricValueDerivativePerThreadVariables;
m_CorrelationMetricValueDerivativePerThreadVariables =
new AlignedCorrelationMetricValueDerivativePerThreadStruct[numWorkUnitsUsed];
make_unique_for_overwrite<AlignedCorrelationMetricValueDerivativePerThreadStruct[]>(numWorkUnitsUsed);
for (ThreadIdType i = 0; i < numWorkUnitsUsed; ++i)
{
this->m_CorrelationMetricValueDerivativePerThreadVariables[i].fdm.SetSize(globalDerivativeSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "itkImageToImageMetricv4GetValueAndDerivativeThreader.h"

#include <memory> // For unique_ptr.

namespace itk
{

Expand Down Expand Up @@ -73,7 +75,7 @@ class ITK_TEMPLATE_EXPORT CorrelationImageToImageMetricv4HelperThreader

protected:
CorrelationImageToImageMetricv4HelperThreader();
~CorrelationImageToImageMetricv4HelperThreader() override;
~CorrelationImageToImageMetricv4HelperThreader() override = default;

/** Overload: Resize and initialize per thread objects. */
void
Expand Down Expand Up @@ -132,7 +134,7 @@ class ITK_TEMPLATE_EXPORT CorrelationImageToImageMetricv4HelperThreader
PaddedCorrelationMetricPerThreadStruct,
AlignedCorrelationMetricPerThreadStruct);
/* per thread variables for correlation and its derivatives */
AlignedCorrelationMetricPerThreadStruct * m_CorrelationMetricPerThreadVariables;
std::unique_ptr<AlignedCorrelationMetricPerThreadStruct[]> m_CorrelationMetricPerThreadVariables;

/** Internal pointer to the metric object in use by this threader.
* This will avoid costly dynamic casting in tight loops. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef itkCorrelationImageToImageMetricv4HelperThreader_hxx
#define itkCorrelationImageToImageMetricv4HelperThreader_hxx

#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand All @@ -30,14 +31,6 @@ CorrelationImageToImageMetricv4HelperThreader<TDomainPartitioner, TImageToImageM
{}


template <typename TDomainPartitioner, typename TImageToImageMetric, typename TCorrelationMetric>
CorrelationImageToImageMetricv4HelperThreader<TDomainPartitioner, TImageToImageMetric, TCorrelationMetric>::
~CorrelationImageToImageMetricv4HelperThreader()
{
delete[] m_CorrelationMetricPerThreadVariables;
}


template <typename TDomainPartitioner, typename TImageToImageMetric, typename TCorrelationMetric>
void
CorrelationImageToImageMetricv4HelperThreader<TDomainPartitioner, TImageToImageMetric, TCorrelationMetric>::
Expand All @@ -49,8 +42,8 @@ CorrelationImageToImageMetricv4HelperThreader<TDomainPartitioner, TImageToImageM
this->m_CorrelationAssociate = dynamic_cast<TCorrelationMetric *>(this->m_Associate);

const ThreadIdType numWorkUnitsUsed = this->GetNumberOfWorkUnitsUsed();
delete[] this->m_CorrelationMetricPerThreadVariables;
this->m_CorrelationMetricPerThreadVariables = new AlignedCorrelationMetricPerThreadStruct[numWorkUnitsUsed];
this->m_CorrelationMetricPerThreadVariables =
make_unique_for_overwrite<AlignedCorrelationMetricPerThreadStruct[]>(numWorkUnitsUsed);

//---------------------------------------------------------------
// Set initial values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "itkDomainThreader.h"
#include "itkCompensatedSummation.h"

#include <memory> // For unique_ptr.

namespace itk
{

Expand Down Expand Up @@ -95,7 +97,7 @@ class ITK_TEMPLATE_EXPORT ImageToImageMetricv4GetValueAndDerivativeThreaderBase

protected:
ImageToImageMetricv4GetValueAndDerivativeThreaderBase();
~ImageToImageMetricv4GetValueAndDerivativeThreaderBase() override;
~ImageToImageMetricv4GetValueAndDerivativeThreaderBase() override = default;

/** Resize and initialize per thread objects. */
void
Expand Down Expand Up @@ -191,7 +193,7 @@ class ITK_TEMPLATE_EXPORT ImageToImageMetricv4GetValueAndDerivativeThreaderBase
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT,
PaddedGetValueAndDerivativePerThreadStruct,
AlignedGetValueAndDerivativePerThreadStruct);
AlignedGetValueAndDerivativePerThreadStruct * m_GetValueAndDerivativePerThreadVariables;
std::unique_ptr<AlignedGetValueAndDerivativePerThreadStruct[]> m_GetValueAndDerivativePerThreadVariables;

/** Cached values to avoid call overhead.
* These will only be set once threading has been started. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkImageToImageMetricv4GetValueAndDerivativeThreaderBase_hxx

#include "itkNumericTraits.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand All @@ -31,13 +32,6 @@ ImageToImageMetricv4GetValueAndDerivativeThreaderBase<TDomainPartitioner, TImage
, m_CachedNumberOfLocalParameters(0)
{}

template <typename TDomainPartitioner, typename TImageToImageMetricv4>
ImageToImageMetricv4GetValueAndDerivativeThreaderBase<TDomainPartitioner, TImageToImageMetricv4>::
~ImageToImageMetricv4GetValueAndDerivativeThreaderBase()
{
delete[] m_GetValueAndDerivativePerThreadVariables;
}

template <typename TDomainPartitioner, typename TImageToImageMetricv4>
void
ImageToImageMetricv4GetValueAndDerivativeThreaderBase<TDomainPartitioner,
Expand All @@ -52,8 +46,8 @@ ImageToImageMetricv4GetValueAndDerivativeThreaderBase<TDomainPartitioner,

/* Per-thread results */
const ThreadIdType numWorkUnitsUsed = this->GetNumberOfWorkUnitsUsed();
delete[] m_GetValueAndDerivativePerThreadVariables;
this->m_GetValueAndDerivativePerThreadVariables = new AlignedGetValueAndDerivativePerThreadStruct[numWorkUnitsUsed];
this->m_GetValueAndDerivativePerThreadVariables =
make_unique_for_overwrite<AlignedGetValueAndDerivativePerThreadStruct[]>(numWorkUnitsUsed);

if (this->m_Associate->GetComputeDerivative())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "itkDomainThreader.h"
#include "itkImage.h"

#include <memory> // For unique_ptr.

namespace itk
{

Expand Down Expand Up @@ -65,7 +67,7 @@ class ITK_TEMPLATE_EXPORT JointHistogramMutualInformationComputeJointPDFThreader

protected:
JointHistogramMutualInformationComputeJointPDFThreaderBase();
~JointHistogramMutualInformationComputeJointPDFThreaderBase() override;
~JointHistogramMutualInformationComputeJointPDFThreaderBase() override = default;

/** Create the \c m_JointPDFPerThread's. */
void
Expand All @@ -92,7 +94,7 @@ class ITK_TEMPLATE_EXPORT JointHistogramMutualInformationComputeJointPDFThreader
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT,
PaddedJointHistogramMIPerThreadStruct,
AlignedJointHistogramMIPerThreadStruct);
AlignedJointHistogramMIPerThreadStruct * m_JointHistogramMIPerThreadVariables;
std::unique_ptr<AlignedJointHistogramMIPerThreadStruct[]> m_JointHistogramMIPerThreadVariables;
};

} // end namespace itk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


#include "itkImageRegionIterator.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand All @@ -30,21 +31,14 @@ JointHistogramMutualInformationComputeJointPDFThreaderBase<TDomainPartitioner, T
: m_JointHistogramMIPerThreadVariables(nullptr)
{}

template <typename TDomainPartitioner, typename TJointHistogramMetric>
JointHistogramMutualInformationComputeJointPDFThreaderBase<TDomainPartitioner, TJointHistogramMetric>::
~JointHistogramMutualInformationComputeJointPDFThreaderBase()
{
delete[] this->m_JointHistogramMIPerThreadVariables;
}

template <typename TDomainPartitioner, typename TJointHistogramMetric>
void
JointHistogramMutualInformationComputeJointPDFThreaderBase<TDomainPartitioner,
TJointHistogramMetric>::BeforeThreadedExecution()
{
const ThreadIdType numWorkUnitsUsed = this->GetNumberOfWorkUnitsUsed();
delete[] this->m_JointHistogramMIPerThreadVariables;
this->m_JointHistogramMIPerThreadVariables = new AlignedJointHistogramMIPerThreadStruct[numWorkUnitsUsed];
this->m_JointHistogramMIPerThreadVariables =
make_unique_for_overwrite<AlignedJointHistogramMIPerThreadStruct[]>(numWorkUnitsUsed);
for (ThreadIdType i = 0; i < numWorkUnitsUsed; ++i)
{
if (this->m_JointHistogramMIPerThreadVariables[i].JointHistogram.IsNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "itkImageToImageMetricv4GetValueAndDerivativeThreader.h"

#include <memory> // For unique_ptr.

namespace itk
{

Expand Down Expand Up @@ -79,7 +81,7 @@ class ITK_TEMPLATE_EXPORT JointHistogramMutualInformationGetValueAndDerivativeTh

protected:
JointHistogramMutualInformationGetValueAndDerivativeThreader();
~JointHistogramMutualInformationGetValueAndDerivativeThreader() override;
~JointHistogramMutualInformationGetValueAndDerivativeThreader() override = default;

using JointHistogramType = Image<SizeValueType, 2>;

Expand Down Expand Up @@ -122,7 +124,7 @@ class ITK_TEMPLATE_EXPORT JointHistogramMutualInformationGetValueAndDerivativeTh
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT,
PaddedJointHistogramMIPerThreadStruct,
AlignedJointHistogramMIPerThreadStruct);
AlignedJointHistogramMIPerThreadStruct * m_JointHistogramMIPerThreadVariables;
std::unique_ptr<AlignedJointHistogramMIPerThreadStruct[]> m_JointHistogramMIPerThreadVariables;

private:
/** Internal pointer to the metric object in use by this threader.
Expand Down
Loading

0 comments on commit 2f2b29c

Please sign in to comment.