void MyRecycle(T* ptr) { if (ptr == NULL) throw std::invalid_argument("the pointer can not be NULL!"); /*获取单例*/ CMemoryPool* myPool = CMemoryPool::getDefaultMemoryPool(); myPool->recyleMemory(ptr); }
///内存分配函数接口 void* MyAlloate(size_t size) { /*边界检测*/ ///不能一次性分配内存超过阙值 if (size >= MAX_MEMORY_REQUIRE) throw std::invalid_argument("out of memory threshold!"); /*获取单例*/ CMemoryPool* myPool = CMemoryPool::getDefaultMemoryPool(); return myPool->requireMemoryFromRecycle(size); }
///测试函数 void MemoryFactory(int num) { std::vector<Traii*> objList; CMemoryPool* myPool = CMemoryPool::getDefaultMemoryPool(); for (int i = 0; i < 10; i++) { Traii* myobj = (Traii*)myPool->requireMemoryFromRecycle(sizeof(Traii)); objList.push_back(myobj); } while (!objList.empty()) { Traii* objToRecycle = objList.back(); objList.pop_back(); myPool->recyleMemory(objToRecycle); } }
//----------------------------------------------------------------------------- // Purpose: allocates a KeyValues object from the shared mempool //----------------------------------------------------------------------------- void *CKeyValuesSystem::AllocKeyValuesMemory(int size) { // allocate, if we don't have one yet if (!m_pMemPool) { m_pMemPool = new CMemoryPool(m_iMaxKeyValuesSize, 1024); } return m_pMemPool->Alloc(size); }
void CViewport::EmptyDictionaryHash(void) { int i; hash_item_t *item; hash_item_t *temp; hash_item_t *free; for (i = 0; i < m_StringsHashTable.Count(); i++) { item = &m_StringsHashTable[i]; temp = item->next; item->dict = NULL; item->dictIndex = 0; item->lastHash = NULL; item->next = NULL; while (temp) { free = temp; temp = temp->next; m_HashItemMemPool.Free(free); } } }
void *CEventAction::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine ) { return g_EntityListPool.Alloc( stAllocateBlock ); }
void ConvertRainbowTable( std::string pathName, std::string resultFileName, std::string sType, bool debug, uint32 dropLastNchains, uint32 dropHighSPcount, int sptl ) { #ifdef _WIN32 std::string::size_type nIndex = pathName.find_last_of('\\'); #else std::string::size_type nIndex = pathName.find_last_of('/'); #endif std::string fileName; if ( nIndex != std::string::npos ) fileName = pathName.substr( nIndex + 1 ); else fileName = pathName; // Info printf("%s:\n", fileName.c_str()); FILE *fResult = fopen(resultFileName.c_str(), "wb"); if(fResult == NULL) { printf("Could not open %s for write access", resultFileName.c_str()); return; } static CMemoryPool mp; uint64 nAllocatedSize; BaseRTReader *reader = NULL; if(sType == "RTI2") reader = new RTI2Reader( pathName ); else if(sType == "RTI") reader = new RTIReader( pathName ); else { printf("Invalid table type '%s'", sType.c_str()); return ; } if ( debug ) reader->Dump(); uint64 size = reader->getChainsLeft() * sizeof(RainbowChainO); uint64 rainbowChainCount = reader->getChainsLeft(); uint64 chainsLeft; rainbowChainCount -= dropLastNchains; rainbowChainCount -= dropHighSPcount; size -= sizeof(RainbowChainO) * dropLastNchains; #ifdef _MEMORYDEBUG printf("Starting allocation of %i bytes\n", size); #endif RainbowChainO* pChain = (RainbowChainO*)mp.Allocate(size, nAllocatedSize); #ifdef _MEMORYDEBUG printf("Finished. Got %i bytes\n", nAllocatedSize); #endif if (pChain != NULL) { nAllocatedSize = nAllocatedSize / sizeof(RainbowChainO) * sizeof(RainbowChainO); // Round to boundary unsigned int nChains = nAllocatedSize / sizeof(RainbowChainO); while( ( chainsLeft = reader->getChainsLeft() ) > 0 && chainsLeft > dropLastNchains ) { #ifdef _MEMORYDEBUG printf("Grabbing %i chains from file\n", nChains); #endif reader->readChains(nChains, pChain); #ifdef _MEMORYDEBUG printf("Recieved %i chains from file\n", nChains); #endif for(uint32 i = 0; i < nChains; i++) { if ( dropHighSPcount > 0 && GetMaxBits(pChain[i].nIndexS) > sptl ) { //dropHighSPcount++; } else if ( dropLastNchains > 0 && dropLastNchains >= ( chainsLeft - i ) ) { } else fwrite(&pChain[i], 1, 16, fResult); } } } fclose(fResult); if(reader != NULL) delete reader; if ( dropHighSPcount > 0 ) { std::string::size_type lastX = resultFileName.find_last_of('x'); if ( lastX == std::string::npos ) { std::cout << "Could not parse the filename to drop the high SP chains" << std::endl; exit( -1 ); } std::string::size_type firstSplit = resultFileName.find_first_of('_',lastX); if ( firstSplit == std::string::npos ) { std::cout << "Could not parse the filename to drop the high SP chains" << std::endl; exit( -1 ); } std::string newResultFileName = resultFileName; newResultFileName.replace( lastX + 1, firstSplit - lastX - 1, uint64tostr( rainbowChainCount ) ); if ( rename( resultFileName.c_str(), newResultFileName.c_str() ) != 0 ) { std::cout << "Could not parse the filename to drop the high SP chains" << std::endl; exit( -1 ); } } }
/************************************************************************************* * 函数功能:将内存空间暂存到内存池中 * 参 数:[IN] void * pFree: 释放内存的首地址 ***************************************************************************************/ void CMemoryObject::operator delete[](void * pFree, unsigned int size) { memPool.Free(pFree); }
namespace fge { static CMemoryPool memPool; CMemoryObject::CMemoryObject(void) { } CMemoryObject::~CMemoryObject(void) { } /************************************************************************************* * 函数功能:从(基于可利用空闲表)的内存池中分配对应字节大小的内存空间 * 参 数:[IN] size_t size: 需要动态分配内存的大小 * 返 回 值: 如果分配成功,则返回内存空间的首地址;否则返回NULL *备 注:如果分配空间大于MALLOC_MAX_SIZE,则将从系统堆上直接分配内存 ***************************************************************************************/ void * CMemoryObject::operator new(unsigned int size) { return memPool.Allot(size); } /************************************************************************************* * 函数功能:从(基于可利用空闲表)的内存池中分配对应字节大小的内存空间 * 参 数:[IN] size_t size: 需要动态分配内存的大小 * 返 回 值:如果分配成功,则返回内存空间的首地址;否则返回NULL * 备 注:如果分配空间大于MALLOC_MAX_SIZE,则将从系统堆上直接分配内存 ***************************************************************************************/ void * CMemoryObject::operator new[](unsigned int size) { return memPool.Allot(size); } /************************************************************************************* * 函数功能:将内存空间暂存到内存池中, * 注 意:因为重载delete会检查参数是否为空,如果为空,则 * 自动返回,不执行函数的代码。所以我们不做指针检查 * 参 数:[IN] void * pFree: 释放内存的首地址 ***************************************************************************************/ void CMemoryObject::operator delete(void * pFree, unsigned int size) { memPool.Free(pFree); } /************************************************************************************* * 函数功能:将内存空间暂存到内存池中 * 参 数:[IN] void * pFree: 释放内存的首地址 ***************************************************************************************/ void CMemoryObject::operator delete[](void * pFree, unsigned int size) { memPool.Free(pFree); } //============================== CMemoryObjectThreadSafe::CMemoryObjectThreadSafe(void){} CMemoryObjectThreadSafe::~CMemoryObjectThreadSafe(void){} void * CMemoryObjectThreadSafe::operator new(unsigned int size) { return Safe_Allot(size); } void * CMemoryObjectThreadSafe::operator new[](unsigned int size) { return Safe_Allot(size); } void CMemoryObjectThreadSafe::operator delete(void * pFree, unsigned int size) { Safe_Free(pFree); } void CMemoryObjectThreadSafe::operator delete[](void * pFree, unsigned int size) { Safe_Free(pFree); } }
void *entitem_t::operator new( size_t stAllocateBlock ) { return g_EntListMemPool.Alloc( stAllocateBlock ); }
//----------------------------------------------------------------------------- // Purpose: memory allocation (for using mem pool) //----------------------------------------------------------------------------- void *CSendMessage::operator new(unsigned int size) { return g_MemPool.Alloc(size); }
static void* operator new(const size_t size) { return MPool.AllocMem(size); }
//----------------------------------------------------------------------------- // Purpose: frees a KeyValues object from the shared mempool //----------------------------------------------------------------------------- void CKeyValuesSystem::FreeKeyValuesMemory(void *pMem) { m_pMemPool->Free(pMem); }
void CViewport::AddDictionaryHash(CDictionary *dict, const char *value) { int count; hash_item_t *item; hash_item_t *next; hash_item_t *temp; hash_item_t *newp; unsigned int hash = 0; int dictIndex; CDictionary *dictTemp; if (!dict->m_szTitle[0]) return; count = m_StringsHashTable.Count(); hash = CaseInsensitiveHash(value, count); dictIndex = dict - m_Dictionary[0]; item = &m_StringsHashTable[hash]; while (item->dict) { if (!Q_strcmp(item->dict->m_szTitle, dict->m_szTitle)) break; hash = (hash + 1) % count; item = &m_StringsHashTable[hash]; } if (item->dict) { next = item->next; while (next) { if (item->dict == dict) break; if (item->dictIndex >= dictIndex) break; item = next; next = next->next; } if (dictIndex < item->dictIndex) { dictTemp = item->dict; item->dict = dict; item->lastHash = NULL; item->dictIndex = dictIndex; dictIndex = dictTemp - m_Dictionary[0]; } else dictTemp = dict; if (item->dict != dictTemp) { temp = item->next; newp = (hash_item_t *)m_HashItemMemPool.Alloc(sizeof(hash_item_t)); item->next = newp; newp->dict = dictTemp; newp->lastHash = NULL; newp->dictIndex = dictIndex; if (temp) newp->next = temp; else newp->next = NULL; } } else { item->dict = dict; item->lastHash = NULL; item->dictIndex = dict - m_Dictionary[0]; } }
void *CEventAction::operator new( size_t stAllocateBlock ) { return g_EntityListPool.Alloc( stAllocateBlock ); }
void entitem_t::operator delete( void *pMem ) { g_EntListMemPool.Free( pMem ); }
void *entitem_t::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine ) { return g_EntListMemPool.Alloc( stAllocateBlock ); }
void CViewport::RemoveDictionaryHash(CDictionary *dict, const char *value) { int hash = 0; hash_item_t *item; hash_item_t *last; int dictIndex; int count; count = m_StringsHashTable.Count(); hash = CaseInsensitiveHash(value, count); dictIndex = dict - m_Dictionary[0]; hash = hash % count; item = &m_StringsHashTable[hash]; while (item->dict) { if (!Q_strcmp(item->dict->m_szTitle, dict->m_szTitle)) break; hash = (hash + 1) % count; item = &m_StringsHashTable[hash]; } if (item->dict) { last = item; while (item->next) { if (item->dict == dict) break; last = item; item = item->next; } if (item->dict == dict) { if (last == item) { if (item->next) { item->dict = item->next->dict; item->dictIndex = item->next->dictIndex; item->lastHash = NULL; item->next = item->next->next; } else { item->dict = NULL; item->lastHash = NULL; item->dictIndex = 0; } } else { if (m_StringsHashTable[hash].lastHash == item) m_StringsHashTable[hash].lastHash = NULL; last->next = item->next; m_HashItemMemPool.Free(item); } } } }
void CEventAction::operator delete( void *pMem ) { g_EntityListPool.Free( pMem ); }
//----------------------------------------------------------------------------- // Purpose: allocates memory from the entitylist pool //----------------------------------------------------------------------------- void *CMultiInputVar::inputitem_t::operator new( size_t stAllocateBlock ) { return g_EntityListPool.Alloc( stAllocateBlock ); }
void *CMultiInputVar::inputitem_t::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine ) { return g_EntityListPool.Alloc( stAllocateBlock ); }
//----------------------------------------------------------------------------- // Purpose: memory allocation (for using mem pool) //----------------------------------------------------------------------------- void CSendMessage::operator delete(void *pMem) { g_MemPool.Free(pMem); }
//----------------------------------------------------------------------------- // Purpose: frees memory from the entitylist pool //----------------------------------------------------------------------------- void CMultiInputVar::inputitem_t::operator delete( void *pMem ) { g_EntityListPool.Free( pMem ); }
static void operator delete(void* pItem) { MPool.FreeMem(pItem); }
/************************************************************************************* * 函数功能:从(基于可利用空闲表)的内存池中分配对应字节大小的内存空间 * 参 数:[IN] size_t size: 需要动态分配内存的大小 * 返 回 值:如果分配成功,则返回内存空间的首地址;否则返回NULL * 备 注:如果分配空间大于MALLOC_MAX_SIZE,则将从系统堆上直接分配内存 ***************************************************************************************/ void * CMemoryObject::operator new[](unsigned int size) { return memPool.Allot(size); }