Skip to content

Commit

Permalink
buffer add ownership flag to zero-copy constructor
Browse files Browse the repository at this point in the history
if this is not set then the memory is not deleted by the buffer instance
  • Loading branch information
burlen committed Aug 14, 2023
1 parent 6ab95cb commit dbfc22a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion hamr_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,11 @@ class HAMR_EXPORT buffer
* driver API is used to determine the device that
* allocated the memory.
* @param[in] ptr a pointer to the array
* @param[in] take set non-zero if the buffer should delete the passed
* memory using the named allocator
*/
buffer(allocator alloc, const hamr::stream &strm,
transfer sync, size_t size, int owner, T *ptr);
transfer sync, size_t size, int owner, T *ptr, int take = 1);

/** Construct by directly providing the buffer contents. This can be used
* for zero-copy transfer of data. One must also name the allocator type
Expand Down
8 changes: 6 additions & 2 deletions hamr_buffer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,18 @@ buffer<T>::buffer(allocator alloc, const hamr::stream &strm, transfer sync,
// --------------------------------------------------------------------------
template <typename T>
buffer<T>::buffer(allocator alloc, const hamr::stream &strm, transfer sync,
size_t size, int owner, T *ptr) : m_alloc(alloc), m_data(nullptr),
size_t size, int owner, T *ptr, int take) : m_alloc(alloc), m_data(nullptr),
m_size(size), m_capacity(size), m_owner(owner), m_stream(strm),
m_sync(sync)
{
assert_valid_allocator(alloc);

// create the deleter for the passed allocator
if (alloc == allocator::cpp)
if (!take)
{
m_data = std::shared_ptr<T>(ptr, [](T*){});
}
else if (alloc == allocator::cpp)
{
m_data = std::shared_ptr<T>(ptr, new_deleter<T>(ptr, m_size));
}
Expand Down

0 comments on commit dbfc22a

Please sign in to comment.