///////////////////////////q-4 void listtest(){ Position pHead; pHead=NULL; AddToTail(&pHead,4); AddToTail(&pHead,5); RemoveNode(&pHead,4); RemoveNode(&pHead,5); AddToTail(&pHead,5); }
/** * @author ACM2012 * @param [in] pReadQueue 读队列控制块结构, ReadQueue 读队列控制块句柄,SockMark 套接字标志 * @note 函数的主要功能是从应用程序写队列中读取数据并传送到传输层。函数提取 */ void SockPool::WriteSock(HANDLE CH, unsigned int SockMark, HANDLE WriteQueue, PM pWriteQueue,HANDLE &SDestory) { bool closeflag = false; int readnum; while (SockMark2WriteState[SockMark]){ HANDLE NewNode = CreateFileMapping(HANDLE(0xFFFFFFFF), NULL, PAGE_READWRITE, 0, sizeof(Node), NULL); PN pNode = (PN)MapViewOfFile(NewNode, FILE_MAP_WRITE, 0, 0, sizeof(Node)); WaitForSingleObject(*SockMark2REvent[SockMark], INFINITE);///<等待传输层数据到来 ClearNode(pWriteQueue); if (!SockDataToNode(pNode, SockMark)) break; UnmapViewOfFile(pNode); SockMark2WEvent[SockMark]->SetEvent(); AddToTail(pWriteQueue,NewNode);///<添加数据到写队列 CloseHandle(NewNode); } ClearNode(pWriteQueue);///<释放写队列资源 CloseHandle(pWriteQueue->Head); CloseHandle(pWriteQueue->Tail); CloseHandle(pWriteQueue->Cur); UnmapViewOfFile(pWriteQueue); CloseHandle(WriteQueue); SockMark2WriteState.erase(SockMark); SockMark2WEvent.erase(SockMark); if (SockMark2State[SockMark]) CloseSock(SockMark); else SockMark2State[SockMark] = true; }
/** * @param eLogLevel - log level number. * @param eEntryMode - entry mode. * @param rcsConsoleAccess - provides synchronous access to the console. * @param pszEntry - log entry text. * @return true if operation was completed successfully. */ BOOL CTextLogFile::WriteLogEntry(BUGTRAP_LOGLEVEL eLogLevel, ENTRY_MODE eEntryMode, CRITICAL_SECTION& rcsConsoleAccess, PCTSTR pszEntry) { BOOL bResult = TRUE; BUGTRAP_LOGLEVEL eLogFileLevel = GetLogLevel(); if (eLogLevel <= eLogFileLevel) { SYSTEMTIME st; GetLocalTime(&st); if (! WriteLogEntryToConsole(eLogLevel, &st, rcsConsoleAccess, pszEntry)) FillEntryText(eLogLevel, &st, pszEntry); EncodeEntryText(); switch (eEntryMode) { case EM_APPEND: AddToTail(FALSE); break; case EM_INSERT: AddToHead(FALSE); break; default: _ASSERT(FALSE); bResult = FALSE; } } return bResult; }
/** * @param bAddCrLf - true if CR/LF must be added. * @return true if entry was added. */ BOOL CTextLogFile::AddToTail(BOOL bAddCrLf) { const BYTE* pBuffer = m_MemStream.GetBuffer(); if (pBuffer == NULL) return FALSE; DWORD dwLength = (DWORD)m_MemStream.GetLength(); _ASSERTE(dwLength > 0); return AddToTail(pBuffer, dwLength, bAddCrLf); }
///////////////////////////q-5 void ReversePrintList(){ Position pHead; int i; pHead=NULL; for (i=0;i<10;i++) { AddToTail(&pHead,i); } IterativelyPrintList(pHead); }
void LinkedList::AddAfter(ListItem* aExistingItem,ListItem* aNewItem) { if(aExistingItem==NULL) AddToHead(aNewItem); else if(aExistingItem == iTail) AddToTail(aNewItem); else { aNewItem->iNext = aExistingItem->iNext; aExistingItem->iNext = aNewItem; } }
//生成一个确定头的位置、运行方向以及节段长度的贪食蛇 Snake::Snake(UnitPosition headunit, Orientation movedirection, int length) { head =new LinkNode(headunit); tail = head; Length = 1; moveDirection = movedirection; if (length < 1) length = 1; //至少有一个头 for (int i = 1; i < length; i++) { UnitPosition bodypos = UnitPosition(headunit.ColIndex,headunit.RowIndex); bodypos.Move(movedirection, -1 * i); AddToTail(bodypos); } }
void CAI_InterestTarget::Add( CAI_InterestTarget_t::CAI_InterestTarget_e type, CBaseEntity *pTarget, const Vector &vecPosition, float flImportance, float flDuration, float flRamp ) { int i = AddToTail(); CAI_InterestTarget_t &target = Element( i ); target.m_eType = type; target.m_hTarget = pTarget; target.m_vecPosition = vecPosition; target.m_flInterest = flImportance; target.m_flStartTime = gpGlobals->curtime; target.m_flEndTime = gpGlobals->curtime + flDuration; target.m_flRamp = flRamp / flDuration; }
/** * \fn que_Requeue * \brief Requeue an item * * Requeue an item at the queue's tail (first in queue). * * \note * \param hQue - The queue object * \param hItem - Handle to queued item * \return TI_OK if item was queued, or TI_NOK if not queued due to overflow * \sa que_Enqueue, que_Dequeue */ TI_STATUS que_Requeue (TI_HANDLE hQue, TI_HANDLE hItem) { TQueue *pQue = (TQueue *)hQue; TQueNodeHdr *pQueNodeHdr; /* the NodeHeader in the given item */ /* * If queue's limits not exceeded add the packet to queue's tail and return TI_OK */ if (pQue->uCount < pQue->uLimit) { /* Find NodeHeader in the given item */ pQueNodeHdr = (TQueNodeHdr *)((TI_UINT8*)hItem + pQue->uNodeHeaderOffset); /* Verify that pNext is NULL --> Sanity check that this item is not already linked to a queue */ if (pQueNodeHdr->pNext) { TRACE0(pQue->hReport, REPORT_SEVERITY_ERROR, "que_Requeue(): Trying to Requeue an item that is already enqueued!!"); return TI_NOK; } /* Enqueue item and increment items counter */ AddToTail (pQueNodeHdr, &pQue->tHead); pQue->uCount++; #ifdef TI_DBG if (pQue->uCount > pQue->uMaxCount) pQue->uMaxCount = pQue->uCount; TRACE0(pQue->hReport, REPORT_SEVERITY_INFORMATION , "que_Requeue(): Requeued successfully\n"); #endif return TI_OK; } /* * Queue is overflowed, return TI_NOK. * Note: This is not expected in the current design, since Tx packet may be requeued * only right after it was dequeued in the same context so the queue can't be full. */ #ifdef TI_DBG pQue->uOverflow++; TRACE0(pQue->hReport, REPORT_SEVERITY_ERROR , "que_Requeue(): Queue Overflow\n"); #endif return TI_NOK; }
int Add( CBaseEntity *pOwner, typedescription_t *pTypeDesc, void **ppPhysObj, PhysInterfaceId_t type ) { int i = AddToTail(); Assert( ppPhysObj ); Assert( *ppPhysObj == NULL ); // expected field to have been cleared Assert( pOwner ); QueuedItem_t &item = Element( i ); item.ppPhysObj = ppPhysObj; item.header.hEntity = pOwner; item.header.type = type; item.header.nObjects = pTypeDesc->fieldSize; item.header.fieldName = AllocPooledString( pTypeDesc->fieldName ); // See comment in CPhysSaveRestoreBlockHandler::QueueSave() return i; }
void CVwsEventQueue::ProcessEventL(CVwsEvent* aEvent) { TInt err=AddToTail(aEvent); if(err!=KErrNone) { if(aEvent!=NULL) { delete aEvent; aEvent=NULL; } User::Leave(err); } if (aEvent) { switch (iState) { case EEmpty: { iState=EProcessing; TRAPD(err,aEvent->ProcessEventL()); if (err) { DeleteHead(); iState=EEmpty; } User::LeaveIfError(err); } break; case EProcessing: // Wait until current event has finished being processed. break; default: ASSERT(EFalse); } } }
/** * @return true if the log was loaded successfully. */ BOOL CTextLogFile::LoadEntries(void) { #ifdef _DEBUG DWORD dwStartTime = GetTickCount(); #endif BOOL bResult = FALSE; PCTSTR pszLogFileName = GetLogFileName(); HANDLE hFile = CreateFile(pszLogFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { DWORD dwFileSize = (DWORD)GetFileSize(hFile, NULL); if (dwFileSize == 0) { // ignore empty files CloseHandle(hFile); return TRUE; } DWORD dwBufferSize = min(dwFileSize, g_dwMaxBufferSize); PBYTE pFileBuffer = new BYTE[dwBufferSize]; if (pFileBuffer) { BYTE arrUTF8Preamble[sizeof(g_arrUTF8Preamble)]; DWORD dwWritten = 0; if (ReadFile(hFile, arrUTF8Preamble, sizeof(arrUTF8Preamble), &dwWritten, NULL) && dwWritten == sizeof(arrUTF8Preamble) && memcmp(arrUTF8Preamble, g_arrUTF8Preamble, sizeof(arrUTF8Preamble)) == 0) { bResult = TRUE; DWORD dwCurrentPos = 0, dwLineStart = 0; for (;;) { dwWritten = 0; DWORD dwFreeSize = dwBufferSize - dwCurrentPos; if (! ReadFile(hFile, pFileBuffer + dwCurrentPos, dwFreeSize, &dwWritten, NULL)) goto end; BOOL bEndOfFile = dwWritten < dwFreeSize; dwWritten += dwCurrentPos; dwCurrentPos = 0; if (dwWritten == 0) goto end; for (;;) { if (dwCurrentPos >= dwWritten && ! bEndOfFile) { if (dwLineStart > 0) { dwCurrentPos -= dwLineStart; MoveMemory(pFileBuffer, pFileBuffer + dwLineStart, dwCurrentPos); dwLineStart = 0; } else { dwBufferSize *= 2; PBYTE pNewFileBuffer = new BYTE[dwBufferSize]; if (! pNewFileBuffer) { bResult = FALSE; goto end; } CopyMemory(pNewFileBuffer, pFileBuffer, dwCurrentPos); delete[] pFileBuffer; pFileBuffer = pNewFileBuffer; } break; } if (dwCurrentPos < dwWritten ? pFileBuffer[dwCurrentPos] == '\r' || pFileBuffer[dwCurrentPos] == '\n' : bEndOfFile) { if (dwLineStart < dwCurrentPos && ! AddToTail(pFileBuffer + dwLineStart, dwCurrentPos - dwLineStart, true)) { bResult = FALSE; goto end; } if (dwCurrentPos == dwWritten) // bEndOfFile == TRUE, see condition above { bResult = TRUE; goto end; } dwLineStart = dwCurrentPos + 1; } ++dwCurrentPos; } } } end: if (! bResult) FreeEntries(); delete[] pFileBuffer; } CloseHandle(hFile); } else { DWORD dwLastError = GetLastError(); if (dwLastError == ERROR_FILE_NOT_FOUND || dwLastError == ERROR_PATH_NOT_FOUND || GetFileAttributes(pszLogFileName) == INVALID_FILE_ATTRIBUTES) { bResult = TRUE; // ignore missing files } } #ifdef _DEBUG DWORD dwEndTime = GetTickCount(); TCHAR szMessage[128]; _stprintf_s(szMessage, countof(szMessage), _T("CTextLogFile::LoadEntries(): %lu entries, %lu bytes, %lu milliseconds\r\n"), GetNumEntries(), GetNumBytes(), dwEndTime - dwStartTime); OutputDebugString(szMessage); #endif return bResult; }
void CopyAndAddToTail(char const *pString) // clone the string and add to the end { char *pNewStr = new char[1 + strlen(pString)]; strcpy_s(pNewStr, 1 + strlen(pString), pString); AddToTail(pNewStr); }