程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 閒來無事,為大伙分享一個對象池的編寫,閒來無事

閒來無事,為大伙分享一個對象池的編寫,閒來無事

編輯:C++入門知識

閒來無事,為大伙分享一個對象池的編寫,閒來無事


這個對象池相當小巧,支持加鎖以方便支持線程安全,當然了,如果在單線程中使用,可以指定一個偽鎖。

這個對象池並不能解決內存碎片問題,只是用空間換時間。這個代碼相當簡短,一看就明白,所以不寫用例了。還有這個鎖的代碼就不貼了,因為鎖的樣式各有不同,還有避免跑題,避免喧賓奪主。

上代碼:

不夠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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved