Changeset 8782


Ignore:
Timestamp:
Jul 6, 2009, 12:27:24 PM (13 years ago)
Author:
charles
Message:

(trunk libT) fix r8781 oops

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r8781 r8782  
    21592159
    21602160static int
    2161 compareCandidates( const void * va,
    2162                    const void * vb )
     2161compareCandidates( const void * va, const void * vb )
    21632162{
    21642163    const struct peer_atom * a = *(const struct peer_atom**) va;
     
    24142413{
    24152414    tr_peer * peer;
    2416     int speed;
     2415    void * clientData;
    24172416    time_t pieceDataTime;
    24182417    time_t time;
    2419     void * clientData;
     2418    int speed;
     2419    tr_bool doPurge;
    24202420};
    24212421
     
    24262426    const struct peer_liveliness * b = vb;
    24272427
     2428    if( a->doPurge != b->doPurge )
     2429        return a->doPurge ? 1 : -1;
     2430
    24282431    if( a->speed != b->speed ) /* faster goes first */
    24292432        return a->speed > b->speed ? -1 : 1;
    24302433
    2431     if( a->pieceDataTime != b->pieceDataTime ) /* the one to give us data more recently goes first */
     2434    /* the one to give us data more recently goes first */
     2435    if( a->pieceDataTime != b->pieceDataTime )
    24322436        return a->pieceDataTime > b->pieceDataTime ? -1 : 1;
    24332437
    2434     if( a->time != b->time ) /* the one we connected to most recently goes first */
     2438    /* the one we connected to most recently goes first */
     2439    if( a->time != b->time )
    24352440        return a->time > b->time ? -1 : 1;
    24362441
     
    24402445/* FIXME: getPeersToClose() should use this */
    24412446static void
    2442 sortPeersFromBestToWorst( tr_peer ** peers, void** clientData, int n, uint64_t now )
     2447sortPeersByLiveliness( tr_peer ** peers, void** clientData, int n, uint64_t now )
    24432448{
    24442449    int i;
    2445     struct peer_liveliness * l;
     2450    struct peer_liveliness *lives, *l;
    24462451
    24472452    /* build a sortable array of peer + extra info */
    2448     l = tr_new0( struct peer_liveliness, n );
    2449     for( i=0; i<n; ++i ) {
     2453    lives = l = tr_new0( struct peer_liveliness, n );
     2454    for( i=0; i<n; ++i, ++l ) {
    24502455        tr_peer * p = peers[i];
    2451         l[i].peer = p;
    2452         l[i].speed = 1024.0 * (tr_peerGetPieceSpeed( p, now, TR_UP ) + tr_peerGetPieceSpeed( p, now, TR_DOWN ));
    2453         l[i].pieceDataTime = p->atom->piece_data_time;
    2454         l[i].time = p->atom->time;
     2456        l->peer = p;
     2457        l->doPurge = p->doPurge;
     2458        l->pieceDataTime = p->atom->piece_data_time;
     2459        l->time = p->atom->time;
     2460        l->speed = 1024.0 * (   tr_peerGetPieceSpeed( p, now, TR_UP )
     2461                              + tr_peerGetPieceSpeed( p, now, TR_DOWN ) );
    24552462        if( clientData )
    2456             l[i].clientData = clientData[i];
     2463            l->clientData = clientData[i];
    24572464    }
    24582465
    24592466    /* sort 'em */
    2460     qsort( l, n, sizeof(struct peer_liveliness), comparePeerLiveliness );
     2467    assert( n == ( l - lives ) );
     2468    qsort( lives, n, sizeof( struct peer_liveliness ), comparePeerLiveliness );
    24612469
    24622470    /* build the peer array */
    2463     for( i=0; i<n; ++i ) {
    2464         peers[i] = l[i].peer;
     2471    for( i=0, l=lives; i<n; ++i, ++l ) {
     2472        peers[i] = l->peer;
    24652473        if( clientData )
    2466             clientData[i] = l[i].clientData;
    2467     }
     2474            clientData[i] = l->clientData;
     2475    }
     2476    assert( n == ( l - lives ) );
    24682477
    24692478    /* cleanup */
    2470     tr_free( l );
     2479    tr_free( lives );
    24712480}
    24722481
     
    24782487    if( n > max )
    24792488    {
    2480         tr_peer ** peers = tr_memdup( tr_ptrArrayBase( &t->peers ), n*sizeof(tr_peer*) );
    2481         sortPeersFromBestToWorst( peers, NULL, n, now );
     2489        void * base = tr_ptrArrayBase( &t->peers );
     2490        tr_peer ** peers = tr_memdup( base, n*sizeof( tr_peer* ) );
     2491        sortPeersByLiveliness( peers, NULL, n, now );
    24822492        while( n > max )
    24832493            closePeer( t, peers[--n] );
     
    24892499enforceSessionPeerLimit( tr_session * session, uint64_t now )
    24902500{
    2491     int n;
    2492     tr_torrent * tor;
     2501    int n = 0;
     2502    tr_torrent * tor = NULL;
    24932503    const int max = tr_sessionGetPeerLimit( session );
    24942504
    24952505    /* count the total number of peers */
    2496     n = 0;
    2497     tor = NULL;
    24982506    while(( tor = tr_torrentNext( session, tor )))
    24992507        n += tr_ptrArraySize( &tor->torrentPeers->peers );
     
    25022510    if( n > max )
    25032511    {
    2504         int n = 0;
    25052512        tr_peer ** peers = tr_new( tr_peer*, n );
    25062513        Torrent ** torrents = tr_new( Torrent*, n );
    25072514
    25082515        /* populate the peer array */
     2516        n = 0;
    25092517        tor = NULL;
    25102518        while(( tor = tr_torrentNext( session, tor ))) {
     
    25192527       
    25202528        /* sort 'em */
    2521         sortPeersFromBestToWorst( peers, (void**)torrents, n, now );
     2529        sortPeersByLiveliness( peers, (void**)torrents, n, now );
    25222530
    25232531        /* cull out the crappiest */
Note: See TracChangeset for help on using the changeset viewer.