Int64 HawkMemoryFile::Write(const void* pData, Int64 iSize, Bool bFlush) { if (!pData || iSize <= 0 || (m_eOpenType != OPEN_WRITE && m_eOpenType != OPEN_RW && m_eOpenType != OPEN_APPEND) ) return 0; if (m_iFilePos + iSize > m_iFileSize) { Int64 iTmp = m_iFileSize + iSize; void* pTmp = HawkMalloc((Size_t)iTmp); memset(pTmp, 0, (Size_t)iTmp); if (m_pData) { memcpy(pTmp, m_pData, (Size_t)m_iFileSize); HawkFree(m_pData); m_pData = 0; } m_pData = pTmp; m_iFileSize = m_iFilePos + iSize; } memcpy((Char*)m_pData+m_iFilePos, pData, (Size_t)iSize); m_iFilePos += iSize; return iSize; }
Bool HawkMemoryFile::Chsize(Int64 iSize) { if((m_eOpenType == OPEN_WRITE || m_eOpenType == OPEN_RW || m_eOpenType == OPEN_APPEND)) { void* pData = HawkMalloc((Size_t)iSize); if (pData) { memset(pData, 0, (Size_t)iSize); if(m_pData) { Int64 iTmp = HawkMath::Min<Int64>(iSize, m_iFileSize); memcpy(pData, m_pData, (Size_t)iTmp); HawkFree(m_pData); m_pData = 0; } } m_iFileSize = iSize; m_iFilePos = 0; m_pData = pData; return true; } return false; }
HawkLogServer::HawkLogServer() { m_pLogDB = 0; m_pLogFile = 0; m_pLogCache = 0; m_iCacheTime = 0; m_bConsole = true; m_bRunning = false; m_pFmtBuf = (Utf8*)HawkMalloc(PAGE_SIZE); m_pRecvBuf = new OctetsStream(PAGE_SIZE); }
Int64 HawkMemoryFile::Seek(Int64 iOffset, SeekPos ePos) { Int64 iRealPos = m_iFilePos; Int64 iOldPos = m_iFilePos; if (ePos == POS_BEGIN) { iRealPos = iOffset; } else if (ePos == POS_CURRENT) { iRealPos = m_iFilePos + iOffset; } else { iRealPos = m_iFileSize + iOffset; } if(iRealPos > m_iFileSize) { if((m_eOpenType == OPEN_WRITE || m_eOpenType == OPEN_RW || m_eOpenType == OPEN_APPEND)) { void* pData = HawkMalloc((Size_t)iRealPos); if (pData) { memset(pData, 0, (Size_t)iRealPos); if(m_pData) { memcpy(pData, m_pData, (Size_t)m_iFileSize); HawkFree(m_pData); m_pData = 0; } } m_iFileSize = iRealPos; m_iFilePos = iRealPos; m_pData = pData; } else { m_iFilePos = m_iFileSize; } } else { m_iFilePos = iRealPos; } return m_iFilePos - iOldPos; }
HawkSpinLock::HawkSpinLock() : m_pSpinLock(0) { m_pSpinLock = HawkMalloc(sizeof(pthread_spinlock_t)); if (pthread_spin_init((pthread_spinlock_t*)m_pSpinLock, 0) != HAWK_OK) { HawkFree(m_pSpinLock); m_pSpinLock = 0; } #ifdef _DEBUG m_bLocked = false; m_sFile = ""; m_iLine = 0; m_iThread = 0; #endif }
Bool HawkMemoryFile::Open(Int64 iSize, OpenType eOpen) { Close(); m_eOpenType = eOpen; m_iFileSize = iSize; m_bExtra = false; if(iSize > 0) { m_pData = (Char*)HawkMalloc((Size_t)m_iFileSize); memset(m_pData, 0, (Size_t)m_iFileSize); } return true; }
Bool HawkMemoryFile::Open(const AString& sFile) { Close(); m_eOpenType = OPEN_READ; m_bExtra = false; HawkDiskFile df; if (df.Open(sFile)) { m_iFileSize = df.GetFileSize(); m_pData = (Char*)HawkMalloc((Size_t)m_iFileSize); memset(m_pData, 0, (Size_t)m_iFileSize); df.Read(m_pData, m_iFileSize); return true; } return false; }
Bool HawkMemoryFile::Open(void* pData, Int64 iSize, Bool bExtra, OpenType eOpen) { Close(); m_eOpenType = eOpen; m_iFileSize = iSize; m_bExtra = bExtra; if (!m_bExtra) { m_pData = (Char*)HawkMalloc((Size_t)m_iFileSize); memcpy(m_pData, pData, (Size_t)m_iFileSize); } else { m_pData = pData; } return true; }
HawkGateThread::Session* HawkGateThread::AllocSession(SOCKET hSocket, const SocketAddr& sAddr) { if (hSocket != INVALID_SOCKET) { Session* pSession = (Session*)HawkMalloc(sizeof(Session)); memset(pSession, 0, sizeof(Session)); pSession->GThread = this; pSession->Socket = hSocket; pSession->Sid = GenSessionId(); pSession->Addr = new SocketAddr(sAddr); pSession->IBuffer = new OctetsStream(m_pGateway->GetBufSize()); HawkSocket::SetBlocking(pSession->Socket, false); m_pGateway->CreateISecurity(pSession->ISecurity); m_pGateway->CreateOSecurity(pSession->OSecurity); return pSession; } return 0; }
Bool HawkThread::Start(void* pArgs) { if(!m_bRunning && !m_pThread) { m_pArgs = pArgs; m_bRunning = true; m_pThread = HawkMalloc(sizeof(pthread_t)); if (pthread_create((pthread_t*)m_pThread, 0, hawk_ThreadRoutine, this) != HAWK_OK) { m_bRunning = false; m_iState = STATE_CLOSED; return false; } while (m_iState != STATE_RUNNING) { HawkSleep(DEFAULT_SLEEP); } return true; } return false; }
AString HawkSqlite::AmendString(const AString& sValue) { Int32 iSize = sValue.size()*2 + 1; Char* pBuf = (Char*)HawkMalloc(iSize * sizeof(Char)); memset(pBuf,0,iSize * sizeof(Char)); HawkScope::MallocPtr scope(pBuf); Char* pPos = pBuf; for (Size_t i=0;i<sValue.size();i++) { if (is_trans_char(sValue[i])) { if(sValue[i] == '\'') { *pPos++ = '\''; *pPos++ = '\''; //跳过后续点 if (i < sValue.size()-1 && sValue[i+1] == '\'') i += 1; } else { if (i > 0 && sValue[i-1] != '/') *pPos++ = '/'; *pPos++ = sValue[i]; } } else { *pPos++ = sValue[i]; } } return pBuf; }
void* HawkRefCounter::operator new(Size_t iSize) { return HawkMalloc(iSize); }