int MoveBlock(int Direction) { switch(Direction) { case LEFT: if(DetectCollision(LEFT)) return 1; DisplayBlock(--BlockX, BlockY); break; case RIGHT: if(DetectCollision(RIGHT)) return 1; DisplayBlock(++BlockX, BlockY); break; case DOWN: if(DetectCollision(DOWN)) { GetNextBlock(); //Generate new block on screen return 1; } DisplayBlock(BlockX, ++BlockY); break; case REFRESH: DisplayBlock(BlockX, BlockY); break; } return 0; }
char * const MemoryManager::CoalesceWithPreviousBlock(char * const ptr) { if (IsPreviousBlockFree(ptr) && IsValidAddress(GetPreviousBlock(ptr))) { // Remove previous block from free blocks freeBlocks.remove((Node*)GetPreviousBlock(ptr)); // Little trick: // Set the pointer to point to the previous free block (char*)ptr = GetPreviousBlock(ptr); //Update header size_t newHeaderData = GetHeader(ptr) + GetHeader(GetNextBlock(ptr)); SetHeader(ptr, newHeaderData); //Update footer SetFooter(ptr, newHeaderData); return ptr; } else { return ptr; } }
void * StormFixedBlockAllocator::GetNextBlock(void * resolved_pointer) { if (resolved_pointer == NULL) { return NULL; } return ResolveHandle(GetNextBlock(GetHandleForBlock(resolved_pointer))); }
StormFixedBlockHandle StormFixedBlockAllocator::GetPrevBlock(StormFixedBlockHandle chain_start, StormFixedBlockHandle handle) { if (chain_start == handle) { return InvalidBlockHandle; } StormFixedBlockHandle next = GetNextBlock(chain_start); while (next != InvalidBlockHandle) { if (next == handle) { return chain_start; } chain_start = next; next = GetNextBlock(chain_start); } return InvalidBlockHandle; }
char * const MemoryManager::CoalesceWithNextBlock(char * const ptr) { if (IsNextBlockFree(ptr) && IsValidAddress(GetNextBlock(ptr))) { //Remove next block from free blocks freeBlocks.remove((Node*)GetNextBlock(ptr)); ForwardIteratationOverFreeBlocks(); //Update header size_t newHeaderData = GetHeader(ptr) + GetHeader(GetNextBlock(ptr)); SetHeader(ptr, newHeaderData); //Update footer SetFooter(ptr, newHeaderData); return ptr; } else { return ptr; } }
CTapeBlock CTapFile::operator[](dword blkIdx) { CTapeBlock tb; if (blkIdx < m_Idx.size()) { fseek(tapFile, m_Idx[blkIdx].Offset, SEEK_SET); GetNextBlock(&tb); } else { tb.m_BlkType = CTapeBlock::TAPE_BLK_INVALID; } return tb; }
STDMETHODIMP CMatchFinderMT::Skip(UInt32 num) { do { if (m_Pos == m_PosLimit) GetNextBlock(); if (m_Result != S_OK) return m_Result; m_NumAvailableBytes--; m_DataCurrentPos++; UInt32 len = m_Buffer[m_Pos++]; m_Pos += len; } while(--num != 0); return S_OK; }
StormFixedBlockHandle StormFixedBlockAllocator::FreeBlock(StormFixedBlockHandle handle, StormFixedBlockType::Index type) { if (handle == InvalidBlockHandle) { return InvalidBlockHandle; } StormFixedBlockHandle block_next = GetNextBlock(handle); if (handle.m_Index < 0) { #ifdef _WINDOWS //if (m_UseVirtual) //{ // VirtualFree(handle.m_MallocBlock, 0, MEM_RELEASE); //} //else { free(handle.m_MallocBlock); } #else free(handle.m_MallocBlock); #endif m_OutstandingMallocs--; return block_next; } while (true) { // Read the list head StormGenIndex list_head = m_BlockHead; // Write out the old list head to the new head's next pointer m_NextBlockList[handle.m_Index] = list_head.GetIndex(); // Swap the new value in StormGenIndex new_head = StormGenIndex(handle.m_Index, list_head.GetGen() + 1); if (std::atomic_compare_exchange_weak((std::atomic_uint *)&m_BlockHead.Raw, (unsigned int *)&list_head.Raw, new_head.Raw)) { return block_next; } } }
static void InsertBlockIntoPositionList(void* block) { void* current, *previous; current = GetFirstBlock(); if ((char*)block < (char*)current) /* Make this the head of the list */ { ((struct FreeListElement*) current)->positionprev = RELATIVEPOINTER(block); ((struct FreeListElement*) block)->positionnext = RELATIVEPOINTER(current); FreePositionHead = block; } else /* Insert somewhere in the middle */ { do { previous = current; /* We need to keep a chaising pointer to be able to add to the end of the list. */ current = GetNextBlock(current); } while (current && ((char*)block < (char*)current)); if (current) { ((struct FreeListElement*) current)->positionprev = RELATIVEPOINTER(block); ((struct FreeListElement*) previous)->positionnext = RELATIVEPOINTER(block); ((struct FreeListElement*) block)->positionprev = RELATIVEPOINTER(previous); ((struct FreeListElement*) block)->positionnext = RELATIVEPOINTER(current); } else /* Add to the end of the list */ { ((struct FreeListElement*) previous)->positionnext = RELATIVEPOINTER(block); ((struct FreeListElement*) block)->positionprev = RELATIVEPOINTER(previous); ((struct FreeListElement*) block)->positionnext = 0; } } }
STDMETHODIMP CMatchFinderMT::GetMatches(UInt32 *distances) { if (m_Pos == m_PosLimit) GetNextBlock(); if (m_Result != S_OK) return m_Result; m_NumAvailableBytes--; m_DataCurrentPos++; const UInt32 *buffer = m_Buffer + m_Pos; UInt32 len = *buffer++; *distances++ = len; m_Pos += 1 + len; for (UInt32 i = 0; i != len; i += 2) { distances[i] = buffer[i]; distances[i + 1] = buffer[i + 1]; } return S_OK; }
bool CTZXFile::GetFirstBlock(CTapeBlock* tb) { m_CurBlkIdx = -1; fseek(tapFile, sizeof(TZXHeader), SEEK_SET); return GetNextBlock(tb); }
bool CTapFile::GetBlock(word blkIdx, CTapeBlock* tb) { return blkIdx < GetBlockCount() && Seek(blkIdx) && (blkIdx == 0 ? GetFirstBlock(tb) : GetNextBlock(tb)); }
bool CTapFile::GetFirstBlock(CTapeBlock* tb) { m_CurBlkIdx = -1; return (fseek(tapFile, 0, SEEK_SET) == 0) && GetNextBlock(tb); }
bool MemoryManager::IsNextBlockFree(char * const ptr) const { return IsBlockFree(GetNextBlock(ptr)); }
static void FormatTable (TablePtr tptr) { Int2 blklen; Char ch; Int2 col; ColPtr colFmt; Int2 insetLeft; Int2 insetRight; Char just; Int2 len = 0; Int2 maxwid; Int2 numCols; Int2 returnRow; Int2 row; Uint2 start; Boolean stripSpaces; Int2 tabRow; CharPtr text; text = tptr->text; colFmt = tptr->colFmt; numCols = tptr->numCols; if (text != NULL) { start = 0; row = 0; tabRow = 0; returnRow = 1; col = 0; while (text [start] != '\0') { if (col < numCols) { stripSpaces = colFmt [col].strip; } else { stripSpaces = TRUE; } if (stripSpaces) { while (text [start] == ' ') { start++; } } ch = text [start]; if (ch != '\0' && ch != '\n' && ch != '\r' && ch != '\t') { maxwid = INT2_MAX; just = colFmt [col].just; if (just == 'c') { insetLeft = colFmt [col].inset; insetRight = colFmt [col].inset; } else if (just == 'l') { insetLeft = colFmt [col].inset; insetRight = 0; } else if (just == 'r') { insetLeft = 0; insetRight = colFmt [col].inset; } else { insetLeft = 0; insetRight = 0; } if (col < numCols && colFmt [col].wrap && colFmt [col].width > 0 && colFmt [col].width >= insetLeft + insetRight) { maxwid = colFmt [col].width - insetLeft - insetRight; } blklen = GetNextBlock (text + start, maxwid); len = blklen; if (len > 0) { if (text [start + len] != '\0') { while (len > 0 && text [start + len - 1] == ' ') { len--; } } RecordCell (tptr, start, len, row, col); if (len > 0) { if (colFmt [col].width == 0) { colFmt [col].width = len + 1; } } start += blklen; } else { while (len <= maxwid) { len++; } len--; if (len > 0) { RecordCell (tptr, start, len, row, col); } ch = text [start]; while (ch != '\0' && ch != '\n' && ch != '\r' && ch != '\t') { start++; ch = text [start]; } } } ch = text [start]; if (ch == '\n') { start++; row = returnRow; tabRow = row; returnRow++; col = 0; } else if (ch == '\r') { start++; row++; returnRow = MAX (returnRow, row + 1); } else if (ch == '\t') { start++; row = tabRow; col++; } else if (ch != '\0' && colFmt [col].wrap) { if (len == 0) { start++; } row++; returnRow = MAX (returnRow, row + 1); } else if (ch != '\0') { start++; } } if (row > tptr->numRows) { tptr->numRows = row; } } }