write block-aligned fragment to cache
| 3966 | |
| 3967 | // write block-aligned fragment to cache |
| 3968 | lverror_t writeToCache( const void * buf, lvpos_t pos, lvsize_t count ) |
| 3969 | { |
| 3970 | Block * p = findBlock( pos ); |
| 3971 | if ( p ) { |
| 3972 | #if TRACE_BLOCK_WRITE_STREAM |
| 3973 | CRLog::trace("saving data to existing block %x (%x, %x)", (int)p->block_start, (int)pos, (int)count); |
| 3974 | #endif |
| 3975 | p->save( (const lUInt8 *)buf, pos, count ); |
| 3976 | if ( pos + count > _size ) |
| 3977 | _size = pos + count; |
| 3978 | return LVERR_OK; |
| 3979 | } |
| 3980 | #if TRACE_BLOCK_WRITE_STREAM |
| 3981 | CRLog::trace("Block %x not found in cache", pos); |
| 3982 | #endif |
| 3983 | if ( _count>=_blockCount-1 ) { |
| 3984 | // remove last |
| 3985 | for ( Block * p = _firstBlock; p; p=p->next ) { |
| 3986 | if ( p->next && !p->next->next ) { |
| 3987 | #if TRACE_BLOCK_WRITE_STREAM |
| 3988 | dumpBlocks("before remove last"); |
| 3989 | CRLog::trace("dropping block %x (%x, %x)", (int)p->next->block_start, (int)p->next->modified_start, (int)(p->next->modified_end-p->next->modified_start)); |
| 3990 | #endif |
| 3991 | writeBlock( p->next ); |
| 3992 | delete p->next; |
| 3993 | _count--; |
| 3994 | p->next = NULL; |
| 3995 | #if TRACE_BLOCK_WRITE_STREAM |
| 3996 | dumpBlocks("after remove last"); |
| 3997 | #endif |
| 3998 | } |
| 3999 | } |
| 4000 | } |
| 4001 | p = newBlock( pos, count ); |
| 4002 | #if TRACE_BLOCK_WRITE_STREAM |
| 4003 | CRLog::trace("creating block %x", (int)p->block_start); |
| 4004 | #endif |
| 4005 | if ( readBlock( p )!=LVERR_OK ) { |
| 4006 | return LVERR_FAIL; |
| 4007 | } |
| 4008 | #if TRACE_BLOCK_WRITE_STREAM |
| 4009 | CRLog::trace("saving data to new block %x (%x, %x)", (int)p->block_start, (int)pos, (int)count); |
| 4010 | #endif |
| 4011 | p->save( (const lUInt8 *)buf, pos, count ); |
| 4012 | p->next = _firstBlock; |
| 4013 | _firstBlock = p; |
| 4014 | _count++; |
| 4015 | if ( pos + count > _size ) { |
| 4016 | _size = pos + count; |
| 4017 | p->modified_start = p->block_start; |
| 4018 | p->modified_end = p->block_end; |
| 4019 | } |
| 4020 | return LVERR_OK; |
| 4021 | } |
| 4022 | |
| 4023 | |
| 4024 | public: |