Nothing to do, to share the writing of an object pool for everyone

Source: Internet
Author: User

This object pool is quite small, supports locking to support thread safety, and of course, if used in a single thread, you can specify a pseudo-lock.

This object pool does not solve the memory fragmentation problem, it just uses space to change time. This code is quite brief, it is clear at a glance, so do not write the use case. And the code of the lock is not affixed, because the style of the lock is different, but also to avoid the topic, avoid distracting.

On the code:

Not enough 150 words are not allowed to publish to the homepage candidate area, good pit. That's enough for 150 words. Simple use case

int main ()

{

Objpool<int> _intpool;

int* p = _intpool.alloc ();

int* P2 = _intpool.alloc ();

_intpoll.dealloc (P);

int* p3 = _intpool.alloc ();

return 0;

}

In fact, this example is not good,

Enough for 150?

#ifndef objpool_include#defineObjpool_include#include"pool_config.hpp"#include"lock/lock.hpp"pool_namespace_begintypedef locklib::scopedlock scopedlock;template<classTclassLockmode=locklib::fakelock>classobjpool{ 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); }    }    intGetCount ()Const    {        return_numalloc; } T*Alloc () {T* ret =_alloc (); return New(ret) T (); } template<classP>T*alloc (Constp&p) {T* ret =_alloc (); return New(ret) T (p); } template<classP1,classP2>T*alloc (Constp1& P1,Constp2&p2) {T* ret =_alloc (); return New(ret) T (P1,P2); } template<classP1,classP2,classP3>T*alloc (Constp1& P1,Constp2& P2,Constp3&p3) {T* ret =_alloc (); return New(ret) T (P1,P2,P3); } template<classP1,classP2,classP3,classP4>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4&P4) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4); } template<classP1,classP2,classP3,classP4,classP5>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5&p5) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5); } template<classP1,classP2,classP3,classP4,classP5,classP6>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6&P6) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6); } template<classP1,classP2,classP3,classP4,classP5,classP6,classP7>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6& P6,Constp7&P7) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6,P7); } template<classP1,classP2,classP3,classP4,classP5,classP6,classP7,classP8>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6& P6,Constp7& P7,Constp8&P8) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6,P7,P8); } template<classP1,classP2,classP3,classP4,class55ωclassP6,classP7,classP8,classP9>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6& P6,Constp7& P7,Constp8& P8,Constp9&p9) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6,P7,P8,P9); } template<classP1,classP2,classP3,classP4,classP5,classP6,classP7,classP8,classP9,classP10>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6& P6,Constp7& P7,Constp8& P8,Constp9& P9,Constp10&P10) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6,P7,P8,P9,P10); } template<classP1,classP2,classP3,classP4,classP5,classP6,classP7,classP8,classP9,classP10,classP11>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6& P6,Constp7& P7,Constp8& P8,Constp9& P9,Constp10& P10,Constp11&P11) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11); } template<classP1,classP2,classP3,classP4,classP5,classP6,classP7,classP8,classP9,classP10,classP11,classP12>T*alloc (Constp1& P1,Constp2& P2,Constp3& P3,Constp4& P4,Constp5& P5,Constp6& P6,Constp7& P7,Constp8& P8,Constp9& P9,Constp10& P10,Constp11& P11,Constp12&p12) {T* ret =_alloc (); return New(ret) T (P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12); }    voidDealloc (t*elem) {ScopedlockLock(&_lock); Elem->~T (); memset (Elem,0xFE, _elemsize); _numalloc--; * (Reinterpret_cast<t**> (elem)) =_listhead; _listhead=Elem; }protected: T*_alloc () {ScopedlockLock(&_lock); T* ret =0; _numalloc++; if(_listhead = =NULL) {ret= (t*)malloc(_elemsize); }            Else{ret=_listhead; _listhead= * (reinterpret_cast<t**>(_listhead)); } memset (ret,0xFE, _elemsize); returnret; }protected:        int_numalloc;///< number of elements currently allocated through this classpoolsize_t _elemsize;///< The size of each element, or the size of a pointer, whichever is greaterT * _listhead;///< A pointer to a linked list of freed elements for reuseLockmode _lock;        }; Pool_namesapce_end#endif

Nothing to do, to share the writing of an object pool for everyone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.