Commit 0ff83e94 authored by Kenneth Moreland's avatar Kenneth Moreland

Properly handle conditions when VirtualStorage is null

If an ArrayHandleVirtual is constructed without an underlying concrete
array handle, then the Storage<T, StorageTagVirtual> holds a
StorageVirtual pointer that is null. Generally, a null
ArrayHandleVirtual cannot do much, but its operations should still be
correct. There were a few places where the Storage would blindly try to
use its StorageVirtual pointer without checking it first. This adds some
conditions that should correct the behavior when StorageVirtual is null.
parent 0571c633
......@@ -269,23 +269,13 @@ public:
vtkm::Id GetNumberOfValues() const { return this->VirtualStorage->GetNumberOfValues(); }
void Allocate(vtkm::Id numberOfValues) { this->VirtualStorage->Allocate(numberOfValues); }
void Allocate(vtkm::Id numberOfValues);
void Shrink(vtkm::Id numberOfValues) { this->VirtualStorage->Shrink(numberOfValues); }
void Shrink(vtkm::Id numberOfValues);
void ReleaseResources() { this->VirtualStorage->ReleaseResources(); }
void ReleaseResources();
Storage<T, vtkm::cont::StorageTagVirtual> NewInstance() const
{
if (this->GetStorageVirtual())
{
return Storage<T, vtkm::cont::StorageTagVirtual>(this->GetStorageVirtual()->NewInstance());
}
else
{
return Storage<T, vtkm::cont::StorageTagVirtual>();
}
}
Storage<T, vtkm::cont::StorageTagVirtual> NewInstance() const;
const detail::StorageVirtual* GetStorageVirtual() const { return this->VirtualStorage.get(); }
......
......@@ -239,9 +239,72 @@ void StorageVirtualImpl<T, S>::TransferPortalForOutput(
vtkm::cont::TryExecuteOnDevice(
devId, detail::PortalWrapperToDevice(), this->Handle, numberOfValues, payload, mode);
}
} // namespace detail
template <typename T>
void Storage<T, vtkm::cont::StorageTagVirtual>::Allocate(vtkm::Id numberOfValues)
{
if (this->VirtualStorage)
{
this->VirtualStorage->Allocate(numberOfValues);
}
else if (numberOfValues != 0)
{
throw vtkm::cont::ErrorBadAllocation("Attempted to allocate memory in a virtual array that "
"does not have an underlying concrete array.");
}
else
{
// Allocating a non-existing array to 0 is OK.
}
}
template <typename T>
void Storage<T, vtkm::cont::StorageTagVirtual>::Shrink(vtkm::Id numberOfValues)
{
if (this->VirtualStorage)
{
this->VirtualStorage->Shrink(numberOfValues);
}
else if (numberOfValues != 0)
{
throw vtkm::cont::ErrorBadAllocation(
"Attempted to shrink a virtual array that does not have an underlying concrete array.");
}
else
{
// Shrinking a non-existing array to 0 is OK.
}
}
template <typename T>
void Storage<T, vtkm::cont::StorageTagVirtual>::ReleaseResources()
{
if (this->VirtualStorage)
{
this->VirtualStorage->ReleaseResources();
}
else
{
// No concrete array, nothing allocated, nothing to do.
}
}
template <typename T>
Storage<T, vtkm::cont::StorageTagVirtual> Storage<T, vtkm::cont::StorageTagVirtual>::NewInstance()
const
{
if (this->GetStorageVirtual())
{
return Storage<T, vtkm::cont::StorageTagVirtual>(this->GetStorageVirtual()->NewInstance());
}
else
{
return Storage<T, vtkm::cont::StorageTagVirtual>();
}
}
}
} // namespace internal::detail
}
} // namespace vtkm::cont::
} // namespace vtkm::cont::internal
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment