Advertisement
Guest User

cache_test1.diff

a guest
Jun 23rd, 2010
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.47 KB | None | 0 0
  1. Index: cache.c
  2. ===================================================================
  3. --- cache.c (revision 10825)
  4. +++ cache.c (working copy)
  5. @@ -32,6 +32,7 @@
  6.      uint32_t offset;
  7.      uint32_t length;
  8.  
  9. +    time_t time;
  10.      tr_block_index_t block;
  11.  
  12.      uint8_t * buf;
  13. @@ -67,7 +68,7 @@
  14.          const struct cache_block * b = blocks[i];
  15.          if( b->block != block ) break;
  16.          if( b->tor != ref->tor ) break;
  17. -//fprintf( stderr, "pos %d tor %d block %zu\n", i, b->tor->uniqueId, (size_t)b->block );
  18. +//fprintf( stderr, "pos %d tor %d block %zu time %zu\n", i, b->tor->uniqueId, (size_t)b->block, (size_t)b->time ));
  19.      }
  20.  
  21.  //fprintf( stderr, "run is %d long from [%d to %d)\n", (int)(i-pos), i, (int)pos );
  22. @@ -101,6 +102,26 @@
  23.  }
  24.  
  25.  static int
  26. +findOldestBlock( tr_cache * cache )
  27. +{
  28. +    int i;
  29. +    const int n = tr_ptrArraySize( &cache->blocks );
  30. +    const struct cache_block ** blocks = (const struct cache_block**) tr_ptrArrayBase( &cache->blocks );
  31. +    time_t min = blocks[0]->time;
  32. +    int pos = 0;
  33. +
  34. +    for( i=1; i<n; ++i ) {
  35. +        if( blocks[i]->time < min ) {
  36. +            min = blocks[i]->time;
  37. +            pos = i;
  38. +        }
  39. +    }
  40. +
  41. +//fprintf( stderr, "OLDEST block is %zu pos %d\n", (size_t)blocks[pos]->block, pos );
  42. +    return pos;
  43. +}
  44. +
  45. +static int
  46.  flushContiguous( tr_cache * cache, int pos, int n )
  47.  {
  48.      int i;
  49. @@ -146,7 +167,11 @@
  50.      while( !err && ( tr_ptrArraySize( &cache->blocks ) > cache->maxBlocks ) )
  51.      {
  52.          int n;
  53. -        const int i = findLargestChunk( cache, &n );
  54. +        int i = findLargestChunk( cache, &n );
  55. +
  56. +        if( n == 1 )
  57. +            i = findOldestBlock( cache );
  58. +
  59.          err = flushContiguous( cache, i, n );
  60.      }
  61.  
  62. @@ -182,7 +207,7 @@
  63.  tr_cache *
  64.  tr_cacheNew( double maxMiB )
  65.  {
  66. -    tr_cache * cache = tr_new( tr_cache, 1 );
  67. +    tr_cache * cache = tr_new0( tr_cache, 1 );
  68.      cache->blocks = TR_PTR_ARRAY_INIT;
  69.      cache->maxBlocks = getMaxBlocks( maxMiB );
  70.      return cache;
  71. @@ -214,10 +239,7 @@
  72.      if( a->block != b->block )
  73.          return a->block < b->block ? -1 : 1;
  74.  
  75. -    if( a->block < b->block ) return -1;
  76. -    if( a->block > b->block ) return  1;
  77. -
  78. -    /* they'r eequal */
  79. +    /* they're equal */
  80.      return 0;
  81.  }
  82.  
  83. @@ -255,6 +277,8 @@
  84.          tr_ptrArrayInsertSorted( &cache->blocks, cb, cache_block_compare );
  85.      }
  86.  
  87. +    cb->time = tr_time();
  88. +
  89.      tr_free( cb->buf );
  90.      cb->buf = tr_memdup( writeme, cb->length );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement