Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: cache.c
- ===================================================================
- --- cache.c (revision 10825)
- +++ cache.c (working copy)
- @@ -32,6 +32,7 @@
- uint32_t offset;
- uint32_t length;
- + time_t time;
- tr_block_index_t block;
- uint8_t * buf;
- @@ -67,7 +68,7 @@
- const struct cache_block * b = blocks[i];
- if( b->block != block ) break;
- if( b->tor != ref->tor ) break;
- -//fprintf( stderr, "pos %d tor %d block %zu\n", i, b->tor->uniqueId, (size_t)b->block );
- +//fprintf( stderr, "pos %d tor %d block %zu time %zu\n", i, b->tor->uniqueId, (size_t)b->block, (size_t)b->time ));
- }
- //fprintf( stderr, "run is %d long from [%d to %d)\n", (int)(i-pos), i, (int)pos );
- @@ -101,6 +102,26 @@
- }
- static int
- +findOldestBlock( tr_cache * cache )
- +{
- + int i;
- + const int n = tr_ptrArraySize( &cache->blocks );
- + const struct cache_block ** blocks = (const struct cache_block**) tr_ptrArrayBase( &cache->blocks );
- + time_t min = blocks[0]->time;
- + int pos = 0;
- +
- + for( i=1; i<n; ++i ) {
- + if( blocks[i]->time < min ) {
- + min = blocks[i]->time;
- + pos = i;
- + }
- + }
- +
- +//fprintf( stderr, "OLDEST block is %zu pos %d\n", (size_t)blocks[pos]->block, pos );
- + return pos;
- +}
- +
- +static int
- flushContiguous( tr_cache * cache, int pos, int n )
- {
- int i;
- @@ -146,7 +167,11 @@
- while( !err && ( tr_ptrArraySize( &cache->blocks ) > cache->maxBlocks ) )
- {
- int n;
- - const int i = findLargestChunk( cache, &n );
- + int i = findLargestChunk( cache, &n );
- +
- + if( n == 1 )
- + i = findOldestBlock( cache );
- +
- err = flushContiguous( cache, i, n );
- }
- @@ -182,7 +207,7 @@
- tr_cache *
- tr_cacheNew( double maxMiB )
- {
- - tr_cache * cache = tr_new( tr_cache, 1 );
- + tr_cache * cache = tr_new0( tr_cache, 1 );
- cache->blocks = TR_PTR_ARRAY_INIT;
- cache->maxBlocks = getMaxBlocks( maxMiB );
- return cache;
- @@ -214,10 +239,7 @@
- if( a->block != b->block )
- return a->block < b->block ? -1 : 1;
- - if( a->block < b->block ) return -1;
- - if( a->block > b->block ) return 1;
- -
- - /* they'r eequal */
- + /* they're equal */
- return 0;
- }
- @@ -255,6 +277,8 @@
- tr_ptrArrayInsertSorted( &cache->blocks, cb, cache_block_compare );
- }
- + cb->time = tr_time();
- +
- tr_free( cb->buf );
- cb->buf = tr_memdup( writeme, cb->length );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement