0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 08:12:37 +01:00

ircd::allocator: Minor reorg; dedup alignment utils.

This commit is contained in:
Jason Volk 2020-11-01 04:03:02 -08:00
parent 9950876e09
commit a0a3eeb16e
2 changed files with 61 additions and 61 deletions

View file

@ -33,9 +33,6 @@ namespace ircd::allocator
size_t rlimit_memlock();
size_t rlimit_memlock(const size_t &request);
std::unique_ptr<char, decltype(&std::free)>
aligned_alloc(const size_t &align, const size_t &size);
profile &operator+=(profile &, const profile &);
profile &operator-=(profile &, const profile &);
profile operator+(const profile &, const profile &);
@ -48,6 +45,9 @@ namespace ircd::allocator
template<class T, class R> R &set(const string_view &var, T val, R &cur);
template<class T> T set(const string_view &var, T val);
bool trim(const size_t &pad = 0) noexcept; // malloc_trim(3)
std::unique_ptr<char, decltype(&std::free)>
aligned_alloc(const size_t &align, const size_t &size);
}
/// jemalloc specific suite; note that some of the primary ircd::allocator

View file

@ -18,6 +18,64 @@
//
// #define RB_PROF_ALLOC
std::unique_ptr<char, decltype(&std::free)>
ircd::allocator::aligned_alloc(const size_t &alignment_,
const size_t &size_)
{
constexpr auto align_default
{
sizeof(void *)
};
const auto alignment
{
alignment_?: align_default
};
const auto size
{
pad_to(size_, alignment)
};
assert(size % alignment == 0);
assert(size < size_ + alignment);
assert(alignment % sizeof(void *) == 0);
void *ret;
switch(int errc(::posix_memalign(&ret, alignment, size)); errc)
{
case 0:
break;
case int(std::errc::not_enough_memory):
throw std::bad_alloc{};
default:
throw std::system_error
{
errc, std::system_category()
};
}
assert(ret != nullptr);
assert(uintptr_t(ret) % alignment == 0);
#ifdef RB_PROF_ALLOC
auto &this_thread(ircd::allocator::profile::this_thread);
this_thread.alloc_bytes += size;
this_thread.alloc_count++;
#endif
return
{
reinterpret_cast<char *>(ret), &std::free
};
}
//
// control panel
//
bool
__attribute__((weak))
ircd::allocator::trim(const size_t &pad)
@ -239,64 +297,6 @@ ircd::allocator::operator+=(profile &a,
return a;
}
//
// aligned_alloc
//
std::unique_ptr<char, decltype(&std::free)>
ircd::allocator::aligned_alloc(const size_t &alignment_,
const size_t &size_)
{
static const size_t &align_default
{
16
};
const size_t &alignment
{
alignment_?: align_default
};
const size_t &size
{
size_ % alignment == 0? size_: size_ + (alignment - (size_ % alignment))
};
assert(size % alignment == 0);
assert(size < size_ + alignment);
assert(alignment % sizeof(void *) == 0);
void *ret;
switch(int errc(::posix_memalign(&ret, alignment, size)); errc)
{
case 0:
break;
case int(std::errc::not_enough_memory):
throw std::bad_alloc{};
default:
throw std::system_error
{
errc, std::system_category()
};
}
assert(ret != nullptr);
assert(uintptr_t(ret) % alignment == 0);
#ifdef RB_PROF_ALLOC
auto &this_thread(ircd::allocator::profile::this_thread);
this_thread.alloc_bytes += size;
this_thread.alloc_count++;
#endif
return
{
reinterpret_cast<char *>(ret), &std::free
};
}
//
// resource limits
//