Changeset 7524


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.

Location:
trunk/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r7495 r7524  
    9999    int magicNumber;
    100100    tr_session * session;
    101     tr_ptrArray * children; /* struct tr_bandwidth */
    102     tr_ptrArray * peers; /* tr_peerIo */
     101    tr_ptrArray children; /* struct tr_bandwidth */
     102    tr_ptrArray peers; /* tr_peerIo */
    103103};
    104104
     
    131131    tr_bandwidth * b = tr_new0( tr_bandwidth, 1 );
    132132    b->session = session;
    133     b->children = tr_ptrArrayNew( );
    134     b->peers = tr_ptrArrayNew( );
     133    b->children = TR_PTR_ARRAY_INIT;
     134    b->peers = TR_PTR_ARRAY_INIT;
    135135    b->magicNumber = MAGIC_NUMBER;
    136136    b->band[TR_UP].honorParentLimits = TRUE;
     
    146146
    147147    tr_bandwidthSetParent( b, NULL );
    148     tr_ptrArrayFree( b->peers, NULL );
    149     tr_ptrArrayFree( b->children, NULL );
     148    tr_ptrArrayDestruct( &b->peers, NULL );
     149    tr_ptrArrayDestruct( &b->children, NULL );
    150150    b->magicNumber = 0xDEAD;
    151151    tr_free( b );
     
    167167        assert( tr_isBandwidth( b->parent ) );
    168168
    169         tr_ptrArrayRemoveSorted( b->parent->children, b, comparePointers );
     169        tr_ptrArrayRemoveSorted( &b->parent->children, b, comparePointers );
    170170        b->parent = NULL;
    171171    }
     
    176176        assert( parent->parent != b );
    177177
    178         tr_ptrArrayInsertSorted( parent->children, b, comparePointers );
     178        tr_ptrArrayInsertSorted( &parent->children, b, comparePointers );
    179179        b->parent = parent;
    180180    }
     
    270270    {
    271271        int i;
    272         const int n = tr_ptrArraySize( b->peers );
     272        const int n = TR_PTR_ARRAY_LENGTH( &b->peers );
    273273        for( i=0; i<n; ++i )
    274             tr_ptrArrayAppend( peer_pool, tr_ptrArrayNth( b->peers, i ) );
     274            tr_ptrArrayAppend( peer_pool, tr_ptrArrayNth( &b->peers, i ) );
    275275    }
    276276
     
    282282    /* all children should reallocate too */
    283283    if( 1 ) {
    284         int i, n=0;
    285         struct tr_bandwidth ** children = (struct tr_bandwidth**) tr_ptrArrayPeek( b->children, &n );
     284        int i;
     285        struct tr_bandwidth ** children = (struct tr_bandwidth**) TR_PTR_ARRAY_DATA( &b->children );
     286        const int n = TR_PTR_ARRAY_LENGTH( &b->children );
    286287        for( i=0; i<n; ++i )
    287288            allocateBandwidth( children[i], dir, period_msec, peer_pool );
     
    295296{
    296297    int i, n, peerCount;
    297     tr_ptrArray * tmp;
     298    tr_ptrArray tmp = TR_PTR_ARRAY_INIT;
    298299    struct tr_peerIo ** peers;
    299300    const uint64_t now = tr_date( );
     
    304305     * 1. allocate bandwidth to b and its subtree
    305306     * 2. accumulate an array of all the peerIos from b and its subtree. */
    306     tmp = tr_ptrArrayNew( );
    307     allocateBandwidth( b, dir, period_msec, tmp );
    308     peers = (struct tr_peerIo**) tr_ptrArrayPeek( tmp, &peerCount );
     307    allocateBandwidth( b, dir, period_msec, &tmp );
     308    peers = (struct tr_peerIo**) tr_ptrArrayPeek( &tmp, &peerCount );
    309309
    310310    /* Stop all peers from listening for the socket to be ready for IO.
     
    348348
    349349    /* cleanup */
    350     tr_ptrArrayFree( tmp, NULL );
     350    tr_ptrArrayDestruct( &tmp, NULL );
    351351}
    352352
     
    362362    assert( tr_isPeerIo( peerIo ) );
    363363
    364     tr_ptrArrayInsertSorted( b->peers, peerIo, comparePointers );
     364    tr_ptrArrayInsertSorted( &b->peers, peerIo, comparePointers );
    365365}
    366366
     
    372372    assert( tr_isPeerIo( peerIo ) );
    373373
    374     tr_ptrArrayRemoveSorted( b->peers, peerIo, comparePointers );
     374    tr_ptrArrayRemoveSorted( &b->peers, peerIo, comparePointers );
    375375}
    376376
  • trunk/libtransmission/bencode.c

    r7404 r7524  
    327327{
    328328    int           err;
    329     tr_ptrArray * parentStack = tr_ptrArrayNew( );
     329    tr_ptrArray   parentStack = TR_PTR_ARRAY_INIT;
    330330
    331331    top->type = 0; /* set to `uninitialized' */
    332     err = tr_bencParseImpl( buf, end, top, parentStack, setme_end );
     332    err = tr_bencParseImpl( buf, end, top, &parentStack, setme_end );
    333333    if( err )
    334334        tr_bencFree( top );
    335335
    336     tr_ptrArrayFree( parentStack, NULL );
     336    tr_ptrArrayDestruct( &parentStack, NULL );
    337337    return err;
    338338}
     
    933933          void *             user_data )
    934934{
    935     tr_ptrArray * stack = tr_ptrArrayNew( );
    936 
    937     tr_ptrArrayAppend( stack, nodeNew( top ) );
    938 
    939     while( !tr_ptrArrayEmpty( stack ) )
    940     {
    941         struct SaveNode * node = tr_ptrArrayBack( stack );
     935    tr_ptrArray stack = TR_PTR_ARRAY_INIT;
     936
     937    tr_ptrArrayAppend( &stack, nodeNew( top ) );
     938
     939    while( !tr_ptrArrayEmpty( &stack ) )
     940    {
     941        struct SaveNode * node = tr_ptrArrayBack( &stack );
    942942        const tr_benc *   val;
    943943
     
    956956            if( isContainer( node->val ) )
    957957                walkFuncs->containerEndFunc( node->val, user_data );
    958             tr_ptrArrayPop( stack );
     958            tr_ptrArrayPop( &stack );
    959959            tr_free( node->children );
    960960            tr_free( node );
     
    974974                case TYPE_LIST:
    975975                    if( val != node->val )
    976                         tr_ptrArrayAppend( stack, nodeNew( val ) );
     976                        tr_ptrArrayAppend( &stack, nodeNew( val ) );
    977977                    else
    978978                        walkFuncs->listBeginFunc( val, user_data );
     
    981981                case TYPE_DICT:
    982982                    if( val != node->val )
    983                         tr_ptrArrayAppend( stack, nodeNew( val ) );
     983                        tr_ptrArrayAppend( &stack, nodeNew( val ) );
    984984                    else
    985985                        walkFuncs->dictBeginFunc( val, user_data );
     
    993993    }
    994994
    995     tr_ptrArrayFree( stack, NULL );
     995    tr_ptrArrayDestruct( &stack, NULL );
    996996}
    997997
     
    10881088    if( val && val->type )
    10891089    {
    1090         tr_ptrArray *    freeme = tr_ptrArrayNew( );
     1090        tr_ptrArray a = TR_PTR_ARRAY_INIT;
    10911091        struct WalkFuncs walkFuncs;
    10921092
     
    10961096        walkFuncs.listBeginFunc = freeContainerBeginFunc;
    10971097        walkFuncs.containerEndFunc = freeDummyFunc;
    1098         bencWalk( val, &walkFuncs, freeme );
    1099 
    1100         tr_ptrArrayFree( freeme, tr_free );
     1098        bencWalk( val, &walkFuncs, &a );
     1099
     1100        tr_ptrArrayDestruct( &a, tr_free );
    11011101    }
    11021102}
  • trunk/libtransmission/json.c

    r7404 r7524  
    3030{
    3131    tr_benc *      top;
    32     tr_ptrArray *  stack;
     32    tr_ptrArray    stack;
    3333    char *         key;
    3434};
     
    4040    tr_benc * node = NULL;
    4141
    42     if( tr_ptrArrayEmpty( data->stack ) )
     42    if( tr_ptrArrayEmpty( &data->stack ) )
    4343        parent = NULL;
    4444    else
    45         parent = tr_ptrArrayBack( data->stack );
     45        parent = tr_ptrArrayBack( &data->stack );
    4646
    4747    if( !parent )
     
    7272            node = getNode( data );
    7373            tr_bencInitList( node, 0 );
    74             tr_ptrArrayAppend( data->stack, node );
     74            tr_ptrArrayAppend( &data->stack, node );
    7575            break;
    7676
    7777        case JSON_T_ARRAY_END:
    78             tr_ptrArrayPop( data->stack );
     78            tr_ptrArrayPop( &data->stack );
    7979            break;
    8080
     
    8282            node = getNode( data );
    8383            tr_bencInitDict( node, 0 );
    84             tr_ptrArrayAppend( data->stack, node );
     84            tr_ptrArrayAppend( &data->stack, node );
    8585            break;
    8686
    8787        case JSON_T_OBJECT_END:
    88             tr_ptrArrayPop( data->stack );
     88            tr_ptrArrayPop( &data->stack );
    8989            break;
    9090
     
    149149    data.key = NULL;
    150150    data.top = setme_benc;
    151     data.stack = tr_ptrArrayNew( );
     151    data.stack = TR_PTR_ARRAY_INIT;
    152152
    153153    checker = new_JSON_parser( &config );
     
    162162
    163163    delete_JSON_parser( checker );
    164     tr_ptrArrayFree( data.stack, NULL );
     164    tr_ptrArrayDestruct( &data.stack, NULL );
    165165    return err;
    166166}
  • 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
  • trunk/libtransmission/ptrarray.c

    r7404 r7524  
    2020#define GROW 32
    2121
    22 struct tr_ptrArray
    23 {
    24     void ** items;
    25     int     n_items;
    26     int     n_alloc;
    27 };
     22const tr_ptrArray TR_PTR_ARRAY_INIT = { NULL, 0, 0 };
     23
     24void
     25tr_ptrArrayDestruct( tr_ptrArray * p, PtrArrayForeachFunc func )
     26{
     27    assert( p );
     28    assert( p->items || !p->n_items );
     29
     30    if( func )
     31        tr_ptrArrayForeach( p, func );
     32
     33    tr_free( p->items );
     34
     35    memset( p, ~0, sizeof( tr_ptrArray ) );
     36}
    2837
    2938tr_ptrArray*
    3039tr_ptrArrayNew( void )
    3140{
    32     tr_ptrArray * p;
    33 
    34     p = tr_new( tr_ptrArray, 1 );
    35     p->n_items = 0;
    36     p->n_alloc = 0;
    37     p->items = NULL;
    38 
     41    tr_ptrArray * p = tr_new( tr_ptrArray, 1 );
     42    *p = TR_PTR_ARRAY_INIT;
    3943    return p;
    4044}
     
    7074                 PtrArrayForeachFunc func )
    7175{
    72     assert( t );
    73     assert( t->items || !t->n_items );
    74 
    75     if( func )
    76         tr_ptrArrayForeach( t, func );
    77 
    78     tr_free( t->items );
     76    tr_ptrArrayDestruct( t, func );
    7977    tr_free( t );
    8078}
  • trunk/libtransmission/ptrarray.h

    r7404 r7524  
    3333 * A simple pointer array that resizes itself dynamically.
    3434 */
    35 typedef struct tr_ptrArray tr_ptrArray;
     35typedef struct tr_ptrArray
     36{
     37    void ** items;
     38    int     n_items;
     39    int     n_alloc;
     40}
     41tr_ptrArray;
     42
     43#define TR_PTR_ARRAY_DATA( A ) ((A)->items)
     44#define TR_PTR_ARRAY_LENGTH( A ) ((A)->n_items)
    3645
    3746typedef void ( *PtrArrayForeachFunc )( void * );
     47
     48extern const tr_ptrArray TR_PTR_ARRAY_INIT;
     49
     50void          tr_ptrArrayDestruct( tr_ptrArray*, PtrArrayForeachFunc func );
    3851
    3952tr_ptrArray * tr_ptrArrayNew( void );
  • trunk/libtransmission/torrent.c

    r7476 r7524  
    18421842        char * tmp;
    18431843        tr_ptrArrayInsertSorted( dirtyFolders, tr_strdup( dir ), vstrcmp );
     1844
    18441845        tmp = tr_dirname( dir );
    18451846        tr_free( dir );
     
    18961897    char ** s;
    18971898    tr_file_index_t f;
    1898     tr_ptrArray * torrentFiles = tr_ptrArrayNew( );
    1899     tr_ptrArray * folders = tr_ptrArrayNew( );
    1900     tr_ptrArray * dirtyFolders = tr_ptrArrayNew( ); /* dirty == contains non-torrent files */
     1899    tr_ptrArray torrentFiles = TR_PTR_ARRAY_INIT;
     1900    tr_ptrArray folders      = TR_PTR_ARRAY_INIT;
     1901    tr_ptrArray dirtyFolders = TR_PTR_ARRAY_INIT; /* dirty == contains non-torrent files */
    19011902
    19021903    const char * firstFile = tor->info.files[0].name;
     
    19061907
    19071908    for( f=0; f<tor->info.fileCount; ++f )
    1908         tr_ptrArrayInsertSorted( torrentFiles, tor->info.files[f].name, vstrcmp );
     1909        tr_ptrArrayInsertSorted( &torrentFiles, tor->info.files[f].name, vstrcmp );
    19091910
    19101911    /* build the set of folders and dirtyFolders */
    1911     walkLocalData( tor, root, root, NULL, torrentFiles, folders, dirtyFolders );
     1912    walkLocalData( tor, root, root, NULL, &torrentFiles, &folders, &dirtyFolders );
    19121913
    19131914    /* close all the files because we're about to delete them */
     
    19191920
    19201921    /* try to remove entire folders first, so that the recycle bin will be tidy */
    1921     s = (char**) tr_ptrArrayPeek( folders, &n );
     1922    s = (char**) tr_ptrArrayPeek( &folders, &n );
    19221923    for( i=0; i<n; ++i )
    1923         if( tr_ptrArrayFindSorted( dirtyFolders, s[i], vstrcmp ) == NULL )
     1924        if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
    19241925            fileFunc( s[i] );
    19251926
    1926     /* now blow away any remaining torrent files, such torrent files in dirty folders */
     1927    /* now blow away any remaining torrent files, such as torrent files in dirty folders */
    19271928    for( f=0; f<tor->info.fileCount; ++f ) {
    19281929        char * path = tr_buildPath( tor->downloadDir, tor->info.files[f].name, NULL );
     
    19351936     * won't prevent the upper folders from being deleted */
    19361937    {
    1937         tr_ptrArray * cleanFolders = tr_ptrArrayNew( );
    1938         s = (char**) tr_ptrArrayPeek( folders, &n );
     1938        tr_ptrArray cleanFolders = TR_PTR_ARRAY_INIT;
     1939        s = (char**) tr_ptrArrayPeek( &folders, &n );
    19391940        for( i=0; i<n; ++i )
    1940             if( tr_ptrArrayFindSorted( dirtyFolders, s[i], vstrcmp ) == NULL )
    1941                 tr_ptrArrayInsertSorted( cleanFolders, s[i], compareLongestFirst );
    1942         s = (char**) tr_ptrArrayPeek( cleanFolders, &n );
     1941            if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
     1942                tr_ptrArrayInsertSorted( &cleanFolders, s[i], compareLongestFirst );
     1943        s = (char**) tr_ptrArrayPeek( &cleanFolders, &n );
    19431944        for( i=0; i<n; ++i )
    19441945            fileFunc( s[i] );
    1945         tr_ptrArrayFree( cleanFolders, NULL );
     1946        tr_ptrArrayDestruct( &cleanFolders, NULL );
    19461947    }
    19471948
    19481949    /* cleanup */
    1949     tr_ptrArrayFree( dirtyFolders, tr_free );
    1950     tr_ptrArrayFree( folders, tr_free );
    1951     tr_ptrArrayFree( torrentFiles, NULL );
     1950    tr_ptrArrayDestruct( &dirtyFolders, tr_free );
     1951    tr_ptrArrayDestruct( &folders, tr_free );
     1952    tr_ptrArrayDestruct( &torrentFiles, NULL );
    19521953    tr_free( root );
    19531954    tr_free( tmp );
     
    19631964        deleteLocalData( tor, fileFunc );
    19641965    else {
     1966        /* torrent only has one file */
    19651967        char * path = tr_buildPath( tor->downloadDir, tor->info.files[0].name, NULL );
     1968        tr_fdFileClose( path );
    19661969        fileFunc( path );
    19671970        tr_free( path );
Note: See TracChangeset for help on using the changeset viewer.