|
|
|
@ -17,20 +17,16 @@
|
|
|
|
|
|
|
|
|
|
namespace srsran {
|
|
|
|
|
|
|
|
|
|
/// unique ptr with type-erased dtor, so that it can be used by any object or memory pool
|
|
|
|
|
constexpr size_t unique_pool_deleter_small_buffer = sizeof(void*) * 2u;
|
|
|
|
|
template <typename T>
|
|
|
|
|
using unique_pool_ptr = std::unique_ptr<T, srsran::move_callback<void(T*), unique_pool_deleter_small_buffer> >;
|
|
|
|
|
|
|
|
|
|
/// Common object pool interface
|
|
|
|
|
template <typename T>
|
|
|
|
|
class obj_pool_itf
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct pool_deallocator {
|
|
|
|
|
obj_pool_itf<T>* pool;
|
|
|
|
|
explicit pool_deallocator(obj_pool_itf<T>* pool_ = nullptr) : pool(pool_) {}
|
|
|
|
|
void operator()(void* ptr)
|
|
|
|
|
{
|
|
|
|
|
// dtor is not called, as object is going to be recycled
|
|
|
|
|
pool->do_deallocate(ptr);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
using object_type = T;
|
|
|
|
|
|
|
|
|
|
obj_pool_itf() = default;
|
|
|
|
@ -40,22 +36,26 @@ public:
|
|
|
|
|
obj_pool_itf& operator=(const obj_pool_itf&) = delete;
|
|
|
|
|
obj_pool_itf& operator=(obj_pool_itf&&) = delete;
|
|
|
|
|
|
|
|
|
|
virtual ~obj_pool_itf() = default;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<T, pool_deallocator> make()
|
|
|
|
|
{
|
|
|
|
|
return std::unique_ptr<T, pool_deallocator>(do_allocate(), pool_deallocator(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// defined in child class
|
|
|
|
|
virtual T* do_allocate() = 0;
|
|
|
|
|
virtual void do_deallocate(void* ptr) = 0;
|
|
|
|
|
virtual ~obj_pool_itf() = default;
|
|
|
|
|
virtual unique_pool_ptr<T> make() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// unique ptr with type-erased dtor, so that it can be used by any object pool
|
|
|
|
|
template <typename T>
|
|
|
|
|
using unique_pool_ptr = std::unique_ptr<T, typename obj_pool_itf<T>::pool_deallocator>;
|
|
|
|
|
/// Allocate object in memory pool
|
|
|
|
|
template <typename T, typename MemPool, typename... Args>
|
|
|
|
|
unique_pool_ptr<T> make_pool_obj_with_heap_fallback(MemPool& mempool, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
void* block = mempool.allocate(sizeof(T), alignof(T));
|
|
|
|
|
if (block == nullptr) {
|
|
|
|
|
return unique_pool_ptr<T>(new T(std::forward<Args>(args)...), std::default_delete<T>());
|
|
|
|
|
}
|
|
|
|
|
new (block) T(std::forward<Args>(args)...);
|
|
|
|
|
return unique_pool_ptr<T>(block, [&mempool](T* ptr) {
|
|
|
|
|
if (ptr != nullptr) {
|
|
|
|
|
ptr->~T();
|
|
|
|
|
mempool.deallocate(ptr);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace srsran
|
|
|
|
|
|
|
|
|
|