繁体   English   中英

将内存池分配器耦合到托管已分配实例的各种内存池

[英]Coupling memory pool allocator to various memory pools hosting the allocated instances

有一个无状态内存池分配器类:

template<typename T>
class pool_allocator {
public:
    using value_type = T;
    using pointer = value_type *;

    /* Default constructor */
    constexpr pool_allocator( void ) noexcept = default;

    /* Converting constructor used for rebinding */
    template<typename U>
    constexpr pool_allocator( const pool_allocator<U> & ) noexcept {}

    [[nodiscard]] pointer allocate( size_t n, [[maybe_unused]] const pointer hint = nullptr ) const noexcept {
        return get_pool().allocate( n );
    }

    void deallocate( pointer ptr, size_t n ) const noexcept {
        get_pool().deallocate( ptr, n );
    }

private:
    /* Must be defined in particular .cpp files */
    /* POINT OF INTERREST HERE: */
    static auto & get_pool( void ) noexcept;
};

背后的逻辑是get_pool()成员函数的特殊化,它旨在返回定义类型的特定内存池,其中应分配 T 的实例,例如:

class sample { ... };

在 .cpp 文件中:

memory_pool<sample, 10> sample_storage;  // memory pool capable of holding up to 10 instances of 'sample'

最后是 .cpp 文件中 get_pool() 函数模板的特化:

template<>
auto & pool_allocator<sample>::get_pool( void ) noexcept {
    return sample_storage; // return the memory_pool instance defined above
}

问题是这种模板特化仅在 .cpp 编译单元中可用,并防止在其他编译单元中使用auto get_pool() (不能推断auto占位符的类型,因为get_pool()函数模板特化的主体不可用)

因此,我想以某种方式摆脱auto作为get_pool()返回类型。

我面临的问题主要是内存memory_pool的大小,这是分配器本身未知的。 无论如何,memory_pool 也是我的实现,所以我可以进行任何需要的采用(例如进一步using声明或其他任何需要)。 只是它的骨架:

template<typename T, size_t CAPACITY>
class memory_pool {
public:
    using element_type = T;
    using pointer = element_type *;

    constexpr size_t capacity( void ) noexcept {
        return CAPACITY;
    }
...
};

这是我使用的解决方案 - 实现包含有关池大小信息的特征类:

template<typename T>
class memory_pool {
public:
    using traits = memory_pool_traits<T>;
    using element_type = typename traits::element_type;
    using pointer = element_type *;

    static constexpr size_t capacity { traits::capacity };
...
};

分配器:

template<typename T>
class pool_allocator {
public:
    using value_type = T;
    using pointer = value_type *;

    /* Default constructor */
    constexpr pool_allocator( void ) noexcept = default;

    /* Converting constructor used for rebinding */
    template<typename U>
    constexpr pool_allocator( const pool_allocator<U> & ) noexcept {}

    [[nodiscard]] pointer allocate( size_t n, [[maybe_unused]] const pointer hint = nullptr ) const noexcept {
        return get_pool().allocate( n );
    }

    void deallocate( pointer ptr, size_t n ) const noexcept {
        get_pool().deallocate( ptr, n );
    }

private:
    static memory_pool<T> & get_pool( void ) noexcept;
};

对于任何特定类型,都应该有特性类:

// Primary template
template<typename T> struct memory_pool_traits;

让我想定义memory_pool

class sample { ... };

...除此之外,设置 - 特征 - 用于相应的memory_pool

template<>
struct memory_pool_traits<sample> {
    using element_type = sample;
    static constexpr size_t capacity { 10 };
};

.cpp文件中有池本身的定义和get_pool()函数:

memory_pool<sample> sample_storage;

template<>
memory_pool<sample> & pool_allocator<sample>::get_pool( void ) noexcept {
    return sample_storage;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM