mirror of
https://github.com/matrix-construct/construct
synced 2024-11-17 15:30:52 +01:00
ircd::buffer: Abstract the aligned_alloc() out of the unique_buffer template.
This commit is contained in:
parent
916a5b26ea
commit
10cf8e798c
1 changed files with 22 additions and 1 deletions
|
@ -11,6 +11,11 @@
|
|||
#pragma once
|
||||
#define HAVE_IRCD_BUFFER_UNIQUE_BUFFER_H
|
||||
|
||||
namespace ircd::buffer
|
||||
{
|
||||
std::unique_ptr<char, decltype(&std::free)> aligned_alloc(const size_t &align, const size_t &size);
|
||||
}
|
||||
|
||||
/// Like unique_ptr, this template holds ownership of an allocated buffer
|
||||
///
|
||||
template<class buffer,
|
||||
|
@ -116,6 +121,19 @@ template<class buffer,
|
|||
char *
|
||||
ircd::buffer::unique_buffer<buffer, alignment>::allocate(const size_t &size)
|
||||
{
|
||||
return aligned_alloc(alignment, size).release();
|
||||
}
|
||||
|
||||
inline std::unique_ptr<char, decltype(&std::free)>
|
||||
ircd::buffer::aligned_alloc(const size_t &align,
|
||||
const size_t &size)
|
||||
{
|
||||
static const size_t &align_default{16};
|
||||
const size_t &alignment
|
||||
{
|
||||
align?: align_default
|
||||
};
|
||||
|
||||
int errc;
|
||||
void *ret;
|
||||
if(unlikely((errc = ::posix_memalign(&ret, alignment, size)) != 0))
|
||||
|
@ -126,5 +144,8 @@ ircd::buffer::unique_buffer<buffer, alignment>::allocate(const size_t &size)
|
|||
|
||||
assert(errc == 0);
|
||||
assert(ret != nullptr);
|
||||
return reinterpret_cast<char *>(ret);
|
||||
return std::unique_ptr<char, decltype(&std::free)>
|
||||
{
|
||||
reinterpret_cast<char *>(ret), std::free
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue