C + + Object pool usage

Source: Internet
Author: User

////ObjectPool.h//Dragonball////Created by the user on 13-8-22.////#include<iostream>#include<vector>#include"cocos2d.h"#include<algorithm>using namespacestd;using namespacecocos2d;template<typename t>classobjectpool{ Public: Objectpool (intinitsize): M_initsize (initsize) {Ccassert (m_initsize>=4,"M_initsize should >= 4");    Allocate (initsize);            }; Virtual~Objectpool () {For_each (M_usedlist.begin (), M_usedlist.end (), freeobject);    For_each (M_freelist.begin (), M_freelist.end (), freeobject); }        //request an Objectt*request () {//Check to see if it is empty in the idle        if(m_freelist.size () = =0) {Allocate (M_initsize/2); } T* t = m_freelist[0];        M_freelist.erase (M_freelist.begin ());        M_usedlist.push_back (t); returnT;            }; //put back an object inside the pool    voidPutback (t*t) { for(inti =0; I < m_usedlist.size (); ++i) {if(T = =M_usedlist[i]) {                //find this objectM_usedlist.erase (M_usedlist.begin () +i); M_freelist.push_back (t);return; }        }    }            //in order to avoid the growing memory, you need to keep the space    voidgarbage () {//Print under SizeCclog ("m_usedlist.size:%d", M_usedlist.size ()); Cclog ("m_freelist.size:%d", M_freelist.size ()); //If extra 20 is free, delete to the number of initializations        if(M_freelist.size () >m_initsize) {TypeName Vector<t*>::iterator it =M_freelist.begin (); intPindex =0;  while(It! =M_freelist.end ()) {                                if(Pindex >m_initsize) {T* t = (*it);                    M_freelist.erase (IT);                    Freeobject (t); --it; }                                ++Pindex; ++it; }        }    }    Private:        //allocate a size space    voidAllocateintsize) {         for(inti =0; i < size; ++i) {T* t =NewT;        M_freelist.push_back (t); }    }        Static voidFreeobject (void*p) {T* t = (t*) p; Ref* Temp = (ref*) T; Log ("Temp.ref:%d",temp->Getreferencecount ()); DeleteT; }Private:            //use two list one to put in use, one to put unusedVector<t*>m_freelist; Vector<T*>m_usedlist; //Initial capacity    intm_initsize; };

Instantiate first when used

New Objectpool<t>(size);   Auto PRet;    if (objectpool->request ()) {       = objectpool->request ();   }    Else {      new  T ();   } PRet,Remove (); Objectpool->putback (T);

C + + Object pool usage

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.