void mpGame::moveDown() { if(gameblock.y < TILE_H-3) { if(Tiles[gameblock.x][gameblock.y+3] != 0) { if(gameblock.y <= 1) { // GAME OVER over = true; } else merge_block(); nextBlock(); } else gameblock.y++; } else { merge_block(); nextBlock(); } }
/* Place a block to bp. If bp is larger than asize by more than the minimum block size, then split the block into two part, otherwize just set the whole block allocated */ static void place(void *bp, size_t asize){ size_t csize = getSize(getHeader(bp)); size_t prevAllocBit = getPrevAlloc(getHeader(bp)); size_t index; deleteListNode(bp); // Split the block if ((csize - asize) >= (2 * DSIZE)) { // Allocate the first part put(getHeader(bp), pack(asize, prevAllocBit, 1)); // Set the remaining part free bp = nextBlock(bp); put(getHeader(bp), pack((csize - asize), prevAllocBit, 0)); put(getFooter(bp), pack((csize - asize), 0, 0)); // Insert the free part into free list index = getIndex(csize - asize); insertListNode(bp, index); } // No split else{ put(getHeader(bp), pack(csize, prevAllocBit, 1)); setPrevAlloc(getHeader(nextBlock(bp))); } }
void *myalloc2(int size) { int mem_size = ((size+3)/8 + 1) * 8; int *ptr = start; int newSize=0; int oldsize = *(start-1); while(oldsize != 0){ if (oldsize >= mem_size && !blockAllocated(ptr)){ *(ptr-1) = mem_size; allocateBlock(ptr); newSize = oldsize - mem_size; if (newSize > 0) *(nextBlock(ptr)-1) = newSize; return ptr; } ptr = nextBlock(ptr); oldsize = *(ptr-1); } fprintf(stderr, "No space to allocate more\n"); return NULL; }
void merge() { auto pnextBlock = nextBlock(); if (pnextBlock != nullptr) { auto prightBlock = pnextBlock->nextBlock(); if (prightBlock != nullptr) { prightBlock->previous = this; } } auto myLength = getLength(); myLength += pnextBlock->getLength(); setLength(myLength); }
MREA::StreamReader::StreamReader(athena::io::IStreamReader& source, atUint32 blkCount, atUint32 secIdxCount) : DNAMP2::MREA::StreamReader(source) { m_blkCount = blkCount; m_blockInfos.reserve(blkCount); for (atUint32 i=0 ; i<blkCount ; ++i) { m_blockInfos.emplace_back(); BlockInfo& info = m_blockInfos.back(); info.read(source); m_totalDecompLen += info.decompSize; } source.seekAlign32(); m_secIdxs.reserve(secIdxCount); for (atUint32 i=0 ; i<secIdxCount ; ++i) { m_secIdxs.emplace_back(); std::pair<DNAFourCC, atUint32>& idx = m_secIdxs.back(); idx.first.read(source); idx.second = source.readUint32Big(); } source.seekAlign32(); m_blkBase = source.position(); nextBlock(); }
/* * Freeing memory from the small heap */ void SH_free(void *mem) { //First check if memory was allocated using malloc() void * adr = listGetMallocedMemoryAdr(mallocList, mem); if (adr != NULL ) { free(adr); // Memory was allocated using malloc(), use free instead listRemove(mallocList, adr); // Remove address from the list return; } // Memory was not allocated by using malloc, find which small heap it was put into. //struct shMapType * shMapEntry = listGetMemoryLocation(SHList, pthread_self()); // changed to extract allocator's thread_id from mem header rather than by using pthread_self() void *ptr = mem - sizeof(SHMemHeaderStruct); SHMemHeader header = (SHMemHeader) ptr; struct shMapType * shMapEntry = listGetMemoryLocation(SHList, header->thread_id); my_mutex_lock(&shMapEntry->mutex); unsigned int toFree; // Pointer to block that needs to be freed unsigned int cur, prev; toFree = ((unsigned int *) ptr - (shMapEntry->memArea + 1)); if (toFree < shMapEntry->available) { // If block, that is being freed is before the first free block if (((refToNextBlock(toFree, shMapEntry->memArea) + 1) == shMapEntry->available) && shMapEntry->available < shMapEntry->memAreaSize) // If next free block is immediately after block that is being freed shMapEntry->memArea[toFree] += (shMapEntry->memArea[shMapEntry->available] + 1); // Defragmentation of free space else shMapEntry->memArea[refToNextBlock(toFree, shMapEntry->memArea)] = shMapEntry->available; shMapEntry->available = toFree; } else { // If block, that is being freed isn't before the first free block prev = cur = shMapEntry->available; while (cur < toFree) { prev = cur; cur = nextBlock(cur, shMapEntry->memArea); } if ((refToNextBlock(prev, shMapEntry->memArea) + 1) == toFree) { // If previous free block is immediately before block that is being freed shMapEntry->memArea[prev] += (shMapEntry->memArea[toFree] + 1); // Defragmentation of free space if (((refToNextBlock(toFree, shMapEntry->memArea) + 1) == cur) && cur < shMapEntry->memAreaSize) // If next free block is immediately after block that is being freed shMapEntry->memArea[prev] += (shMapEntry->memArea[cur] + 1); // Defragmentation of free space else shMapEntry->memArea[refToNextBlock(toFree, shMapEntry->memArea)] = cur; } else { shMapEntry->memArea[refToNextBlock(prev, shMapEntry->memArea)] = toFree; shMapEntry->memArea[refToNextBlock(toFree, shMapEntry->memArea)] = cur; } } // jonl changed following line as NULL is incorrect, however, it is unclear what it shouldbe //log_into_file("freed from", ptr, NULL ); log_into_file("freed from", ptr, 0 ); my_mutex_unlock(&shMapEntry->mutex); }
void QIODeviceCopier::start() { if(!d->src->isOpen()) { if(!d->src->open(QIODevice::ReadOnly)) { Q_EMIT error(tr("Unable to open source device for reading")); Q_EMIT finished(); return; } } if(!d->dest->isOpen()) { if(!d->dest->open(QIODevice::WriteOnly)) { Q_EMIT error(tr("Unable to open destination device for writing")); Q_EMIT finished(); return; } } // These signals cannot be connected in the constructor since they may // begin firing before the start() method is called // readyRead() and readChannelFinished() are only emitted for sequential // devices - for other types of devices, it is necessary to check atEnd() // in order to determine whether the end of the device has been reached connect(d->src, SIGNAL(readyRead()), d, SLOT(onReadyRead())); connect(d->src, SIGNAL(readChannelFinished()), d, SLOT(onReadChannelFinished())); // The first read from the device needs to be triggered QTimer::singleShot(0, d, d->src->isSequential() ? SLOT(onReadyRead()) : SLOT(nextBlock())); }
BlockStreamBase* DynamicBlockBuffer::Iterator::atomicNextBlock(){ lock_.acquire(); BlockStreamBase* ret=nextBlock(); lock_.release(); return ret; }
void QIODeviceCopierPrivate::nextBlock() { // Attempt to read an amount of data up to the size of the buffer QByteArray data; data.resize(bufferSize); qint64 dataRead = src->read(data.data(), bufferSize); // If an error occurred during the read, emit an error if(dataRead == -1) { Q_EMIT q->error(src->errorString()); Q_EMIT q->finished(); return; } // Write the data to the destination device if(dest->write(data.constData(), dataRead) == -1) { Q_EMIT q->error(dest->errorString()); Q_EMIT q->finished(); return; } // Check if the end of the device has been reached - if so, // emit the finished signal and if not, continue to read // data at the next iteration of the event loop if(src->atEnd()) { Q_EMIT q->finished(); } else { QTimer::singleShot(0, this, SLOT(nextBlock())); } }
void coalesce() { int *ptr = start; int *nextptr; while (length(ptr)){ nextptr = nextBlock(ptr); //need to check for conditions to coalesce // if conditions not met, move on to next block till end if (*(nextptr-1) == 0 || blockAllocated(ptr) || blockAllocated(nextptr)){ ptr = nextBlock(ptr); continue; } *(ptr-1) += length(nextptr); } }
void mpGame::newGame() { memset(Tiles, 0, sizeof(Tiles)); nextBlock(); score = lines = 0; over = false; }
void printallocation() { int size = *(start-1); int *ptr = start; while (length(ptr)){ printf("Mem: %p - Control: %d - Size: %d - M: %d A: %d\n", ptr, *(ptr-1), length(ptr), blockMarked(ptr), blockAllocated(ptr)); ptr = nextBlock(ptr); } }
void rmNearestNeighbor::printToObj(rmU64 * src, rmU32 iSystem) { rmOutFileStream obj("C:\\aNearestNeighborBlock.obj"); rmScene s; rmNode n; rmMesh m; rmU32 i = 0; rmU32 oldSystem[3]; rmU32 oldExtents[3]; oldSystem[0] = systemCur[0]; oldSystem[1] = systemCur[1]; oldSystem[2] = systemCur[2]; oldExtents[0] = extentsCur[0]; oldExtents[1] = extentsCur[1]; oldExtents[2] = extentsCur[2]; rmU32 * sys = (iSystem == 3) ? system3 : system5; extentsCur[0] = extentsCur[1] = extentsCur[2] = iSystem; systemCur[0] = sys[0]; systemCur[1] = sys[1]; systemCur[2] = sys[2]; s.addNode(&n); m.setNumVerts(extentsCur[0] * extentsCur[1] * extentsCur[2] * 64); curBlock = src; init3dLoop(extentsCur[0], extentsCur[1], extentsCur[2]); while (curBlock != 0) { tmp = 1; for (rmU32 xx = 0; xx != 4; ++xx) { for (rmU32 yy = 0; yy != 4; ++yy) { for (rmU32 zz = 0; zz != 4; ++zz) { if (*curBlock & tmp) { m.setVert(i, rmPoint3((static_cast<rmF32>(xx + 4 * x) * 10 + 5), (static_cast<rmF32>(yy + 4 * y) * 10 + 5), (static_cast<rmF32>(zz + 4 * z) * 10 + 5))); } tmp <<= 1; ++i; } } } nextBlock(); } systemCur[0] = oldSystem[0]; systemCur[1] = oldSystem[1]; systemCur[2] = oldSystem[2]; extentsCur[0] = oldExtents[0]; extentsCur[1] = oldExtents[1]; extentsCur[2] = oldExtents[2]; s.getNode(0).setMesh(m); rmObjWriter::writeSceneToObjFile(&obj, &s, 0); }
int *getHeader(int *p) { int *next = firstBlock(); while (next != lastBlock()) { if (*p >= next) { if (*p <= nextBlock(next)) { return next; } } next++; } }
void split(std::size_t amount) { assert(getLength() >= amount); auto revisedLength = getLength() - amount; if (revisedLength > 0) { metadata.setLength(amount); auto pnextBlock = nextBlock(); *pnextBlock = std::move(Block(revisedLength, this)); pnextBlock->previous = this; } }
int blockList (int* p) { int* end = p + (HEAPSIZE/4); // pointer to end of heap if (p == end) return -1; printf("Size\t\tAllocated\tStart\t\t\tEnd\n"); for (; nextInHeap(p, end) ; nextBlock(&p)) { intptr_t payloadEnd = (long)p; payloadEnd += (*p & -2) - 1; printf("%i\t\t%i\t\t%p\t\t%p\n", *p & -2, *p & 1, p, (int*)payloadEnd); // just stuff :) } return 0; }
void sweep(int *ptr) { ptr = start; while (length(ptr)){ if (blockAllocated(ptr) && !blockMarked(ptr)){ myfree(ptr); } else{ unmarkBlock(ptr); } ptr = nextBlock(ptr); } }
struct blastBlock *blastFileNextBlock(struct blastFile *bf, struct blastQuery *bq, struct blastGappedAli *bga) /* Read in next blast block. Return NULL at EOF or end of * gapped alignment. */ { struct blastBlock *bb = NULL; boolean skip = FALSE; while (((bb = nextBlock(bf, bq, bga, &skip)) == NULL) && skip) continue; /* skip to next one */ return bb; }
qint64 BgzfReader::read(char *buff, qint64 maxSize) { if(0 == maxSize) { return 0; } stream.next_out = (Bytef *)buff; stream.avail_out = maxSize; while(stream.avail_out > 0) { if(0 == stream.avail_in) { qint64 returnedValue = ioAdapter.readBlock(buffer, sizeof(buffer)); if(-1 == returnedValue) { coreLog.error(QString("in BgzfReader::read, failed to read %1 bytes from ioAdapter, after %2 bytes already read. %3") .arg(sizeof(buffer)) .arg(ioAdapter.bytesRead()) .arg(ioAdapter.errorString())); throw IOException(BAMDbiPlugin::tr("Can't read input")); } else if(0 == returnedValue) { endOfFile = true; break; } else { stream.avail_in = returnedValue; stream.next_in = (Bytef *)buffer; } } int returnedValue = inflate(&stream, Z_SYNC_FLUSH); if(Z_STREAM_END == returnedValue) { nextBlock(); } else if(Z_OK != returnedValue) { coreLog.error(QString("in BgzfReader::read, failed to decompress %1 bytes, after %2 raw bytes already read") .arg(sizeof(buffer)) .arg(ioAdapter.bytesRead())); throw InvalidFormatException(BAMDbiPlugin::tr("Can't decompress data")); } } if(0 == stream.avail_in) { nextBlock(); } qint64 bytesRead = maxSize - stream.avail_out; return bytesRead; }
int Allocate (int* p, int bytes) { /* Allocate implicit list - header will contain size of block and allocation status first fit - search until first block that will fit The heap will be WORD aligned (4 bytes for 32 bit system), the first WORD of the header will be the size of the allocation and the status. The 2nd WORD will be the ID of the block. The 3rd WORD will be the size of the payload to prevent writeHeap() from exceeding the size of the payload. Then the payload and finally the buffer(unused) to align to WORD increments. Example: Allocate 6 [20|1][0][6][payload][buffer 2] First WORD tells us it is 20 bytes and 1 is allocated. Second WORD tells us it is block 0. Next WORD tells us the payload is 6 bytes. Then the actual data, followed by a buffer of 2 bytes to make it 12. */ if (isZero(bytes)) return -1; int* end = p + (HEAPSIZE/4); // pointer to end of heap // find free block int len = ((bytes + 12)%4 == 0) ? (bytes + 12) : ((((bytes + 12) >> 2) << 2) + 4); while ((p < end) && // not passed end ((*p & 1) || // already allocated (*p < len))) // too small nextBlock(&p); // goto next block (word addressed) // no room in heap if(p >= end) return -1; // allocate int newsize = len; int oldsize = *p & -2; *p = newsize | 1; // set size & allocation *(p + 1) = ++bID; // set blockId *(p + 2) = bytes; // set payload if(newsize < oldsize) *(p + newsize/4) = oldsize - newsize; // set next block #if DEBUG == 1 printf("Header: Address[%p] ", p); // remove these debug lines before submission printf("Size[%i bytes] Allocated[%i] blockId[%i] payload[%i bytes]\n", *p & -2, *p & 1, *(p+1), *(p+2)); #endif #if TESTING == 0 printf("%i\n", bID); #endif return 0; }
/* coalesce free blocks */ static void *coalesce(void *bp){ size_t prevAllocBit = getPrevAlloc(getHeader(bp)); size_t nextAllocBit = getAlloc(getHeader(nextBlock(bp))); size_t size = getSize(getHeader(bp)); size_t index; size_t prevPAlloc; /* both the previous and next block allocated */ if (prevAllocBit && nextAllocBit) { setPrevFree(getHeader(nextBlock(bp))); } /* Only previous block is free */ else if (!prevAllocBit && nextAllocBit) { size += getSize(getHeader(prevBlock(bp))); prevPAlloc = getPrevAlloc(getHeader(prevBlock(bp))); deleteListNode(prevBlock(bp)); put(getFooter(bp), size); bp = prevBlock(bp); put(getHeader(bp), pack(size, prevPAlloc, 0)); setPrevFree(getHeader(nextBlock(bp))); } /* Only next block is free */ else if (prevAllocBit && !nextAllocBit) { size += getSize(getHeader(nextBlock(bp))); deleteListNode(nextBlock(bp)); put(getHeader(bp), pack(size, prevAllocBit, 0)); put(getFooter(bp), size); } /* Both previous and next block are free */ else { size += getSize(getHeader(prevBlock(bp))) + getSize(getFooter(nextBlock(bp))); prevPAlloc = getPrevAlloc(getHeader(prevBlock(bp))); deleteListNode(nextBlock(bp)); deleteListNode(prevBlock(bp)); put(getHeader(prevBlock(bp)), pack(size, prevPAlloc, 0)); put(getFooter(nextBlock(bp)), size); bp = prevBlock(bp); } index = getIndex(size); insertListNode(bp, index); return bp; }
int* findBlockId(int* p, int blockId) { /* helper function - give it pointer from start of heap and blockId will return pointer to block that contains the blockId or return 0 if no block was found with that blockId */ if (isZero(blockId)) return NULL; int* end = p + (HEAPSIZE/4); while((p < end) && !hasBlockId(p, blockId)) nextBlock(&p); if(p == end) p = 0; return p; }
void mxMut::newGame() { grid.clear(); grid.score = 0; grid.blocks_cleared = 0; next.randBlock(); nextBlock(); //setTimer(600); game_over = false; inc_s = inc_c = 0; increase = false; }
void sweep() { int *ptr = firstBlock(); while (ptr != lastBlock()) { if (isMarked(ptr)) { *ptr = (*ptr) - 2; } else { if (isAllocated(ptr)) { (*ptr)--; } } ptr = nextBlock(ptr); } coalesce(); }
void myallocinit(int size) { int blockLen = ((size+3)/8 + 1) * 8; heapLen = blockLen; start = (int *)malloc(blockLen+8); if (start == NULL){ fprintf(stderr, "Error initializing space.\n"); exit(1); } start++; *start = blockLen; start++; *(nextBlock(start)) = 0; }
void mark(void *p) { int *val = (int *) p; if (!inHeap(val)) { return; } int *pointer = getHeader(p); if (isMarked(pointer)) return; if (inHeap(val)) { //printf("%p\n",pointer); *pointer = (*pointer) + 2; while (pointer != nextBlock(pointer)) { pointer++; if (inHeap(pointer)) mark(pointer); } } }
/* Extend the heap by the size words. */ static void *extendHeap(size_t words){ char *bp; size_t size; size_t prevAllocBit; size = (words % 2) ? (words + 1) * WSIZE : (words * WSIZE); if ((long)(bp = mem_sbrk(size)) == -1) return NULL; prevAllocBit = getPrevAlloc(getHeader(bp)); put(getHeader(bp), pack(size, prevAllocBit, 0)); put(getFooter(bp), pack(size, 0, 0)); put(getHeader(nextBlock(bp)), pack(0, prevAllocBit, 1)); return coalesce(bp); // coalesce if the previous blk is free }
void * realloc(void * ptr, size_t size) { char * charPtr = static_cast<char *>(ptr); Block & block = blocks.at(charPtr); Block & next = nextBlock(charPtr); /* There is enough place ahead of the current block */ if(block.size == size) { } else if(block.size > size) { splitBlock(block, size); Block & rest = blocks.at(block.ptr + size); markBlockFree(rest); if(next.free) { resizeBlock(rest, rest.size + next.size); removeBlock(next); } } else if(next.free && block.size + next.size >= size) { splitBlock(next, size-block.size); removeBlock(next); resizeBlock(block, size); } else { void * newPtr = alloc(size); if(newPtr != NULL) { memcpy(newPtr, ptr, block.size); free(ptr); ptr = newPtr; } } #ifdef SELFTEST selfTest(); #endif return ptr; }
void LLSDMessageBuilder::copyFromMessageData(const LLMsgData& data) { // copy the blocks // counting variables used to encode multiple block info S32 block_count = 0; char* block_name = NULL; // loop through msg blocks to loop through variables, totalling up size // data and filling the new (send) message LLMsgData::msg_blk_data_map_t::const_iterator iter = data.mMemberBlocks.begin(); LLMsgData::msg_blk_data_map_t::const_iterator end = data.mMemberBlocks.end(); for(; iter != end; ++iter) { const LLMsgBlkData* mbci = iter->second; if(!mbci) continue; // do we need to encode a block code? if (block_count == 0) { block_count = mbci->mBlockNumber; block_name = (char*)mbci->mName; } // counting down mutliple blocks block_count--; nextBlock(block_name); // now loop through the variables LLMsgBlkData::msg_var_data_map_t::const_iterator dit = mbci->mMemberVarData.begin(); LLMsgBlkData::msg_var_data_map_t::const_iterator dend = mbci->mMemberVarData.end(); for(; dit != dend; ++dit) { //const LLMsgVarData& mvci = *dit; // TODO: Copy mvci data in to block: // (*mCurrentBlock)[varname] = v; } } }
void free(void * ptr) { char * charPtr = static_cast<char *>(ptr); Block & prev = prevBlock(charPtr); Block & next = nextBlock(charPtr); Block & block = blocks.at(charPtr); assert(block.free == false); assert(prev.ptr == NULL || prev.ptr + prev.size == block.ptr); assert(next.ptr == NULL || block.ptr + block.size == next.ptr); /* prev is busy and next is busy */ /* just mark current block as free */ if(!prev.free && !next.free) { markBlockFree(block); } /* prev is free and next is busy */ /* concat prev and curr block into one free block */ else if(prev.free && !next.free) { resizeBlock(prev, prev.size + block.size); removeBlock(block); } /* prev is busy and next is free */ /* concat curr and next block into one free block */ else if(!prev.free && next.free) { resizeBlock(block, block.size + next.size); removeBlock(next); markBlockFree(block); } /* concat all three blocks into one block*/ else if(prev.free && next.free) { resizeBlock(prev, prev.size + block.size + next.size); removeBlock(block); removeBlock(next); } #ifdef SELFTEST selfTest(); #endif }