Exemplo n.º 1
0
//----------------------------------------------------------------------------
//
int
TextureMemoryLayout::getBlockOffset(int bufferWidth, int posX, int posY, int pixMode)
{
    int horizPages, vertPages;
    int numPages = 0;
    int blockNumber = 0;
    
    PixelType pType = modeToPixelType(pixMode);
    const PixelFormatLayout &pf = pixelFormat[pType];

    // First figure out how many pages are above
    vertPages = posY / pf.pageSize.height;
    if (vertPages) {
        horizPages = bufferWidth / pf.pageSize.width;
        posY -= vertPages * pf.pageSize.height;
        numPages += vertPages * horizPages;
    }

    // Now how many are to the left
    horizPages = posX / pf.pageSize.width;
    if (horizPages) {
        posX -= horizPages * pf.pageSize.width;
        numPages += horizPages;
    }

    // Now figure out where we are within the page
    int horizBlocks, vertBlocks;
    horizBlocks = posX / pf.blockSize.width;
    vertBlocks = posY / pf.blockSize.height;
    blockNumber = getBlockNumber(horizBlocks, vertBlocks, pixMode);

    return (numPages * 32) + blockNumber;
}
/**
  * Allocates a free physical page of memory.
  * This is chosen using a round robin algorithm, to even out the wear on the physical device.
  * @return NULL on error, page address on success
  */
uint32_t* MicroBitFileSystem::getFreePage()
{
    // Walk the file table, starting at the last allocated block, looking for an unused page.
    int blocksPerPage = (PAGE_SIZE / MBFS_BLOCK_SIZE);

    // get a handle on the next physical page.
    uint16_t currentPage = getBlockNumber(getPage(lastBlockAllocated));
    uint16_t page = (currentPage + blocksPerPage) % fileSystemSize;
    uint16_t recyclablePage = 0;

    // Walk around the file table, looking for a free page.
    while (page != currentPage)
    {
        bool empty = true;
        bool deleted = false;
        uint16_t next;

        for (int i = 0; i < blocksPerPage; i++)
        {
            next = getNextFileBlock(page + i);
            
            if (next == MBFS_DELETED)
                deleted = true;
            
            else if (next != MBFS_UNUSED)
            {
                empty = false;
                break;
            }
        }

        // See if we found one...
        if (empty)
        {
            lastBlockAllocated = page;
            return getBlock(page);
        }

        // make note of the first unused but un-erased page we find (if any).
        if (deleted && !recyclablePage)
            recyclablePage = page;

        page = (page + blocksPerPage) % fileSystemSize;
    }

    // No empty pages are available, but we may be able to recycle one.
    if (recyclablePage)
    {
        uint32_t *address = getBlock(recyclablePage);
        flash.erase_page(address);
        return address;
    }

    // Nothing available at all. Use the default.
    flash.erase_page(defaultScratchPage);
    return defaultScratchPage;
}
Exemplo n.º 3
0
    void EtherIPC::connectedToServer() {
        done();

        getClientVersion();
        getBlockNumber(); // initial
        fTimer.start(); // should happen after filter creation, might need to move into last filter response handler

        EtherLog::logMsg("Connected to IPC socket");
        emit connectToServerDone();
        emit connectionStateChanged();
    }
Exemplo n.º 4
0
/**
 * Gets a random map block from a given terrain, using either the groups or the blocks defined.
 * @param terrain the terrain to pick a block from.
 * @return Pointer to a randomly chosen map block, given the options available.
 */
MapBlock *MapScript::getNextBlock(RuleTerrain *terrain)
{
	if (_blocks.empty())
	{
		return terrain->getRandomMapBlock(_sizeX * 10, _sizeY * 10, getGroupNumber());
	}
	int result = getBlockNumber();
	if (result < (int)(terrain->getMapBlocks()->size()) && result != MT_UNDEFINED)
	{
		return terrain->getMapBlocks()->at((size_t)(result));
	}
	return 0;
}
Exemplo n.º 5
0
    void EtherIPC::onTimer() {
        getPeerCount();
        getSyncing();

        if ( !fBlockFilterID.isEmpty() && !fSyncing ) {
            getFilterChanges(fBlockFilterID);
        } else {
            getBlockNumber();
        }

        if ( !fEventFilterID.isEmpty() ) {
            getFilterChanges(fEventFilterID);
        }
    }
Exemplo n.º 6
0
    void EtherIPC::connectedToServer() {
        done();

        getClientVersion();
        getBlockNumber(); // initial
        newBlockFilter();
        getNetVersion();

        if ( fStarting == 1 ) {
            fExternal = true;
            emit externalChanged(true);
            fGethLog.append("Attached to external geth, see logs in terminal window.");
        }
        fStarting = 3;
        EtherLog::logMsg("Connected to IPC socket");
    }
Exemplo n.º 7
0
Arquivo: inode.cpp Projeto: xzblh/fs
void writeINODEData(INODE * inodeP, char c, unsigned int offset)
{
	if(offset > inodeP->length){
		while(inodeP->length < offset){
			writeINODEData(inodeP, '\0');
		}
		writeINODEData(inodeP, c, offset);
	}
	else{
		int blockNumber = getBlockNumber(inodeP, offset);
		if(blockNumber == NULL){
			return ;
		}
		BLOCK * blockP = getBlock(blockNumber);
		char * mem = (char *)Malloc(superBlockPointer->blockSize);
		readBlock(blockP, mem);
		mem[offset % superBlockPointer->blockSize] = c;
		writeBlock(blockP, mem);
		free(mem);
		freeBlock(blockP);
	}
}
Exemplo n.º 8
0
Symmetrizer::QuantumNumbers StatesClassification::getQuantumNumbers(FockState in) const             
{
    return BlockToQuantum.find(getBlockNumber(in))->second;
}
Exemplo n.º 9
0
const FockState StatesClassification::getFockState( QuantumNumbers in, InnerQuantumState m) const
{
    if ( Status < Computed ) { ERROR("StatesClassification is not computed yet."); throw (exStatusMismatch()); };
    return getFockState(getBlockNumber(in),m);
}
bool ossimOpenJpegNitfReader::uncompressJpegBlock(ossim_uint32 x,
                                                  ossim_uint32 y)
{
   ossim_uint32 blockNumber = getBlockNumber(ossimIpt(x,y));

   if (traceDebug())
   {
      ossimNotify(ossimNotifyLevel_DEBUG)
         << "ossimNitfTileSource::uncompressJpegBlock DEBUG:"
         << "\nblockNumber:  " << blockNumber
         << "\noffset to block: " << theNitfBlockOffset[blockNumber]
         << "\nblock size: " << theNitfBlockSize[blockNumber]
         << std::endl;
   }
   
   // Seek to the block.
   theFileStr.seekg(theNitfBlockOffset[blockNumber], ios::beg);

   //---
   // Get a buffer to read the compressed block into.  If all the blocks are
   // the same size this will be theCompressedBuf; else we will make our own.
   //---
   ossim_uint8* compressedBuf = &theCompressedBuf.front();
   if (!compressedBuf)
   {
      compressedBuf = new ossim_uint8[theNitfBlockSize[blockNumber]];
   }


   // Read the block into memory.  We could store this
   if (!theFileStr.read((char*)compressedBuf, theNitfBlockSize[blockNumber]))
   {
      theFileStr.clear();
      ossimNotify(ossimNotifyLevel_FATAL)
         << "ossimNitfTileSource::loadBlock Read Error!"
         << "\nReturning error..." << endl;
      theErrorStatus = ossimErrorCodes::OSSIM_ERROR;
      delete [] compressedBuf;
      compressedBuf = 0;
      return false;
   }

   ossimOpenJpegDecoder decoder;
   try
   {
      //theCacheTile = decoder.decodeBuffer(compressedBuf,
      //                                    theNitfBlockSize[blockNumber]);
   }
   catch (const ossimException& e)
   {
      ossimNotify(ossimNotifyLevel_FATAL)
         << e.what() << std::endl;
      theErrorStatus = ossimErrorCodes::OSSIM_ERROR;
   }

   // If theCompressedBuf is null that means we allocated the compressedBuf.
   if (theCompressedBuf.size() == 0)
   {
      delete [] compressedBuf;
      compressedBuf = 0;
   }
   
   if (theErrorStatus == ossimErrorCodes::OSSIM_ERROR)
   {
      return false;
   }
   return true;
}
/**
  * Allocate a free DiretoryEntry in the given directory, extending and refreshing the directory block if necessary.
  *
  * @param directory The directory to add a DirectoryEntry to
  * @return A pointer to the new DirectoryEntry for the given file, or NULL if it was not possible to allocated resources.
  */
DirectoryEntry* MicroBitFileSystem::createDirectoryEntry(DirectoryEntry *directory)
{
    Directory *dir;
    uint16_t block;
    DirectoryEntry *dirent;
    DirectoryEntry *empty = NULL;
    DirectoryEntry *invalid = NULL;

    // Try to find an unused entry in the directory.
    block = directory->first_block;
    dir = (Directory *)getBlock(block);
    dirent = &dir->entry[0];

    // Iterate through the directory entries until we find and unused entry, or run out of space.
    while (1)
    {
        // Scan through each of the blocks in the directory
        if ((uint32_t)(dirent+1) > (uint32_t)dir + MBFS_BLOCK_SIZE)
        {
            block = getNextFileBlock(block);
            if (block == MBFS_EOF)
                break;

            dir = (Directory *)getBlock(block);
            dirent = &dir->entry[0];
        }

        // If we find an empty slot, use that.
        if (dirent->flags & MBFS_DIRECTORY_ENTRY_FREE)
        {
            empty = dirent;
            break;
        }

        // Record the first invalid block we find (used, but then deleted).
        if ((dirent->flags & MBFS_DIRECTORY_ENTRY_VALID) == 0 && invalid == NULL)
            invalid = dirent;

        // Move onto the next entry.
        dirent++;
    }


    // Now choose the best available slot, giving preference to entries that would avoid a FLASH page erase opreation.
    dirent = NULL;

    // Ideally, choose an unused entry within an existing block.
    if (empty)
    {
        dirent = empty;
    }

    // if not possible, try to re-use a second-hand block that has been freed. This will result in an erase operation of the block,
    // but will not consume any more resources.
    else if (invalid)
    {
        dirent = invalid;
        uint16_t b = getBlockNumber(dirent);
        recycleBlock(b, MBFS_BLOCK_TYPE_DIRECTORY);
    }

    // If nothing is available, extend the directory with a new block.
    else
    {
        // Allocate a new logical block
        uint16_t newBlock = getFreeBlock();
        if (newBlock == 0)
            return NULL;

        // Append this to the directory
        uint16_t lastBlock = directory->first_block;
        while (getNextFileBlock(lastBlock) != MBFS_EOF)
            lastBlock = getNextFileBlock(lastBlock);

        // Append the block.
        fileTableWrite(lastBlock, newBlock);
        fileTableWrite(newBlock, MBFS_EOF);

        dirent = (DirectoryEntry *)getBlock(newBlock);
    }

    return dirent;
}
/**
  * Refresh the physical page associated with the given block.
  * Any logical blocks marked for deletion on that page are recycled.
  *
  * @param block the block to recycle.
  * @param type One of MBFS_BLOCK_TYPE_FILE, MBFS_BLOCK_TYPE_DIRECTORY, MBFS_BLOCK_TYPE_FILETABLE. 
  * Erases and regenerates the given block, recycling and data marked for deletion.
  * @return MICROBIT_OK on success.
  */
int MicroBitFileSystem::recycleBlock(uint16_t block, int type)
{
    uint32_t *page = getPage(block);
    uint32_t* scratch = getFreePage();
    uint8_t *write = (uint8_t *)scratch;
    uint16_t b = getBlockNumber(page);

    for (int i = 0; i < PAGE_SIZE / MBFS_BLOCK_SIZE; i++)
    {
        // If we have an unused or deleted block, there's nothing to do - allow the block to be recycled.
        if (fileSystemTable[b] == MBFS_DELETED || fileSystemTable[b] == MBFS_UNUSED) 
        {}

        // If we have been asked to recycle a valid directory block, recycle individual entries where possible.
        else if (b == block && type == MBFS_BLOCK_TYPE_DIRECTORY)
        {
            DirectoryEntry *direntIn = (DirectoryEntry *)getBlock(b);
            DirectoryEntry *direntOut = (DirectoryEntry *)write;

            for (uint16_t entry = 0; entry < MBFS_BLOCK_SIZE / sizeof(DirectoryEntry); entry++)
            {
                if (direntIn->flags & MBFS_DIRECTORY_ENTRY_VALID)
                    flash.flash_write((uint32_t *)direntOut, (uint32_t *)direntIn, sizeof(DirectoryEntry));

                direntIn++;
                direntOut++;
            }
        }

        // All blocks before the root directory are the FileTable. 
        // Recycle any entries marked as DELETED to UNUSED.
        else if (getBlock(b) < (uint32_t *)rootDirectory)
        {
            uint16_t *tableIn = (uint16_t *)getBlock(b);
            uint16_t *tableOut = (uint16_t *)write;
            
            for (int entry = 0; entry < MBFS_BLOCK_SIZE / 2; entry++)
            {
                if (*tableIn != MBFS_DELETED)
                    flash.flash_write(tableOut, tableIn, 2);

                tableIn++;
                tableOut++;
            }
        }

        // Copy all other VALID blocks directly into the scratch page.
        else
            flash.flash_write(write, getBlock(b), MBFS_BLOCK_SIZE);
        
        // move on to next block.
        write += MBFS_BLOCK_SIZE;
        b++;
    }

    // Now refresh the page originally holding the block.
    flash.erase_page(page);
    flash.flash_write(page, scratch, PAGE_SIZE);

    return MICROBIT_OK;
}