闲来无事,为大伙分享一个对象池的编写

这个对象池相当小巧,支持加锁以方便支持线程安全,当然了,如果在单线程中使用,可以指定一个伪锁。

这个对象池并不能解决内存碎片问题,只是用空间换时间。这个代码相当简短,一看就明白,所以不写用例了。还有这个锁的代码就不贴了,因为锁的样式各有不同,还有避免跑题,避免喧宾夺主。

上代码:

不够150字不允许发布到首页候选区,好坑。那为够150字,那就来段简单用例

int main()

{

  objpool<int> _intpool;

  int* p = _intpool.alloc();

  int* p2 = _intpool.alloc();

  _intpoll.dealloc(p);

  int* p3 = _intpool.alloc();

  return 0; 

}

其实这个例子举的不好,

够150没?

#ifndef OBJPOOL_INCLUDE
#define OBJPOOL_INCLUDE

#include "pool_config.hpp"
#include "lock/lock.hpp"

POOL_NAMESPACE_BEGIN

typedef locklib::scopedlock scopedlock;

template<class T,class LockMode=locklib::fakelock>
class objpool
{
public:
    objpool()
    {
        _numalloc = 0;
        _elemsize = (sizeof(T)>sizeof(T*)) ? sizeof(T) : sizeof(T*);
        _listhead = NULL;
    }

    ~objpool()
    {
        while(_listhead)
        {
            T* ret = _listhead;
            _listhead = *(reinterpret_cast<T**>(_listhead));
            ::free(ret);
        }
    }

    int getCount()const
    {
        return _numalloc;
    }

    T *alloc()
    {
        T* ret = _alloc();
        return new(ret)T();
    }

    template<class P>
    T *alloc(const P& p)
    {
        T* ret = _alloc();
        return new(ret)T(p);
    }

    template<class P1, class P2>
    T *alloc(const P1& p1, const P2& p2)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2);
    }

    template<class P1, class P2, class P3>
    T *alloc(const P1& p1, const P2& p2, const P3& p3)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3);
    }

    template<class P1, class P2, class P3, class P4>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4);
    }

    template<class P1, class P2, class P3, class P4, class P5>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6, class P7>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6,p7);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6,p7,p8);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8, const P9& p9)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6,p7,p8,p9);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8, const P9& p9, const P10& p10)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8, const P9& p9, const P10& p10,const P11& p11)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11);
    }

    template<class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12>
    T *alloc(const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8, const P9& p9, const P10& p10,const P11& p11,const P12& p12)
    {
        T* ret = _alloc();
        return new(ret)T(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12);
    }

    void dealloc(T* elem)
    {
        scopedlock lock(&_lock);
        elem->~T();
        memset(elem, 0xfe, _elemsize);
        _numalloc--;
        *(reinterpret_cast<T**>(elem)) = _listhead;
        _listhead = elem;
    }

protected:
        T* _alloc()
        {
            scopedlock lock(&_lock);
            T* ret = 0;
            _numalloc++;
            if(_listhead == NULL)
            {
                ret = (T*)malloc(_elemsize);
            }
            else
            {
                ret = _listhead;
                _listhead = *(reinterpret_cast<T**>(_listhead));
            }
            memset(ret,0xfe,_elemsize);
            return ret;
        }

protected:
        int          _numalloc; ///< number of elements currently allocated through this ClassPool
        size_t    _elemsize; ///< the size of each element, or the size of a pointer, whichever is greater
        T *          _listhead; ///< a pointer to a linked list of freed elements for reuse
        LockMode  _lock;
        
};

POOL_NAMESAPCE_END
#endif
原文地址:https://www.cnblogs.com/openlib/p/5347312.html