Changeset 7525


Ignore:
Timestamp:
Dec 29, 2008, 9:51:54 AM (12 years ago)
Author:
charles
Message:

(trunk libT) avoid some unnecessary memory fragmentation... for composited objects that have a tr_bitfield, contain it directly rather than a pointer to one allocated elsewhere on the heap.

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r7199 r7525  
    3939
    4040    /* do we have this block? */
    41     tr_bitfield *  blockBitfield;
     41    tr_bitfield    blockBitfield;
    4242
    4343    /* do we have this piece? */
    44     tr_bitfield *  pieceBitfield;
     44    tr_bitfield    pieceBitfield;
    4545
    4646    /* a block is complete if and only if we have it */
     
    6464tr_cpReset( tr_completion * cp )
    6565{
    66     tr_bitfieldClear( cp->pieceBitfield );
    67     tr_bitfieldClear( cp->blockBitfield );
     66    tr_bitfieldClear( &cp->pieceBitfield );
     67    tr_bitfieldClear( &cp->blockBitfield );
    6868    memset( cp->completeBlocks, 0,
    6969            sizeof( uint16_t ) * cp->tor->info.pieceCount );
     
    7979
    8080    cp->tor             = tor;
    81     cp->blockBitfield   = tr_bitfieldNew( tor->blockCount );
    82     cp->pieceBitfield   = tr_bitfieldNew( tor->info.pieceCount );
    8381    cp->completeBlocks  = tr_new( uint16_t, tor->info.pieceCount );
     82    tr_bitfieldConstruct( &cp->blockBitfield, tor->blockCount );
     83    tr_bitfieldConstruct( &cp->pieceBitfield, tor->info.pieceCount );
    8484    tr_cpReset( cp );
    8585    return cp;
     
    8989tr_cpClose( tr_completion * cp )
    9090{
    91     tr_free        ( cp->completeBlocks );
    92     tr_bitfieldFree( cp->pieceBitfield );
    93     tr_bitfieldFree( cp->blockBitfield );
     91    tr_free( cp->completeBlocks );
     92    tr_bitfieldDestruct( &cp->pieceBitfield );
     93    tr_bitfieldDestruct( &cp->blockBitfield );
    9494    tr_free        ( cp );
    9595}
     
    156156tr_cpPieceBitfield( const tr_completion * cp )
    157157{
    158     return cp->pieceBitfield;
     158    return &cp->pieceBitfield;
    159159}
    160160
     
    194194    cp->haveValidIsDirty = 1;
    195195    cp->completeBlocks[piece] = 0;
    196     tr_bitfieldRemRange ( cp->blockBitfield, start, end );
    197     tr_bitfieldRem( cp->pieceBitfield, piece );
     196    tr_bitfieldRemRange ( &cp->blockBitfield, start, end );
     197    tr_bitfieldRem( &cp->pieceBitfield, piece );
    198198}
    199199
     
    202202                      tr_block_index_t      block )
    203203{
    204     return tr_bitfieldHas( cp->blockBitfield, block );
     204    return tr_bitfieldHas( &cp->blockBitfield, block );
    205205}
    206206
     
    220220
    221221        if( tr_cpPieceIsComplete( cp, piece ) )
    222             tr_bitfieldAdd( cp->pieceBitfield, piece );
    223 
    224         tr_bitfieldAdd( cp->blockBitfield, block );
     222            tr_bitfieldAdd( &cp->pieceBitfield, piece );
     223
     224        tr_bitfieldAdd( &cp->blockBitfield, block );
    225225
    226226        cp->sizeNow += blockSize;
     
    235235{
    236236    assert( cp );
    237     assert( cp->blockBitfield );
    238     assert( cp->blockBitfield->bits );
    239     assert( cp->blockBitfield->bitCount );
    240 
    241     return cp->blockBitfield;
     237    assert( cp->blockBitfield.bits );
     238    assert( cp->blockBitfield.bitCount );
     239
     240    return &cp->blockBitfield;
    242241}
    243242
     
    250249    assert( cp );
    251250    assert( bitfield );
    252     assert( cp->blockBitfield );
    253251
    254252    if( tr_bitfieldTestFast( bitfield, cp->tor->blockCount - 1 ) )
  • trunk/libtransmission/torrent.c

    r7524 r7525  
    538538    tor->error   = 0;
    539539
    540     tor->checkedPieces = tr_bitfieldNew( tor->info.pieceCount );
     540    tr_bitfieldConstruct( &tor->checkedPieces, tor->info.pieceCount );
    541541    tr_torrentUncheck( tor );
    542542
     
    10641064    tor->tracker = NULL;
    10651065
    1066     tr_bitfieldFree( tor->checkedPieces );
     1066    tr_bitfieldDestruct( &tor->checkedPieces );
    10671067
    10681068    tr_free( tor->downloadDir );
     
    16021602                          tr_piece_index_t   piece )
    16031603{
    1604     return tr_bitfieldHas( tor->checkedPieces, piece );
     1604    return tr_bitfieldHas( &tor->checkedPieces, piece );
    16051605}
    16061606
     
    16111611{
    16121612    if( isChecked )
    1613         tr_bitfieldAdd( tor->checkedPieces, piece );
     1613        tr_bitfieldAdd( &tor->checkedPieces, piece );
    16141614    else
    1615         tr_bitfieldRem( tor->checkedPieces, piece );
     1615        tr_bitfieldRem( &tor->checkedPieces, piece );
    16161616}
    16171617
     
    16261626
    16271627    if( isChecked )
    1628         tr_bitfieldAddRange ( tor->checkedPieces, begin, end );
     1628        tr_bitfieldAddRange ( &tor->checkedPieces, begin, end );
    16291629    else
    1630         tr_bitfieldRemRange ( tor->checkedPieces, begin, end );
     1630        tr_bitfieldRemRange ( &tor->checkedPieces, begin, end );
    16311631}
    16321632
     
    16511651tr_torrentUncheck( tr_torrent * tor )
    16521652{
    1653     tr_bitfieldRemRange ( tor->checkedPieces, 0, tor->info.pieceCount );
     1653    tr_bitfieldRemRange ( &tor->checkedPieces, 0, tor->info.pieceCount );
    16541654}
    16551655
     
    16571657tr_torrentCountUncheckedPieces( const tr_torrent * tor )
    16581658{
    1659     return tor->info.pieceCount - tr_bitfieldCountTrueBits(
    1660                tor->checkedPieces );
     1659    return tor->info.pieceCount - tr_bitfieldCountTrueBits( &tor->checkedPieces );
    16611660}
    16621661
  • trunk/libtransmission/torrent.h

    r7368 r7525  
    2727#endif
    2828
     29#include "utils.h" /* tr_bitfield */
     30
    2931#ifndef TR_TORRENT_H
    3032#define TR_TORRENT_H 1
     
    206208    struct tr_completion *     completion;
    207209
    208     struct tr_bitfield *       checkedPieces;
     210    struct tr_bitfield         checkedPieces;
    209211    tr_completeness            completeness;
    210212
  • trunk/libtransmission/utils.c

    r7446 r7525  
    747747
    748748tr_bitfield*
     749tr_bitfieldConstruct( tr_bitfield * b, size_t bitCount )
     750{
     751    b->bitCount = bitCount;
     752    b->byteCount = ( bitCount + 7u ) / 8u;
     753    b->bits = tr_new0( uint8_t, b->byteCount );
     754    return b;
     755}
     756
     757void
     758tr_bitfieldDestruct( tr_bitfield * b )
     759{
     760    tr_free( b->bits );
     761}
     762
     763tr_bitfield*
    749764tr_bitfieldNew( size_t bitCount )
    750765{
    751     tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
    752 
    753     ret->bitCount = bitCount;
    754     ret->byteCount = ( bitCount + 7u ) / 8u;
    755     ret->bits = tr_new0( uint8_t, ret->byteCount );
    756     return ret;
     766    return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitCount );
    757767}
    758768
     
    773783    if( bitfield )
    774784    {
    775         tr_free( bitfield->bits );
     785        tr_bitfieldDestruct( bitfield );
    776786        tr_free( bitfield );
    777787    }
  • trunk/libtransmission/utils.h

    r7113 r7525  
    300300***/
    301301
    302 struct tr_bitfield
     302typedef struct tr_bitfield
    303303{
    304304    uint8_t *  bits;
    305305    size_t     bitCount;
    306306    size_t     byteCount;
    307 };
    308 
    309 typedef struct tr_bitfield tr_bitfield;
     307}
     308tr_bitfield;
     309
     310tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
     311
     312void         tr_bitfieldDestruct( tr_bitfield* );
    310313
    311314tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
     
    347350    start looping. */
    348351#define tr_bitfieldHasFast( bitfield, nth ) \
    349     ( ( bitfield->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
     352    ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
    350353
    351354/** @param high the highest nth bit you're going to access */
Note: See TracChangeset for help on using the changeset viewer.