]> granicus.if.org Git - transmission/commitdiff
(trunk libT) code cleanup: moving bitset functions to their own .c file.
authorJordan Lee <jordan@transmissionbt.com>
Mon, 21 Feb 2011 01:40:19 +0000 (01:40 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Mon, 21 Feb 2011 01:40:19 +0000 (01:40 +0000)
libtransmission/Makefile.am
libtransmission/bitfield.c
libtransmission/bitset.c [new file with mode: 0644]
libtransmission/bitset.h
libtransmission/peer-mgr.c

index 96018a9fa93bb4c469da5dae35f69cacf8cf5034..a0266603679d29d61d18f2eef47999c9a20b1284 100644 (file)
@@ -21,6 +21,7 @@ libtransmission_a_SOURCES = \
     bandwidth.c \
     bencode.c \
     bitfield.c \
+    bitset.c \
     blocklist.c \
     cache.c \
     clients.c \
index 97e1ed5a8bef21c19d4f1ce4df40c33eb567e8bc..2563ca9a63a20a528413278c854a80c135d285d8 100644 (file)
@@ -210,24 +210,3 @@ tr_bitfieldCountTrueBits( const tr_bitfield* b )
 
     return ret;
 }
-
-/***
-****
-***/
-
-void
-tr_bitsetReserve( tr_bitset * b, size_t size )
-{
-    if( b->bitfield.bitCount < size )
-    {
-        tr_bitfield * tmp = tr_bitfieldDup( &b->bitfield );
-
-        tr_bitfieldDestruct( &b->bitfield );
-        tr_bitfieldConstruct( &b->bitfield, size );
-
-        if( ( tmp->bits != NULL ) && ( tmp->byteCount > 0 ) )
-            memcpy( b->bitfield.bits, tmp->bits, tmp->byteCount );
-
-        tr_bitfieldFree( tmp );
-    }
-}
diff --git a/libtransmission/bitset.c b/libtransmission/bitset.c
new file mode 100644 (file)
index 0000000..82c6360
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * This file Copyright (C) Mnemosyne LLC
+ *
+ * This file is licensed by the GPL version 2. Works owned by the
+ * Transmission project are granted a special exemption to clause 2(b)
+ * so that the bulk of its code can remain under the MIT license.
+ * This exemption does not extend to derived works not owned by
+ * the Transmission project.
+ *
+ * $Id$
+ */
+
+#include "transmission.h"
+#include "bitset.h"
+
+void
+tr_bitsetConstructor( tr_bitset * b, size_t size )
+{
+    tr_bitfieldConstruct( &b->bitfield, size );
+}
+
+void
+tr_bitsetDestructor( tr_bitset * b )
+{
+    tr_bitfieldDestruct( &b->bitfield );
+}
+
+void
+tr_bitsetReserve( tr_bitset * b, size_t size )
+{
+    if( b->bitfield.bitCount < size )
+    {
+        tr_bitfield * tmp = tr_bitfieldDup( &b->bitfield );
+
+        tr_bitfieldDestruct( &b->bitfield );
+        tr_bitfieldConstruct( &b->bitfield, size );
+
+        if( ( tmp->bits != NULL ) && ( tmp->byteCount > 0 ) )
+            memcpy( b->bitfield.bits, tmp->bits, tmp->byteCount );
+
+        tr_bitfieldFree( tmp );
+    }
+}
+
+tr_bool
+tr_bitsetHas( const tr_bitset * b, const size_t nth )
+{
+    if( b->haveAll ) return TRUE;
+    if( b->haveNone ) return FALSE;
+    if( nth >= b->bitfield.bitCount ) return FALSE;
+    return tr_bitfieldHas( &b->bitfield, nth );
+}
+
+void
+tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
+{
+    if( b->haveAll )
+        tr_bitfieldAddRange( a, 0, a->bitCount );
+    else if( !b->haveNone )
+        tr_bitfieldOr( a, &b->bitfield );
+}
+
+/* set 'a' to all the flags that were in 'a' but not 'b' */
+void
+tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
+{
+    if( b->haveAll )
+        tr_bitfieldClear( a );
+    else if( !b->haveNone )
+        tr_bitfieldDifference( a, &b->bitfield );
+}
+
+double
+tr_bitsetPercent( const tr_bitset * b )
+{
+    if( b->haveAll ) return 1.0;
+    if( b->haveNone ) return 0.0;
+    if( b->bitfield.bitCount == 0 ) return 0.0;
+    return tr_bitfieldCountTrueBits( &b->bitfield ) / (double)b->bitfield.bitCount;
+}
+
+void
+tr_bitsetSetHaveAll( tr_bitset * b )
+{
+    b->haveAll = 1;
+    b->haveNone = 0;
+}
+
+void
+tr_bitsetSetHaveNone( tr_bitset * b )
+{
+    b->haveAll = 0;
+    b->haveNone = 1;
+}
+
+int
+tr_bitsetAdd( tr_bitset * b, size_t i )
+{
+    int ret = 0;
+    if( !b->haveAll ) {
+        b->haveNone = 0;
+        tr_bitsetReserve( b, i+1 );
+        ret = tr_bitfieldAdd( &b->bitfield, i );
+    }
+    return ret;
+}
index f99fddfb6b2a8c1586b0c79c7a79c86493dd3074..7fc842975a327814a45fedaeaf3500b5a6eff49b 100644 (file)
@@ -29,90 +29,26 @@ typedef struct tr_bitset
 }
 tr_bitset;
 
-static inline void
-tr_bitsetConstructor( tr_bitset * b, size_t size )
-{
-    tr_bitfieldConstruct( &b->bitfield, size );
-}
-
-static inline void
-tr_bitsetDestructor( tr_bitset * b )
-{
-    tr_bitfieldDestruct( &b->bitfield );
-}
-
 void tr_bitsetReserve( tr_bitset * b, size_t size );
+void tr_bitsetConstructor( tr_bitset * b, size_t size );
+void tr_bitsetDestructor( tr_bitset * b );
 
-static inline tr_bool
-tr_bitsetHasFast( const tr_bitset * b, const size_t nth )
-{
-    if( b->haveAll ) return TRUE;
-    if( b->haveNone ) return FALSE;
-    if( nth >= b->bitfield.bitCount ) return FALSE;
-    return tr_bitfieldHasFast( &b->bitfield, nth );
-}
+void tr_bitsetSetHaveAll( tr_bitset * b );
+void tr_bitsetSetHaveNone( tr_bitset * b );
 
-static inline tr_bool
-tr_bitsetHas( const tr_bitset * b, const size_t nth )
-{
-    if( b->haveAll ) return TRUE;
-    if( b->haveNone ) return FALSE;
-    if( nth >= b->bitfield.bitCount ) return FALSE;
-    return tr_bitfieldHas( &b->bitfield, nth );
-}
+int  tr_bitsetAdd( tr_bitset * b, size_t i );
 
-static inline void
-tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
-{
-    if( b->haveAll )
-        tr_bitfieldAddRange( a, 0, a->bitCount );
-    else if( !b->haveNone )
-        tr_bitfieldOr( a, &b->bitfield );
-}
+/***
+****
+***/
 
-/* set 'a' to all the flags that were in 'a' but not 'b' */
-static inline void
-tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
-{
-    if( b->haveAll )
-        tr_bitfieldClear( a );
-    else if( !b->haveNone )
-        tr_bitfieldDifference( a, &b->bitfield );
-}
-
-static inline double
-tr_bitsetPercent( const tr_bitset * b )
-{
-    if( b->haveAll ) return 1.0;
-    if( b->haveNone ) return 0.0;
-    if( b->bitfield.bitCount == 0 ) return 0.0;
-    return tr_bitfieldCountTrueBits( &b->bitfield ) / (double)b->bitfield.bitCount;
-}
+double tr_bitsetPercent( const tr_bitset * b );
 
-static inline void
-tr_bitsetSetHaveAll( tr_bitset * b )
-{
-    b->haveAll = 1;
-    b->haveNone = 0;
-}
+tr_bool tr_bitsetHas( const tr_bitset * b, const size_t nth );
 
-static inline void
-tr_bitsetSetHaveNone( tr_bitset * b )
-{
-    b->haveAll = 0;
-    b->haveNone = 1;
-}
+void tr_bitsetOr( tr_bitfield * a, const tr_bitset * b );
 
-static inline int
-tr_bitsetAdd( tr_bitset * b, size_t i )
-{
-    int ret = 0;
-    if( !b->haveAll ) {
-        b->haveNone = 0;
-        tr_bitsetReserve( b, i+1 );
-        ret = tr_bitfieldAdd( &b->bitfield, i );
-    }
-    return ret;
-}
+/* set 'a' to all the flags that were in 'a' but not 'b' */
+void tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b );
 
 #endif
index db05ca5f8a5e839ab80c6da244fddc0b69e47e3e..34c07854e3f88b97eab2a143d6fb878aef443a34 100644 (file)
@@ -475,7 +475,7 @@ replicationNew( Torrent * t )
         uint16_t r = 0;
 
         for( peer_i=0; peer_i<peer_count; ++peer_i )
-            if( tr_bitsetHasFast( &peers[peer_i]->have, piece_i ) )
+            if( tr_bitsetHas( &peers[peer_i]->have, piece_i ) )
                 ++r;
 
         t->pieceReplication[piece_i] = r;
@@ -1023,7 +1023,7 @@ assertReplicationCountIsExact( Torrent * t )
         uint16_t r = 0;
 
         for( peer_i=0; peer_i<peer_count; ++peer_i )
-            if( tr_bitsetHasFast( &peers[peer_i]->have, piece_i ) )
+            if( tr_bitsetHas( &peers[peer_i]->have, piece_i ) )
                 ++r;
 
         assert( rep[piece_i] == r );
@@ -1333,7 +1333,7 @@ tr_peerMgrGetNextRequests( tr_torrent           * tor,
         struct weighted_piece * p = pieces + i;
 
         /* if the peer has this piece that we want... */
-        if( tr_bitsetHasFast( have, p->index ) )
+        if( tr_bitsetHas( have, p->index ) )
         {
             tr_block_index_t b = tr_torPieceFirstBlock( tor, p->index );
             const tr_block_index_t e = b + tr_torPieceCountBlocks( tor, p->index );