0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 19:58:53 +02:00

ircd::ios: Add interface for user allocation closure at descriptor site.

This commit is contained in:
Jason Volk 2019-03-27 02:49:28 -07:00
parent 02f413c834
commit 3225c1fdfe
2 changed files with 34 additions and 4 deletions

View file

@ -70,6 +70,9 @@ struct ircd::ios::descriptor
{ {
static uint64_t ids; static uint64_t ids;
static void *default_allocator(handler &, const size_t &);
static void default_deallocator(handler &, void *const &, const size_t &);
string_view name; string_view name;
uint64_t id {++ids}; uint64_t id {++ids};
uint64_t calls {0}; uint64_t calls {0};
@ -81,7 +84,13 @@ struct ircd::ios::descriptor
uint64_t slice_total {0}; uint64_t slice_total {0};
uint64_t slice_last {0}; uint64_t slice_last {0};
descriptor(const string_view &name); std::function<void *(handler &, const size_t &)> allocator;
std::function<void (handler &, void *const &, const size_t &)> deallocator;
descriptor(const string_view &name,
const decltype(allocator) & = default_allocator,
const decltype(deallocator) & = default_deallocator);
descriptor(descriptor &&) = delete; descriptor(descriptor &&) = delete;
descriptor(const descriptor &) = delete; descriptor(const descriptor &) = delete;
~descriptor() noexcept; ~descriptor() noexcept;

View file

@ -124,9 +124,15 @@ ircd::ios::descriptor::ids;
// descriptor::descriptor // descriptor::descriptor
// //
ircd::ios::descriptor::descriptor(const string_view &name) ircd::ios::descriptor::descriptor(const string_view &name,
const decltype(allocator) &allocator,
const decltype(deallocator) &deallocator)
:name{name} :name{name}
,allocator{allocator}
,deallocator{deallocator}
{ {
assert(allocator);
assert(deallocator);
} }
ircd::ios::descriptor::~descriptor() ircd::ios::descriptor::~descriptor()
@ -134,6 +140,21 @@ noexcept
{ {
} }
void
ircd::ios::descriptor::default_deallocator(handler &handler,
void *const &ptr,
const size_t &size)
{
::operator delete(ptr, size);
}
void *
ircd::ios::descriptor::default_allocator(handler &handler,
const size_t &size)
{
return ::operator new(size);
}
// //
// handler // handler
// //
@ -189,7 +210,7 @@ ircd::ios::handler::deallocate(handler *const &handler,
{ {
assert(handler && handler->descriptor); assert(handler && handler->descriptor);
auto &descriptor(*handler->descriptor); auto &descriptor(*handler->descriptor);
::operator delete(ptr, size); descriptor.deallocator(*handler, ptr, size);
descriptor.free_bytes += size; descriptor.free_bytes += size;
++descriptor.frees; ++descriptor.frees;
} }
@ -202,5 +223,5 @@ ircd::ios::handler::allocate(handler *const &handler,
auto &descriptor(*handler->descriptor); auto &descriptor(*handler->descriptor);
descriptor.alloc_bytes += size; descriptor.alloc_bytes += size;
++descriptor.allocs; ++descriptor.allocs;
return ::operator new(size); return descriptor.allocator(*handler, size);
} }