簡體   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