Skip to content

Commit

Permalink
Fixes #662: unique_span "hardening" and tweaks:
Browse files Browse the repository at this point in the history
* Can no longer construct a `unique_span` with another `unique_span` having a different deleter (via the implicit conversion into a span)
* Can no longer construct a `unique_span` from an untyped region; make a span and use that. We thus also avoid undesirable constructions via implicit conversions to regions
* Comment and spacing tweaks
  • Loading branch information
eyalroz committed Jul 26, 2024
1 parent f18303f commit 3d829a9
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/cuda/api/detail/unique_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ class unique_span : public ::cuda::span<T> {

constexpr unique_span() noexcept = default;

// Disable copy construction - as this class never allocates;
unique_span(const unique_span&) = delete;
// ... and also match other kinds of unique_span's, which may get converted into
// a span and thus leak memory on construction!
template<typename U, typename OtherDeleter>
unique_span(const unique_span<U, OtherDeleter>&) = delete;


/// Take ownership of an existing region or span
///
/// These ctors are all explicit to prevent accidentally relinquishing ownership
/// when passing to a function.
///@{
explicit unique_span(span_type span) noexcept : span_type{span} { }
explicit unique_span(pointer data, size_type size) noexcept : unique_span{span_type{data, size}} { }
explicit unique_span(memory::region_t region) noexcept : span_type{region.as_span<T>()} { }
///@}

// Note: No constructor which also takes a deleter. We do not hold a deleter
Expand All @@ -77,8 +84,10 @@ class unique_span : public ::cuda::span<T> {
* the user is strongly assumed not to use the unique_span after moving from it.
*/
unique_span(unique_span&& other) noexcept : unique_span{ other.release() } { }
// Disable copy construction - we do not allocate
unique_span(const unique_span&) = delete;

// Disable move construction from other kinds of unique_spans
template<typename U, typename OtherDeleter>
unique_span(unique_span<U, OtherDeleter>&&) = delete;

// Note: No conversion from "another type" like with ::std::unique_pointer, since
// this class is not variant with the element type; and there's not much sense in
Expand Down Expand Up @@ -127,7 +136,13 @@ class unique_span : public ::cuda::span<T> {
}

protected: // mutators
/// Release ownership of any stored pointer.
/**
* Release ownership of the stored span
*
* @note This is not marked nodiscard by the same argument as for std::unique_ptr;
* see also @url https://stackoverflow.com/q/60535399/1593077 and
* @url http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0600r1.pdf
*/
span_type release() noexcept
{
span_type released { data(), size() };
Expand Down

0 comments on commit 3d829a9

Please sign in to comment.