哪位高手有内存池的完整代码
谁有内存池的完整代码?
最好c语言写的啊,我要参考一下,谢谢,呵呵
------解决方案--------------------
http://www.codeproject.com/Articles/15527/C-Memory-Pool
------解决方案--------------------
ace ;there have all you want ;C++
------解决方案--------------------
memory pool, thread pool;
------解决方案--------------------
http://blog.****.net/rrrfff/article/details/7228704
这个相对简单一点
------解决方案--------------------
给个简单的:
ccBufferManager.h
最好c语言写的啊,我要参考一下,谢谢,呵呵
------解决方案--------------------
http://www.codeproject.com/Articles/15527/C-Memory-Pool
------解决方案--------------------
ace ;there have all you want ;C++
------解决方案--------------------
memory pool, thread pool;
------解决方案--------------------
http://blog.****.net/rrrfff/article/details/7228704
这个相对简单一点
------解决方案--------------------
给个简单的:
ccBufferManager.h
- C/C++ code
/* Written by cc_team */ #ifndef CC_BUFFER_MGR_H #define CC_BUFFER_MGR_H //#include "ccCommon.h" //#include "ccVector.h" #include <vector> #include <iostream> using namespace std; typedef struct _BufInfo { void * buf; int size; }BufInfo; class CCBufferMgr { public: CCBufferMgr(int size = /*PAGE_SIZE*/ 4096):size(size) { buf = new char[size]; BufInfo bufInfo = {buf, size}; unusedBufVec.push_back(bufInfo); // usedBufVec.setReallocSizeUnit(1024); // unusedBufVec.setReallocSizeUnit(1024); } ~CCBufferMgr() { delete []buf; } public: int getSize() const { return size; } void * getBufPtr() const { return buf; } public: void * askOneBuffer(int bufSize); // if no avaiable buf, return NULL bool returnBackBuffer(void * buf); // if return false, the buf isn't allocated from the mgr, it will do nothing public: void show() const; public: void setReallocSizeUnit(int newReallocSizeUnit); private: void askTwoBuffer( void ** bufOne, int sizeOne, void ** bufTwo , int sizeTwo); private: CCBufferMgr(const CCBufferMgr & bufMgr); CCBufferMgr & operator=(const CCBufferMgr & bufMgr); private: void * buf; int size; //CCVector<BufInfo> usedBufVec; //CCVector<BufInfo> unusedBufVec; vector<BufInfo> usedBufVec; vector<BufInfo> unusedBufVec; }; template<class T> class BaseAlloc { public: static void* operator new(size_t size) { void* pRet = NULL; if(!m_freelist) { m_freelist = ::new Node; m_freelist->next = NULL; for(int i = 0;i < INCREMENT - 1;++i) { Node* p = ::new Node; p->next = m_freelist; m_freelist = p; } m_total += INCREMENT; } ++m_alloc; pRet = reinterpret_cast<void*>(m_freelist->data); m_freelist = m_freelist->next; return pRet; } static void operator delete(void* pr,size_t size) { Node* p = reinterpret_cast<Node*>(pr); p->next = m_freelist; m_freelist = p; if(m_total - --m_alloc > DIFFSIZE) { for(int i = 0;i < RELEASESIZE;++i) { Node* p = m_freelist; m_freelist = m_freelist->next; ::delete p; } m_total -= RELEASESIZE; cout << "Release One Time" << endl; } } private: struct Node { char data[sizeof(T)]; Node* next; }; static Node* m_freelist; static int m_alloc; static int m_total; enum { INCREMENT = 64, DIFFSIZE = 1024, RELEASESIZE = 512, }; }; template<class T> typename BaseAlloc<T>::Node* BaseAlloc<T>::m_freelist = NULL; template<class T> int BaseAlloc<T>::m_alloc = 0; template<class T> int BaseAlloc<T>::m_total = 0; #endif
------解决方案--------------------
ccBufferManager.cpp