Changeset 12509


Ignore:
Timestamp:
Jun 19, 2011, 6:34:10 PM (10 years ago)
Author:
jordan
Message:

(trunk libT) add a unique key to each tr_bandwidth object, so that when sorting them arbitrarily we can use that key rather than their pointer address. Apparently comparing pointers that aren't allocated in the same array is undefined behavior.

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r12427 r12509  
    8383******/
    8484
    85 static inline int
    86 comparePointers( const void * a, const void * b )
    87 {
    88     if( a != b )
    89         return a < b ? -1 : 1;
    90 
    91     return 0;
     85static int
     86compareBandwidth( const void * va, const void * vb )
     87{
     88    const tr_bandwidth * a = va;
     89    const tr_bandwidth * b = vb;
     90    return a->uniqueKey - b->uniqueKey;
    9291}
    9392
     
    9998tr_bandwidthConstruct( tr_bandwidth * b, tr_session * session, tr_bandwidth * parent )
    10099{
     100    static unsigned int uniqueKey = 0;
     101
    101102    b->session = session;
    102103    b->children = TR_PTR_ARRAY_INIT;
    103104    b->magicNumber = BANDWIDTH_MAGIC_NUMBER;
     105    b->uniqueKey = uniqueKey++;
    104106    b->band[TR_UP].honorParentLimits = true;
    105107    b->band[TR_DOWN].honorParentLimits = true;
     
    135137        assert( tr_isBandwidth( b->parent ) );
    136138
    137         removed = tr_ptrArrayRemoveSorted( &b->parent->children, b, comparePointers );
     139        removed = tr_ptrArrayRemoveSorted( &b->parent->children, b, compareBandwidth );
    138140        assert( removed == b );
    139         assert( tr_ptrArrayFindSorted( &b->parent->children, b, comparePointers ) == NULL );
     141        assert( tr_ptrArrayFindSorted( &b->parent->children, b, compareBandwidth ) == NULL );
    140142
    141143        b->parent = NULL;
     
    147149        assert( parent->parent != b );
    148150
    149         tr_ptrArrayInsertSorted( &parent->children, b, comparePointers );
    150         assert( tr_ptrArrayFindSorted( &parent->children, b, comparePointers ) == b );
     151        assert( tr_ptrArrayFindSorted( &parent->children, b, compareBandwidth ) == NULL );
     152        tr_ptrArrayInsertSorted( &parent->children, b, compareBandwidth );
     153        assert( tr_ptrArrayFindSorted( &parent->children, b, compareBandwidth ) == b );
    151154        b->parent = parent;
    152155    }
  • trunk/libtransmission/bandwidth.h

    r12280 r12509  
    112112    tr_priority_t priority;
    113113    int magicNumber;
     114    unsigned int uniqueKey;
    114115    tr_session * session;
    115116    tr_ptrArray children; /* struct tr_bandwidth */
Note: See TracChangeset for help on using the changeset viewer.