Beispiel #1
0
bool FixedAllocator::MakeNewChunk( void )
{
    bool allocated = false;
    try
    {
        std::size_t size = chunks_.size();
        // Calling chunks_.reserve *before* creating and initializing the new
        // Chunk means that nothing is leaked by this function in case an
        // exception is thrown from reserve.
        if ( chunks_.capacity() == size )
        {
            if ( 0 == size ) size = 4;
            chunks_.reserve( size * 2 );
        }
        Chunk newChunk;
        allocated = newChunk.Init( blockSize_, numBlocks_ );
        if ( allocated )
            chunks_.push_back( newChunk );
    }
    catch ( ... )
    {
        allocated = false;
    }
    if ( !allocated ) return false;

    allocChunk_ = &chunks_.back();
    deallocChunk_ = &chunks_.front();
    return true;
}
Beispiel #2
0
	void * FixedAllocator::Allocate()
	{
		if (!last_alloc_ || last_alloc_->blocks_available_ == 0)
		{
			// no memory available in the cached chunk, try to find one.
			
			Chunks::iterator e = chunks_.end();
			Chunks::iterator i = chunks_.begin();
				
			for (; i!=e; ++i)
			{
				if (i->blocks_available_ > 0)
				{
					// found a chunk!
					last_alloc_ = &*i;
					break;
				}
			}
			if (i == e)
			{
				// no chunks have blocks available, add a new one.
				chunks_.reserve(chunks_.size()+1);
				MemChunk chunk;
				chunk.Init(block_size_, blocks_);
				chunks_.push_back(chunk);
				last_alloc_ = &chunks_.back();
				last_dealloc_ = &chunks_.back();
			}
		}
		
		// chunk pointer vaild check
		assert(last_alloc_ != NULL);
		assert(last_alloc_->blocks_available_ > 0);
		return last_alloc_->Allocate(block_size_);
	}
Beispiel #3
0
GLSLChunker::Chunk
GLSLChunker::chunkLine(const std::string& line) const
{
    Chunks chunks;
    read(line, chunks);
    return chunks.size() > 0 ? chunks[0] : Chunk();
}
Beispiel #4
0
void
GLSLChunker::write(const Chunks& input, std::string& output) const
{
    std::stringstream buf;
    for(int i=0; i<input.size(); ++i)
    {
        buf << input[i].text << "\n";
    }
    output = buf.str();
}
Beispiel #5
0
void
GLSLChunker::replace(Chunks& input, const std::string& pattern, const std::string& replacement) const
{
    for(int i=0; i<input.size(); ++i)
    {
        Chunk& chunk = input[i];
        osgEarth::replaceIn(chunk.text, pattern, replacement);
        for (unsigned t = 0; t<chunk.tokens.size(); ++t)
            osgEarth::replaceIn(chunk.tokens[t], pattern, replacement);
    }
}
Beispiel #6
0
void FsDirectoryImpl::deleteFile(const std::string& filename, Chunks& deletedChunks)
{
    Path path(filename);
    DirNode* pDirNode;
    INode* pINode;

    //preconditions check
    if (path.isPureDir() ){
        LOG4CPLUS_WARN(m_logger, "deleteFile, but filename is a pure dir " << filename);
        throw InvalidFileOrDirName("deleteFile : is pure dir");
    }
    
    //#TODO, lock

    pDirNode = m_pDirTree->findDirNode(path.getDirName());
    if(!pDirNode) {
        LOG4CPLUS_WARN(m_logger, "directory not exist :" << path.getDirName());
        throw NoSuchFileOrDir("deleteFile : directory");
    }

    if (!(pINode = pDirNode->findFile(path.getFileName()))){
        LOG4CPLUS_WARN(m_logger, "file not exist : " << path.getFileName());
        throw NoSuchFileOrDir("deleteFile : filename");
    }

    Chunks chunks = pINode->getChunks();
    deletedChunks.assign(chunks.begin(), chunks.end());

    //#BUGFIX, [#13] INode:findChunk segment fatal error
    //ChunkTable not synchronized with deleteFile
    //
    for( int i = 0; i < chunks.size() ; i++){
        m_pChunkTable->deleteItem(chunks[i].id);
    }

    //#BUGFIX, [#17] INode:findChunk segment fatal error
    //NewChunkTable not synchronized with deleteFile
    //
    vector<Long> deleteNewChunkIds;
    if(m_pNewChunkTable->deleteItem(pINode, deleteNewChunkIds)){
        string chunkIdList;
        for (int i = 0 ; i < deleteNewChunkIds.size(); i++){
            chunkIdList += deleteNewChunkIds[i];
            chunkIdList += " ";
        }
        LOG4CPLUS_WARN(m_logger, "delete newchunks not committed: " << chunkIdList);
    }

    pDirNode->deleteFile(path.getFileName());
}
Beispiel #7
0
bool FixedAllocator::TrimChunkList( void )
{
    if ( chunks_.empty() )
    {
        assert( NULL == allocChunk_ );
        assert( NULL == deallocChunk_ );
    }

    if ( chunks_.size() == chunks_.capacity() )
        return false;
    // Use the "make-a-temp-and-swap" trick to remove excess capacity.
    Chunks( chunks_ ).swap( chunks_ );

    return true;
}
Beispiel #8
0
	void FixedAllocator::Deallocate(void *p)
	{
		assert(last_dealloc_);
		assert(!chunks_.empty());
		
		// find the chunk that holds the pointer.
		last_dealloc_ = FindChunk(p);
		assert(last_dealloc_);
		
		// found the chunk, so forwards deallocation request to it.
		last_dealloc_->Deallocate(p, block_size_);
		
		// the chunk is empty, should we free it?
		if (last_dealloc_->blocks_available_ == blocks_)
		{
			MemChunk *last_chunk = &chunks_.back();
			
			if (last_chunk = last_dealloc_)
			{
				if (chunks_.size() > 1 && last_dealloc_[-1].blocks_available_ == blocks_)
				{
					// there are two free chunk, so free one.
					last_chunk->Free();
					chunks_.pop_back();
					last_alloc_ = last_dealloc_ = &chunks_.front();
				}
			}
			else if (last_chunk->blocks_available_ == blocks_)
			{
				// there are two free chunk, so free one.
				last_chunk->Free();
				chunks_.pop_back();
				last_alloc_ = last_dealloc_;
			}
			else
			{
				// move the empty chunk to the bottom.
				std::swap(*last_dealloc_, *last_chunk);
				last_alloc_ = last_dealloc_;
			}
		}
	}