# ipst_ # define _MEM_POOL_TEST _ / ******************************************************* ******************************** Created: 2004/11/29 create: 29: 11: 2004 15:27 FileName: E: / Reserved / Memory Pool / Test / CMemPooltest.h File Path: E: / Memory Pool / Test File Base: CMemPooltest File Ext: H Author: David Purpose: This is a class that tests the memory pool class, which is mainly used. : 1.cmempool - cMemPool.h is mainly used for the Template of the Allocate and Release, which is suitable for the size of each node. 2. Clock - Lock.h is mainly used inside CMemPool, synchronized CMemPool internal InUse Links and Free Linches. 3. CMemPooltest CBuffer - The class used to test CMemPool. A CMEMPoolTest instance simulates a network session because the class instance may be generated and released frequently, so you need to use the memory pool, and each instance manages your own buffer list, and the buffer list may be frequently generated and released. Therefore, the memory pool is also adopted so that the memory pool nested phenomenon can occur. These two classes are used to simulate CMEMPOOL nested, and the test results are good. 4. Note: Use class cmempool class must reload new and delete, from the memory pool allocate memory in New, and return it to the memory pool in delete. The memory pool should belong to the asset of the class, not the asset of the object, so the Static type member. 5. Recommendation: For frequently generated in the same type of object, it is recommended to use the memory pool so that memory debris can be prevented from being stable for the program. *********************************************************** ****************** /
#include "cmempool.h" #include "global.h" #include
class CBuffer {public: CBuffer (); virtual ~ CBuffer (); static void * operator new (size_t); static void operator delete (void *); private: int m_nBufLen; char m_pBuffer [2048]; static CMemPool
Class cmempooltest {public: cmempooltest (); virtual ~ cmempooltest ();
Static void * operator new (size_t); static void operator delete (void *);
Void setBuffer (); private:
Static CMempool
List
#ndif //. cpp # include "cmempooltest.h" CBuffer :: cbuffer () {}
CBuffer :: ~ cbuffer () {cout << "~ cbuffer << Endl;} void * cbuffer :: operator new (size_t) {cbuffer * p = null; p = static_cast
void CBuffer :: operator delete (void * deadobject) {m_MemPool.Release ((CBuffer *) deadobject); int m_nInuse, m_nFree; m_MemPool.GetInfo (m_nInuse, m_nFree); cout << "CBuffer Release m_nInuse is" << m_nInuse < <"m_nfree IS" << m_nfree << endl;}
CMemPooltest :: cmempooltest () {}
CMemPooltest :: ~ cmempooltest () {list
void * CMemPoolTest :: operator new (size_t) {CMemPoolTest * p = NULL; = p static_cast
void CMemPoolTest :: operator delete (void * deadobject) {m_MemPool.Release ((CMemPoolTest *) deadobject); / * int m_nInuse, m_nFree; m_MemPool.GetInfo (m_nInuse, m_nFree); cout << "Release m_nInuse is" << m_nInuse << "m_nfree is" << m_nfree << endl; * /}
Void CMempooltest :: setBuffer () {CBuffer * P = NULL; P = new cBuffer ();
m_bufferlist.push_back (p);}
// main () # include "global.h" #include #include "CMemPoolTest.h" #include "UnfixedMem_Pool.h" CMemPool
INT main () {// test cmempool int = 0; cmempooltest * test [1000]; for (i = 0; i <1000; i ) {test [i] = new cmempooltest; test [i] -> setBuffer ); Test [i] -> setBuffer ();
For (i = 0; i <1000; i ) {if (TEST [I]! = null) {delete test [i]; test [i] = null;}} // end test cmempool
Return Success_code;}