void* IMemory::ReAlloc(void* mem,uint len){ LCS lcs(_cs); if(_handle==NULL) return 0; if(len%2) len += 1; uint preLen = GetLength(mem); if(preLen==len) return mem; if(mem){ char* pt = _getPointer((char*)mem); int size = _getPointerSize(pt); char* next = _getNextPointer(pt); if(next==0) next = (char*)_handle + _length; int freeLen = next - pt - size - HANDLE_SIZE*3; if((int)len<=freeLen){ _setPointer(pt,len); return mem; } Free(mem); } void* p = Alloc(len); if(p==0){//空间不足 return 0; } if(p!=mem&&mem!=0){ uint clen = len<preLen?len:preLen; memcpy(p,mem,clen); } return p; }
//这个函数无法判定mem的有效性, 而且如果mem不正确, 可能会破坏整个内存对象. 访问释放的内存并不会引起错误. bool IMemory::Free(void* mem){ if(mem==0) return 0; LCS lcs(_cs); if(_handle==NULL) return 0; char* pt = _getPointer((char*)mem); #ifdef _DEBUG if(!_findPointer((char*)pt,_curLarge)){ CONASSERT(L"IMemory.Free:pointer not find"); return 0; } #endif uint size = _getPointerSize(pt); char* pre = _getPrePointer(pt); char* after = _getNextPointer(pt); if(pre){ _setPointerAfter(pre,after); }else{ pre = (char*)_handle; _setPointer(pre,0,after,-HANDLE_SIZE*3); } if(after){ _setPointerPre(after,pre); } if(size>=large_small_divider){ _curLarge = pre; if((uint)(UINT_PTR)_curLarge<(uint)(UINT_PTR)_curSmall){ _curSmall = _curLarge; } }else{ _curSmall = pre; } return true; }
void PointerContextManager::updateCursor() { if (isHighLevelContext()) { _setPointer("default"); } else { if (mCurrentGuiPointer != MyGUI::PointerManager::getInstance().getDefaultPointer()) { mPointerSetter->setPointer(mCurrentGuiPointer); } else { _setPointer(mCurrentPointer); } } }
char* IMemory::_allocIn(char* pt,uint len){ char* next = _getNextPointer(pt); uint size = _getPointerSize(pt); int freelen; if(next==0){ freelen = (char*)_handle + _length - pt - size - mem_head_size; }else{ freelen = next - pt - size - mem_head_size; } if((int)len>freelen) return 0; char* p = pt + size; if(p==_handle) pt = 0; _setPointer(p,pt,next,len); if(pt) _setPointerAfter(pt,p); if(next) _setPointerPre(next,p); return p; }
bool ExecMemory::Create(int size){ LCS lcs(_cs); if(_handle!=0) return false; _map = ::CreateFileMapping(INVALID_HANDLE_VALUE,0,PAGE_EXECUTE_READWRITE|SEC_COMMIT,0,size,0); if(_map==0){ return false; } _handle = MapViewOfFile(_map,FILE_MAP_EXECUTE|FILE_MAP_ALL_ACCESS,0,0,size); if(_handle==NULL){ CloseHandle(_map); _map = NULL; return false; } _curLarge = (char*)_handle; _curSmall = _curLarge; _setPointer(_curLarge,0,0,-HANDLE_SIZE*3); _length = size; return 1; }