C++11 带有Boost池分配器的自定义线程本地STL分配器

C++11 带有Boost池分配器的自定义线程本地STL分配器,c++11,memory-management,boost,dynamic-allocation,allocator,C++11,Memory Management,Boost,Dynamic Allocation,Allocator,我们有一个庞大的遗留代码库,它是多线程的,并且广泛使用向量。为了减少在动态内存分配上花费的时间,我们正在迁移到一个池。计划是使用boostsmall-vector和一个自定义分配器。自定义分配器将为每个容器类型创建一个线程本地池。我基于上述思想实现了一个自定义分配器,并对其进行了测试。出于某种原因,代码在Boost简单隔离存储中的find_prev方法中以无限模式出现。很多地方都有容器嵌套,比如vector>>等等。这是定义分配器的正确方法吗 template<typename T, ty

我们有一个庞大的遗留代码库,它是多线程的,并且广泛使用向量。为了减少在动态内存分配上花费的时间,我们正在迁移到一个池。计划是使用boostsmall-vector和一个自定义分配器。自定义分配器将为每个容器类型创建一个线程本地池。我基于上述思想实现了一个自定义分配器,并对其进行了测试。出于某种原因,代码在Boost简单隔离存储中的find_prev方法中以无限模式出现。很多地方都有容器嵌套,比如vector>>等等。这是定义分配器的正确方法吗

template<typename T, typename allocatorType>
class customAllocator
{
    public:
        static thread_local allocatorType *_allocator;
        typedef T value_type;
        typedef allocatorType allocator_Type;
        template <class X> struct rebind
        {
            typedef customAllocator<X, allocatorType> other;
        };
        customAllocator()
        {
            _allocator = new allocatorType;
            assert(_allocator);
            return;
        }
        ~customAllocator()
        {
            delete _allocator;
            _allocator = nullptr;
            return;
        }
        template<class X, class Y> customAllocator(const customAllocator<X, Y>& other)
        {
            _allocator = other._allocator;
            return;
        }
        template<class X, class Y> customAllocator(customAllocator<X, Y>&& other)
        {
            _allocator = other._allocator;
            other._allocator = nullptr;
            return;
        }
        template<class X, class Y> customAllocator& operator=(const customAllocator<X, Y>& other)
        {
            _allocator = other._allocator;
            return *this;
        }
        template<class X, class Y> customAllocator& operator=(customAllocator<X, Y>&& other)
        {
            _allocator = other._allocator;
            other._allocator = nullptr;
            return *this;
        }
        T* allocate(size_t n)
        {
            return _allocator->allocate(n * sizeof(T));
        }
        void deallocate(T* ptr, size_t n)
        {
            _allocator->deallocate(ptr, n);
            return;
        }
        template<class X, class Y> bool operator==(const customAllocator<X, Y>& other) const noexcept
        { return (*this._allocator == other.allocator);  }
        template<class X, class Y> bool operator!=(const customAllocator<X, Y>& other) const noexcept
        { return !(*this._allocator == other._allocator); }
};
template <typename T1, typename T2>
thread_local T2 *customAllocator<T1, T2>::_allocator = nullptr;

using smallVector = boost::container::small_vector<
T,
    DEFAULT_SMALL_VECTOR_LENGTH,
    customAllocator<T,
    boost::pool_allocator<
    T,
    boost::default_user_allocator_new_delete,
    boost::details::pool::null_mutex,
    2,
    4
    >>>;
模板
类自定义分配器
{
公众:
静态线程\本地分配器类型*\分配器;
类型定义T值_类型;
类型定义分配器类型分配器\u类型;
模板结构重新绑定
{
typedef自定义分配器其他;
};
customAllocator()
{
_分配器=新的分配器类型;
断言(_分配器);
返回;
}
~customAllocator()
{
删除分配程序;
_分配器=nullptr;
返回;
}
模板customAllocator(常量customAllocator和其他)
{
_分配器=其他分配器;
返回;
}
模板customAllocator(customAllocator&&other)
{
_分配器=其他分配器;
其他。_分配器=nullptr;
返回;
}
模板customAllocator和运算符=(常量customAllocator和其他)
{
_分配器=其他分配器;
归还*这个;
}
模板customAllocator&运算符=(customAllocator&其他)
{
_分配器=其他分配器;
其他。_分配器=nullptr;
归还*这个;
}
分配(大小)
{
返回_分配器->分配(n*sizeof(T));
}
无效解除分配(T*ptr,大小\u T n)
{
_分配器->解除分配(ptr,n);
返回;
}
模板布尔运算符==(常量customAllocator和其他)常量noexcept
{return(*this.\u分配器==其他.allocator);}
模板布尔运算符!=(常量customAllocator和其他)常量noexcept
{return!(*this.\u分配器==other.\u分配器);}
};
模板
线程\u local T2*customAllocator::\u allocator=nullptr;
使用smallVector=boost::container::small\u vector<
T
默认的小向量长度,
自定义分配器>>;

您是否首先对内存分配确实是一个瓶颈进行了基准测试?另外,我不认为池分配器适合
std::vector
(但不确定
small\u vector