Skip to content

Commit

Permalink
Templatize internal utility aligned_alloc (Xilinx#7641)
Browse files Browse the repository at this point in the history
* Templatize internal utility aligned_alloc

Make aligned alloc return managed ptr of specified type.

Preserve existing usage of default void and exposed deleter, but
hide the deleter for specified types.

Feels like this could be more elegant with better use of C++ features.

Signed-off-by: Soren Soe <[email protected]>

* Expose detail type for convenience

Signed-off-by: Soren Soe <[email protected]>

---------

Signed-off-by: Soren Soe <[email protected]>
  • Loading branch information
stsoe authored Jul 26, 2023
1 parent c2c9573 commit 9036de7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
52 changes: 27 additions & 25 deletions src/runtime_src/core/common/AlignedAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,32 @@

namespace xrt_core {

// Memory alignment for DDR and AXI-MM trace access
template <typename T> class AlignedAllocator {
void *mBuffer;
size_t mCount;
public:
T *getBuffer() {
return (T *)mBuffer;
}

size_t size() const {
return mCount * sizeof(T);
}

AlignedAllocator(size_t alignment, size_t count) : mBuffer(0), mCount(count) {
if (xrt_core::posix_memalign(&mBuffer, alignment, count * sizeof(T))) {
mBuffer = 0;
}
}
~AlignedAllocator() {
if (mBuffer) {
xrt_core::aligned_ptr_deleter pDel;
pDel(mBuffer);
}
}
};
// Memory alignment for DDR and AXI-MM trace access
template <typename T>
class AlignedAllocator
{
void *mBuffer;
size_t mCount;
public:
T *getBuffer() {
return (T *)mBuffer;
}

size_t size() const {
return mCount * sizeof(T);
}

AlignedAllocator(size_t alignment, size_t count) : mBuffer(0), mCount(count) {
if (xrt_core::posix_memalign(&mBuffer, alignment, count * sizeof(T))) {
mBuffer = 0;
}
}
~AlignedAllocator() {
if (mBuffer) {
xrt_core::detail::aligned_ptr_deleter<void> pDel;
pDel(mBuffer);
}
}
};
}
#endif
48 changes: 42 additions & 6 deletions src/runtime_src/core/common/memalign.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,65 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
#endif
}

namespace detail {

template <typename MyType>
struct aligned_ptr_deleter
{
#if defined(_WINDOWS)
void operator() (void* ptr) { _aligned_free(ptr); }
void operator() (MyType* ptr) { _aligned_free(ptr); }
#else
void operator() (void* ptr) { free(ptr); }
void operator() (MyType* ptr) { free(ptr); }
#endif
};
using aligned_ptr_type = std::unique_ptr<void, aligned_ptr_deleter>;
inline aligned_ptr_type

template <typename MyType>
using aligned_ptr_t = std::unique_ptr<MyType, aligned_ptr_deleter<MyType>>;

// aligned_alloc - Allocated managed aligned memory
//
// Allocates size bytes of uninitialized storage whose alignment is
// specified by align. The allocated memory is managed by a
// unique_ptr to ensure proper freeing of the memory upon destruction.
template <typename MyType>
inline aligned_ptr_t<MyType>
aligned_alloc(size_t align, size_t size)
{
// power of 2
if (!align || (align & (align - 1)))
throw std::runtime_error("xrt_core::aligned_alloc requires power of 2 for alignment");

#if defined(_WINDOWS)
return aligned_ptr_type(_aligned_malloc(size, align));
return aligned_ptr_t<MyType>(reinterpret_cast<MyType*>(_aligned_malloc(size, align)));
#else
return aligned_ptr_type(::aligned_alloc(align, size));
return aligned_ptr_t<MyType>(reinterpret_cast<MyType*>(::aligned_alloc(align, size)));
#endif
}

} // detail

// This type is used in legacy interfaces
using aligned_ptr_type = detail::aligned_ptr_t<void>;

// Expose templated detail type for convenience in use as data member
template <typename MyType>
using aligned_ptr_t = detail::aligned_ptr_t<MyType>;

// Untyped aligned memory allocation
inline aligned_ptr_t<void>
aligned_alloc(size_t align, size_t size)
{
return detail::aligned_alloc<void>(align, size);
}

// Typed aligned memory allocation
template <typename MyType>
inline aligned_ptr_t<MyType>
aligned_alloc(size_t align)
{
return detail::aligned_alloc<MyType>(align, sizeof(MyType));
}

} // xrt_core

#endif

0 comments on commit 9036de7

Please sign in to comment.