Skip to content

Commit

Permalink
STYLE: Update comments and parameter in the ImportImageContainer
Browse files Browse the repository at this point in the history
Updated comments and the parameter 'UseValueInitialization' better
describe the behavior of the ImportImageContainer.
The same parameter in VectorImage has also been updated.
Also removed comment from AllocateElements about 'new' and throwing
or not throwing an exception if allocation fails.
Closes InsightSoftwareConsortium#4359
  • Loading branch information
issakomi authored and N-Dekker committed Dec 5, 2023
1 parent a6036a8 commit 3ad51b4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
13 changes: 6 additions & 7 deletions Modules/Core/Common/include/itkImportImageContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ class ITK_TEMPLATE_EXPORT ImportImageContainer : public Object
* container. However, in this particular case, Reserve as a Resize
* semantics that is kept for backward compatibility reasons.
*
* If UseDefaultConstructor is true, then * the default constructor is used
* to initialize each element. POD date types initialize to zero.
* If UseValueInitialization is true, then POD types will be
* zero-initialized.
*
* \sa SetImportPointer() */
void
Reserve(ElementIdentifier size, const bool UseDefaultConstructor = false);
Reserve(ElementIdentifier size, const bool UseValueInitialization = false);

/** Tell the container to try to minimize its memory usage for
* storage of the current number of elements. If new memory is
Expand Down Expand Up @@ -162,12 +162,11 @@ class ITK_TEMPLATE_EXPORT ImportImageContainer : public Object
PrintSelf(std::ostream & os, Indent indent) const override;

/**
* Allocates elements of the array. If UseDefaultConstructor is true, then
* the default constructor is used to initialize each element. POD date types
* initialize to zero.
* Allocates elements of the array. If UseValueInitialization is true, then
* POD types will be zero-initialized.
*/
virtual TElement *
AllocateElements(ElementIdentifier size, bool UseDefaultConstructor = false) const;
AllocateElements(ElementIdentifier size, bool UseValueInitialization = false) const;

virtual void
DeallocateManagedMemory();
Expand Down
17 changes: 7 additions & 10 deletions Modules/Core/Common/include/itkImportImageContainer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ImportImageContainer<TElementIdentifier, TElement>::~ImportImageContainer()
*/
template <typename TElementIdentifier, typename TElement>
void
ImportImageContainer<TElementIdentifier, TElement>::Reserve(ElementIdentifier size, const bool UseDefaultConstructor)
ImportImageContainer<TElementIdentifier, TElement>::Reserve(ElementIdentifier size, const bool UseValueInitialization)
{
// Reserve has a Resize semantics. We keep it that way for
// backwards compatibility .
Expand All @@ -53,7 +53,7 @@ ImportImageContainer<TElementIdentifier, TElement>::Reserve(ElementIdentifier si
{
if (size > m_Capacity)
{
TElement * temp = this->AllocateElements(size, UseDefaultConstructor);
TElement * temp = this->AllocateElements(size, UseValueInitialization);
// only copy the portion of the data used in the old buffer
std::copy_n(m_ImportPointer, m_Size, temp);

Expand All @@ -73,7 +73,7 @@ ImportImageContainer<TElementIdentifier, TElement>::Reserve(ElementIdentifier si
}
else
{
m_ImportPointer = this->AllocateElements(size, UseDefaultConstructor);
m_ImportPointer = this->AllocateElements(size, UseValueInitialization);
m_Capacity = size;
m_Size = size;
m_ContainerManageMemory = true;
Expand Down Expand Up @@ -153,22 +153,19 @@ ImportImageContainer<TElementIdentifier, TElement>::SetImportPointer(TElement *
template <typename TElementIdentifier, typename TElement>
TElement *
ImportImageContainer<TElementIdentifier, TElement>::AllocateElements(ElementIdentifier size,
bool UseDefaultConstructor) const
bool UseValueInitialization) const
{
// Encapsulate all image memory allocation here to throw an
// exception when memory allocation fails even when the compiler
// does not do this by default.
TElement * data;

try
{
if (UseDefaultConstructor)
if (UseValueInitialization)
{
data = new TElement[size](); // POD types initialized to 0, others use default constructor.
data = new TElement[size]();
}
else
{
data = new TElement[size]; // Faster but uninitialized
data = new TElement[size];
}
}
catch (...)
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkVectorImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class ITK_TEMPLATE_EXPORT VectorImage : public ImageBase<VImageDimension>
/** Allocate the image memory. The size of the image must
* already be set, e.g. by calling SetRegions(). */
void
Allocate(bool UseDefaultConstructor = false) override;
Allocate(bool UseValueInitialization = false) override;

/** Restore the data object to its initial state. This means releasing
* memory. */
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkVectorImage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace itk
//----------------------------------------------------------------------------
template <typename TPixel, unsigned int VImageDimension>
void
VectorImage<TPixel, VImageDimension>::Allocate(const bool UseDefaultConstructor)
VectorImage<TPixel, VImageDimension>::Allocate(const bool UseValueInitialization)
{
if (m_VectorLength == 0)
{
Expand All @@ -46,7 +46,7 @@ VectorImage<TPixel, VImageDimension>::Allocate(const bool UseDefaultConstructor)
this->ComputeOffsetTable();
num = this->GetOffsetTable()[VImageDimension];

m_Buffer->Reserve(num * m_VectorLength, UseDefaultConstructor);
m_Buffer->Reserve(num * m_VectorLength, UseValueInitialization);
}

template <typename TPixel, unsigned int VImageDimension>
Expand Down

0 comments on commit 3ad51b4

Please sign in to comment.