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

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

File:
1 edited

Legend:

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

    r7486 r7524  
    126126    uint8_t         hash[SHA_DIGEST_LENGTH];
    127127    int         *   pendingRequestCount;
    128     tr_ptrArray *   outgoingHandshakes; /* tr_handshake */
    129     tr_ptrArray *   pool; /* struct peer_atom */
    130     tr_ptrArray *   peers; /* tr_peer */
    131     tr_ptrArray *   webseeds; /* tr_webseed */
     128    tr_ptrArray     outgoingHandshakes; /* tr_handshake */
     129    tr_ptrArray     pool; /* struct peer_atom */
     130    tr_ptrArray     peers; /* tr_peer */
     131    tr_ptrArray     webseeds; /* tr_webseed */
    132132    tr_timer *      reconnectTimer;
    133133    tr_timer *      rechokeTimer;
     
    143143{
    144144    tr_session      * session;
    145     tr_ptrArray     * torrents; /* Torrent */
    146     tr_ptrArray     * incomingHandshakes; /* tr_handshake */
    147     tr_ptrArray     * finishedHandshakes; /* tr_handshake */
     145    tr_ptrArray       torrents; /* Torrent */
     146    tr_ptrArray       incomingHandshakes; /* tr_handshake */
     147    tr_ptrArray       finishedHandshakes; /* tr_handshake */
    148148    tr_timer        * bandwidthTimer;
    149149};
     
    264264                    const uint8_t * hash )
    265265{
    266     return (Torrent*) tr_ptrArrayFindSorted( manager->torrents,
     266    return (Torrent*) tr_ptrArrayFindSorted( &manager->torrents,
    267267                                             hash,
    268268                                             torrentCompareToHash );
     
    293293    assert( addr );
    294294
    295     return tr_ptrArrayFindSorted( torrent->peers, addr, peerCompareToAddr );
     295    return tr_ptrArrayFindSorted( &torrent->peers, addr, peerCompareToAddr );
    296296}
    297297
     
    300300                 const tr_address * addr )
    301301{
     302    Torrent * tt = (Torrent*)t;
    302303    assert( torrentIsLocked( t ) );
    303     return tr_ptrArrayFindSorted( t->pool, addr, comparePeerAtomToAddress );
     304    return tr_ptrArrayFindSorted( &tt->pool, addr, comparePeerAtomToAddress );
    304305}
    305306
     
    313314
    314315    return getExistingPeer( t, addr )
    315         || getExistingHandshake( t->outgoingHandshakes, addr )
    316         || getExistingHandshake( t->manager->incomingHandshakes, addr );
     316        || getExistingHandshake( &t->outgoingHandshakes, addr )
     317        || getExistingHandshake( &t->manager->incomingHandshakes, addr );
    317318}
    318319
     
    340341    {
    341342        peer = peerConstructor( torrent->tor, addr );
    342         tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare );
     343        tr_ptrArrayInsertSorted( &torrent->peers, peer, peerCompare );
    343344    }
    344345
     
    381382    atom->time = time( NULL );
    382383
    383     removed = tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare );
     384    removed = tr_ptrArrayRemoveSorted( &t->peers, peer, peerCompare );
    384385    assert( removed == peer );
    385386    peerDestructor( removed );
     
    389390removeAllPeers( Torrent * t )
    390391{
    391     while( !tr_ptrArrayEmpty( t->peers ) )
    392         removePeer( t, tr_ptrArrayNth( t->peers, 0 ) );
     392    while( !tr_ptrArrayEmpty( &t->peers ) )
     393        removePeer( t, tr_ptrArrayNth( &t->peers, 0 ) );
    393394}
    394395
     
    401402    assert( t );
    402403    assert( !t->isRunning );
    403     assert( t->peers );
    404404    assert( torrentIsLocked( t ) );
    405     assert( tr_ptrArrayEmpty( t->outgoingHandshakes ) );
    406     assert( tr_ptrArrayEmpty( t->peers ) );
     405    assert( tr_ptrArrayEmpty( &t->outgoingHandshakes ) );
     406    assert( tr_ptrArrayEmpty( &t->peers ) );
    407407
    408408    memcpy( hash, t->hash, SHA_DIGEST_LENGTH );
     
    412412    tr_timerFree( &t->refillTimer );
    413413
    414     tr_ptrArrayFree( t->webseeds, (PtrArrayForeachFunc)tr_webseedFree );
    415     tr_ptrArrayFree( t->pool, (PtrArrayForeachFunc)tr_free );
    416     tr_ptrArrayFree( t->outgoingHandshakes, NULL );
    417     tr_ptrArrayFree( t->peers, NULL );
     414    tr_ptrArrayDestruct( &t->webseeds, (PtrArrayForeachFunc)tr_webseedFree );
     415    tr_ptrArrayDestruct( &t->pool, (PtrArrayForeachFunc)tr_free );
     416    tr_ptrArrayDestruct( &t->outgoingHandshakes, NULL );
     417    tr_ptrArrayDestruct( &t->peers, NULL );
    418418
    419419    tr_free( t->pendingRequestCount );
     
    435435    t->manager = manager;
    436436    t->tor = tor;
    437     t->pool = tr_ptrArrayNew( );
    438     t->peers = tr_ptrArrayNew( );
    439     t->webseeds = tr_ptrArrayNew( );
    440     t->outgoingHandshakes = tr_ptrArrayNew( );
     437    t->pool = TR_PTR_ARRAY_INIT;
     438    t->peers = TR_PTR_ARRAY_INIT;
     439    t->webseeds = TR_PTR_ARRAY_INIT;
     440    t->outgoingHandshakes = TR_PTR_ARRAY_INIT;
    441441    memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH );
    442442
     
    444444    {
    445445        tr_webseed * w =
    446             tr_webseedNew( tor, tor->info.webseeds[i], peerCallbackFunc,
    447                            t );
    448         tr_ptrArrayAppend( t->webseeds, w );
     446            tr_webseedNew( tor, tor->info.webseeds[i], peerCallbackFunc, t );
     447        tr_ptrArrayAppend( &t->webseeds, w );
    449448    }
    450449
     
    462461
    463462    m->session = session;
    464     m->torrents = tr_ptrArrayNew( );
    465     m->incomingHandshakes = tr_ptrArrayNew( );
    466     m->finishedHandshakes = tr_ptrArrayNew( );
     463    m->torrents = TR_PTR_ARRAY_INIT;
     464    m->incomingHandshakes = TR_PTR_ARRAY_INIT;
     465    m->finishedHandshakes = TR_PTR_ARRAY_INIT;
    467466    m->bandwidthTimer = tr_timerNew( session, bandwidthPulse, m, BANDWIDTH_PERIOD_MSEC );
    468467    return m;
     
    480479    /* free the handshakes.  Abort invokes handshakeDoneCB(), which removes
    481480     * the item from manager->handshakes, so this is a little roundabout... */
    482     while( !tr_ptrArrayEmpty( manager->incomingHandshakes ) )
    483         tr_handshakeAbort( tr_ptrArrayNth( manager->incomingHandshakes, 0 ) );
    484 
    485     tr_ptrArrayFree( manager->incomingHandshakes, NULL );
    486 
    487     while(( handshake = tr_ptrArrayPop( manager->finishedHandshakes )))
     481    while( !tr_ptrArrayEmpty( &manager->incomingHandshakes ) )
     482        tr_handshakeAbort( tr_ptrArrayNth( &manager->incomingHandshakes, 0 ) );
     483
     484    tr_ptrArrayDestruct( &manager->incomingHandshakes, NULL );
     485
     486    while(( handshake = tr_ptrArrayPop( &manager->finishedHandshakes )))
    488487        tr_handshakeFree( handshake );
    489488
    490     tr_ptrArrayFree( manager->finishedHandshakes, NULL );
     489    tr_ptrArrayDestruct( &manager->finishedHandshakes, NULL );
    491490
    492491    /* free the torrents. */
    493     tr_ptrArrayFree( manager->torrents, torrentDestructor );
     492    tr_ptrArrayDestruct( &manager->torrents, torrentDestructor );
    494493
    495494    managerUnlock( manager );
     
    506505    assert( torrentIsLocked( t ) );
    507506
    508     peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
     507    peers = (tr_peer **) tr_ptrArrayPeek( &t->peers, &peerCount );
    509508    ret = tr_new( tr_peer *, peerCount );
    510509
     
    759758    int peerCount = 0;
    760759    int retCount = 0;
    761     tr_peer ** peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
     760    tr_peer ** peers = (tr_peer **) tr_ptrArrayPeek( &t->peers, &peerCount );
    762761    tr_peer ** ret = tr_new( tr_peer *, peerCount );
    763762
     
    813812    blockIterator = blockIteratorNew( t );
    814813    peers = getPeersUploadingToClient( t, &peerCount );
    815     webseedCount = tr_ptrArraySize( t->webseeds );
    816     webseeds = tr_memdup( tr_ptrArrayBase( t->webseeds ),
     814    webseedCount = tr_ptrArraySize( &t->webseeds );
     815    webseeds = tr_memdup( tr_ptrArrayBase( &t->webseeds ),
    817816                          webseedCount * sizeof( tr_webseed* ) );
    818817
     
    11831182        a->from = from;
    11841183        tordbg( t, "got a new atom: %s", tr_peerIoAddrStr( &a->addr, a->port ) );
    1185         tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms );
     1184        tr_ptrArrayInsertSorted( &t->pool, a, comparePeerAtoms );
    11861185    }
    11871186}
     
    11961195getPeerCount( const Torrent * t )
    11971196{
    1198     return tr_ptrArraySize( t->peers ) + tr_ptrArraySize( t->outgoingHandshakes );
     1197    return tr_ptrArraySize( &t->peers ) + tr_ptrArraySize( &t->outgoingHandshakes );
    11991198}
    12001199
     
    12231222
    12241223    if( tr_peerIoIsIncoming ( io ) )
    1225         ours = tr_ptrArrayRemoveSorted( manager->incomingHandshakes,
     1224        ours = tr_ptrArrayRemoveSorted( &manager->incomingHandshakes,
    12261225                                        handshake, handshakeCompare );
    12271226    else if( t )
    1228         ours = tr_ptrArrayRemoveSorted( t->outgoingHandshakes,
     1227        ours = tr_ptrArrayRemoveSorted( &t->outgoingHandshakes,
    12291228                                        handshake, handshakeCompare );
    12301229    else
     
    12971296
    12981297    if( !success )
    1299         tr_ptrArrayAppend( manager->finishedHandshakes, handshake );
     1298        tr_ptrArrayAppend( &manager->finishedHandshakes, handshake );
    13001299
    13011300    if( t )
     
    13181317        tr_netClose( socket );
    13191318    }
    1320     else if( getExistingHandshake( manager->incomingHandshakes, addr ) )
     1319    else if( getExistingHandshake( &manager->incomingHandshakes, addr ) )
    13211320    {
    13221321        tr_netClose( socket );
     
    13341333                                     manager );
    13351334
    1336         tr_ptrArrayInsertSorted( manager->incomingHandshakes, handshake,
     1335        tr_ptrArrayInsertSorted( &manager->incomingHandshakes, handshake,
    13371336                                 handshakeCompare );
    13381337    }
     
    14571456        assert( torrentIsLocked( t ) );
    14581457
    1459         peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
     1458        peers = (tr_peer **) tr_ptrArrayPeek( &t->peers, &peerCount );
    14601459        for( i = 0; i < peerCount; ++i )
    14611460        {
     
    15091508                    uint8_t           af)
    15101509{
    1511     int peerCount = 0;
    15121510    int peersReturning = 0;
    15131511    const Torrent *  t;
     
    15231521    {
    15241522        int i;
    1525         const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
     1523        const tr_peer ** peers = (const tr_peer**) TR_PTR_ARRAY_DATA( &t->peers );
     1524        const int peerCount = TR_PTR_ARRAY_LENGTH( &t->peers );
    15261525        /* for now, this will waste memory on torrents that have both
    15271526         * ipv6 and ipv4 peers */
     
    15921591        rechokePulse( t );
    15931592
    1594         if( !tr_ptrArrayEmpty( t->webseeds ) )
     1593        if( !tr_ptrArrayEmpty( &t->webseeds ) )
    15951594            refillSoon( t );
    15961595    }
     
    16091608
    16101609    /* disconnect the peers. */
    1611     tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor );
    1612     tr_ptrArrayClear( t->peers );
     1610    tr_ptrArrayForeach( &t->peers, (PtrArrayForeachFunc)peerDestructor );
     1611    tr_ptrArrayClear( &t->peers );
    16131612
    16141613    /* disconnect the handshakes.  handshakeAbort calls handshakeDoneCB(),
    16151614     * which removes the handshake from t->outgoingHandshakes... */
    1616     while( !tr_ptrArrayEmpty( t->outgoingHandshakes ) )
    1617         tr_handshakeAbort( tr_ptrArrayNth( t->outgoingHandshakes, 0 ) );
     1615    while( !tr_ptrArrayEmpty( &t->outgoingHandshakes ) )
     1616        tr_handshakeAbort( tr_ptrArrayNth( &t->outgoingHandshakes, 0 ) );
    16181617}
    16191618
     
    16411640
    16421641    t = torrentConstructor( manager, tor );
    1643     tr_ptrArrayInsertSorted( manager->torrents, t, torrentCompare );
     1642    tr_ptrArrayInsertSorted( &manager->torrents, t, torrentCompare );
    16441643
    16451644    managerUnlock( manager );
     
    16571656    assert( t );
    16581657    stopTorrent( t );
    1659     tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare );
     1658    tr_ptrArrayRemoveSorted( &manager->torrents, t, torrentCompare );
    16601659    torrentDestructor( t );
    16611660
     
    16831682    interval = tor->info.pieceCount / (float)tabCount;
    16841683    isSeed = tor && ( tr_cpGetStatus ( tor->completion ) == TR_SEED );
    1685     peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
     1684    peers = (const tr_peer **) TR_PTR_ARRAY_DATA( &t->peers );
     1685    peerCount = TR_PTR_ARRAY_LENGTH( &t->peers );
    16861686
    16871687    memset( tab, 0, tabCount );
     
    17351735
    17361736    t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
    1737     ret = t && ( !tr_ptrArrayEmpty( t->peers ) || !tr_ptrArrayEmpty( t->webseeds ) );
     1737    ret = t && ( !tr_ptrArrayEmpty( &t->peers ) || !tr_ptrArrayEmpty( &t->webseeds ) );
    17381738
    17391739    managerUnlock( manager );
     
    17601760
    17611761    t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
    1762     peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size );
    1763 
    1764     *setmePeersKnown           = tr_ptrArraySize( t->pool );
     1762    peers = (const tr_peer **) TR_PTR_ARRAY_DATA( &t->peers );
     1763    size = TR_PTR_ARRAY_LENGTH( &t->peers );
     1764
     1765    *setmePeersKnown           = tr_ptrArraySize( &t->pool );
    17651766    *setmePeersConnected       = 0;
    17661767    *setmeSeedsConnected       = 0;
     
    17941795    }
    17951796
    1796     webseeds = (const tr_webseed **) tr_ptrArrayPeek( t->webseeds, &size );
     1797    webseeds = (const tr_webseed**) TR_PTR_ARRAY_DATA( &t->webseeds );
     1798    size = TR_PTR_ARRAY_LENGTH( &t->webseeds );
    17971799    for( i=0; i<size; ++i )
    17981800        if( tr_webseedIsActive( webseeds[i] ) )
     
    18161818
    18171819    t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
    1818     webseeds = (const tr_webseed**) tr_ptrArrayPeek( t->webseeds,
    1819                                                      &webseedCount );
     1820    webseeds = (const tr_webseed**) TR_PTR_ARRAY_DATA( &t->webseeds );
     1821    webseedCount = TR_PTR_ARRAY_LENGTH( &t->webseeds );
    18201822    assert( webseedCount == t->tor->info.webseedCount );
    18211823    ret = tr_new0( float, webseedCount );
     
    20152017        int n;
    20162018        struct ChokeData * c;
    2017         tr_ptrArray * randPool = tr_ptrArrayNew( );
     2019        tr_ptrArray randPool = TR_PTR_ARRAY_INIT;
    20182020
    20192021        for( ; i<size; ++i )
     
    20262028                if( isSame( peer ) ) x *= 3;
    20272029                for( y=0; y<x; ++y )
    2028                     tr_ptrArrayAppend( randPool, &choke[i] );
     2030                    tr_ptrArrayAppend( &randPool, &choke[i] );
    20292031            }
    20302032        }
    20312033
    2032         if(( n = tr_ptrArraySize( randPool )))
     2034        if(( n = tr_ptrArraySize( &randPool )))
    20332035        {
    2034             c = tr_ptrArrayNth( randPool, tr_cryptoWeakRandInt( n ));
     2036            c = tr_ptrArrayNth( &randPool, tr_cryptoWeakRandInt( n ));
    20352037            c->doUnchoke = 1;
    20362038            t->optimistic = c->peer;
    20372039        }
    20382040
    2039         tr_ptrArrayFree( randPool, NULL );
     2041        tr_ptrArrayDestruct( &randPool, NULL );
    20402042    }
    20412043
     
    21342136{
    21352137    int i, peerCount, outsize;
    2136     tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &peerCount );
     2138    tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( &t->peers, &peerCount );
    21372139    struct tr_peer ** ret = tr_new( tr_peer *, peerCount );
    21382140
     
    22202222    assert( torrentIsLocked( t ) );
    22212223
    2222     atoms = (struct peer_atom**) tr_ptrArrayPeek( t->pool, &atomCount );
     2224    atoms = (struct peer_atom**) tr_ptrArrayPeek( &t->pool, &atomCount );
    22232225    ret = tr_new( struct peer_atom*, atomCount );
    22242226    for( i = retCount = 0; i < atomCount; ++i )
     
    22992301                    "%d connection candidates, %d atoms, max per pulse is %d",
    23002302                    t->tor->info.name, nBad, nCandidates,
    2301                     tr_ptrArraySize( t->pool ),
     2303                    tr_ptrArraySize( &t->pool ),
    23022304                    (int)MAX_RECONNECTIONS_PER_PULSE );
    23032305
     
    23462348                ++newConnectionsThisSecond;
    23472349
    2348                 tr_ptrArrayInsertSorted( t->outgoingHandshakes, handshake,
     2350                tr_ptrArrayInsertSorted( &t->outgoingHandshakes, handshake,
    23492351                                         handshakeCompare );
    23502352            }
     
    23712373pumpAllPeers( tr_peerMgr * mgr )
    23722374{
    2373     const int torrentCount = tr_ptrArraySize( mgr->torrents );
     2375    const int torrentCount = tr_ptrArraySize( &mgr->torrents );
    23742376    int       i, j;
    23752377
    23762378    for( i=0; i<torrentCount; ++i )
    23772379    {
    2378         Torrent * t = tr_ptrArrayNth( mgr->torrents, i );
    2379         for( j=0; j<tr_ptrArraySize( t->peers ); ++j )
     2380        Torrent * t = tr_ptrArrayNth( &mgr->torrents, i );
     2381        for( j=0; j<tr_ptrArraySize( &t->peers ); ++j )
    23802382        {
    2381             tr_peer * peer = tr_ptrArrayNth( t->peers, j );
     2383            tr_peer * peer = tr_ptrArrayNth( &t->peers, j );
    23822384            tr_peerMsgsPulse( peer->msgs );
    23832385        }
     
    24002402
    24012403    /* free all the finished handshakes */
    2402     while(( handshake = tr_ptrArrayPop( mgr->finishedHandshakes )))
     2404    while(( handshake = tr_ptrArrayPop( &mgr->finishedHandshakes )))
    24032405        tr_handshakeFree( handshake );
    24042406
Note: See TracChangeset for help on using the changeset viewer.