Changeset 1534
- Timestamp:
- Mar 5, 2007, 11:03:38 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/completion.c
r1356 r1534 31 31 cp = malloc( sizeof( tr_completion_t ) ); 32 32 cp->tor = tor; 33 cp->blockBitfield = malloc( ( tor->blockCount + 7 ) / 8);33 cp->blockBitfield = tr_bitfieldNew( tor->blockCount ); 34 34 cp->blockDownloaders = malloc( tor->blockCount ); 35 cp->pieceBitfield = malloc( ( tor->info.pieceCount + 7 ) / 8);35 cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount ); 36 36 cp->missingBlocks = malloc( tor->info.pieceCount * sizeof( int ) ); 37 37 … … 43 43 void tr_cpClose( tr_completion_t * cp ) 44 44 { 45 free( cp->blockBitfield );46 free( cp->blockDownloaders );47 free( cp->pieceBitfield );48 free( cp->missingBlocks );49 free( cp );45 tr_bitfieldFree( cp->blockBitfield ); 46 free( cp->blockDownloaders ); 47 tr_bitfieldFree( cp->pieceBitfield ); 48 free( cp->missingBlocks ); 49 free( cp ); 50 50 } 51 51 … … 56 56 57 57 cp->blockCount = 0; 58 memset( cp->blockBitfield, 0, ( tor->blockCount + 7 ) / 8);58 tr_bitfieldClear( cp->blockBitfield ); 59 59 memset( cp->blockDownloaders, 0, tor->blockCount ); 60 memset( cp->pieceBitfield, 0, ( tor->info.pieceCount + 7 ) / 8);60 tr_bitfieldClear( cp->pieceBitfield ); 61 61 for( i = 0; i < tor->info.pieceCount; i++ ) 62 62 { … … 107 107 } 108 108 109 uint8_t * tr_cpPieceBitfield( tr_completion_t * cp )109 tr_bitfield_t * tr_cpPieceBitfield( tr_completion_t * cp ) 110 110 { 111 111 return cp->pieceBitfield; … … 196 196 } 197 197 198 uint8_t * tr_cpBlockBitfield( tr_completion_t * cp )198 tr_bitfield_t * tr_cpBlockBitfield( tr_completion_t * cp ) 199 199 { 200 200 return cp->blockBitfield; 201 201 } 202 202 203 void tr_cpBlockBitfieldSet( tr_completion_t * cp, uint8_t * bitfield )203 void tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield ) 204 204 { 205 205 tr_torrent_t * tor = cp->tor; … … 238 238 int blockCount, startBlock, endBlock; 239 239 int complete; 240 uint8_t * bitfield;240 tr_bitfield_t * bitfield; 241 241 242 242 blockCount = tr_pieceCountBlocks( piece ); -
trunk/libtransmission/completion.h
r1356 r1534 25 25 struct tr_completion_s 26 26 { 27 tr_torrent_t * tor;28 uint8_t* blockBitfield;29 uint8_t * blockDownloaders;30 int blockCount;31 uint8_t* pieceBitfield;32 int * missingBlocks;27 tr_torrent_t * tor; 28 tr_bitfield_t * blockBitfield; 29 uint8_t * blockDownloaders; 30 int blockCount; 31 tr_bitfield_t * pieceBitfield; 32 int * missingBlocks; 33 33 }; 34 34 … … 48 48 int tr_cpPieceHasAllBlocks( tr_completion_t *, int piece ); 49 49 int tr_cpPieceIsComplete( tr_completion_t *, int piece ); 50 uint8_t* tr_cpPieceBitfield( tr_completion_t * );50 tr_bitfield_t * tr_cpPieceBitfield( tr_completion_t * ); 51 51 void tr_cpPieceAdd( tr_completion_t *, int piece ); 52 52 void tr_cpPieceRem( tr_completion_t *, int piece ); … … 58 58 void tr_cpBlockAdd( tr_completion_t *, int block ); 59 59 void tr_cpBlockRem( tr_completion_t *, int block ); 60 uint8_t* tr_cpBlockBitfield( tr_completion_t * );61 void tr_cpBlockBitfieldSet( tr_completion_t *, uint8_t * );60 tr_bitfield_t * tr_cpBlockBitfield( tr_completion_t * ); 61 void tr_cpBlockBitfieldSet( tr_completion_t *, tr_bitfield_t * ); 62 62 float tr_cpPercentBlocksInPiece( tr_completion_t * cp, int piece ); 63 63 /* Missing = we don't have it and we are not getting it from any peer yet */ -
trunk/libtransmission/fastresume.h
r1419 r1534 192 192 uint8_t * buf; 193 193 size_t len; 194 tr_bitfield_t bitfield; 194 195 195 196 len = FR_PROGRESS_LEN( tor ); … … 220 221 221 222 /* Copy the bitfield for blocks and fill blockHave */ 222 tr_cpBlockBitfieldSet( tor->completion, buf + FR_MTIME_LEN( tor ) ); 223 memset( &bitfield, 0, sizeof bitfield ); 224 bitfield.len = FR_BLOCK_BITFIELD_LEN( tor ); 225 bitfield.bits = buf + FR_MTIME_LEN( tor ); 226 tr_cpBlockBitfieldSet( tor->completion, &bitfield ); 223 227 224 228 /* Copy the 'slotPiece' table */ -
trunk/libtransmission/internal.h
r1462 r1534 125 125 typedef struct tr_completion_s tr_completion_t; 126 126 typedef struct tr_shared_s tr_shared_t; 127 typedef struct tr_bitfield_s tr_bitfield_t; 127 128 128 129 typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t; -
trunk/libtransmission/peer.c
r1452 r1534 39 39 struct tr_peer_s 40 40 { 41 tr_torrent_t * tor;42 43 struct in_addr addr;44 in_port_t port; /* peer's listening port, 0 if not known */45 46 #define PEER_STATUS_IDLE 1 /* Need to connect */47 #define PEER_STATUS_CONNECTING 2 /* Trying to send handshake */48 #define PEER_STATUS_HANDSHAKE 4 /* Waiting for peer's handshake */49 #define PEER_STATUS_CONNECTED 8 /* Got peer's handshake */50 int status;51 int socket;52 char incoming;53 uint64_t date;54 uint64_t keepAlive;55 56 char amChoking;57 char amInterested;58 char peerChoking;59 char peerInterested;60 61 int optimistic;62 uint64_t lastChoke;63 64 uint8_t id[20];41 tr_torrent_t * tor; 42 43 struct in_addr addr; 44 in_port_t port; /* peer's listening port, 0 if not known */ 45 46 #define PEER_STATUS_IDLE 1 /* Need to connect */ 47 #define PEER_STATUS_CONNECTING 2 /* Trying to send handshake */ 48 #define PEER_STATUS_HANDSHAKE 4 /* Waiting for peer's handshake */ 49 #define PEER_STATUS_CONNECTED 8 /* Got peer's handshake */ 50 int status; 51 int socket; 52 char incoming; 53 uint64_t date; 54 uint64_t keepAlive; 55 56 char amChoking; 57 char amInterested; 58 char peerChoking; 59 char peerInterested; 60 61 int optimistic; 62 uint64_t lastChoke; 63 64 uint8_t id[20]; 65 65 66 66 /* The pieces that the peer has */ 67 uint8_t* bitfield;68 int pieceCount;69 float progress;70 71 int goodPcs;72 int badPcs;73 int banned;67 tr_bitfield_t * bitfield; 68 int pieceCount; 69 float progress; 70 71 int goodPcs; 72 int badPcs; 73 int banned; 74 74 /* The pieces that the peer is contributing to */ 75 uint8_t* blamefield;75 tr_bitfield_t * blamefield; 76 76 /* The bad pieces that the peer has contributed to */ 77 uint8_t* banfield;78 79 uint8_t * buf;80 int size;81 int pos;82 83 uint8_t * outMessages;84 int outMessagesSize;85 int outMessagesPos;86 uint8_t outBlock[13+16384];87 int outBlockSize;88 int outBlockLoaded;89 int outBlockSending;90 91 int inRequestCount;92 tr_request_t inRequests[OUR_REQUEST_COUNT];93 int inIndex;94 int inBegin;95 int inLength;96 uint64_t inTotal;97 98 int outRequestCount;99 tr_request_t outRequests[MAX_REQUEST_COUNT];100 uint64_t outTotal;101 uint64_t outDate;102 103 tr_ratecontrol_t * download;104 tr_ratecontrol_t * upload;77 tr_bitfield_t * banfield; 78 79 uint8_t * buf; 80 int size; 81 int pos; 82 83 uint8_t * outMessages; 84 int outMessagesSize; 85 int outMessagesPos; 86 uint8_t outBlock[13+16384]; 87 int outBlockSize; 88 int outBlockLoaded; 89 int outBlockSending; 90 91 int inRequestCount; 92 tr_request_t inRequests[OUR_REQUEST_COUNT]; 93 int inIndex; 94 int inBegin; 95 int inLength; 96 uint64_t inTotal; 97 98 int outRequestCount; 99 tr_request_t outRequests[MAX_REQUEST_COUNT]; 100 uint64_t outTotal; 101 uint64_t outDate; 102 103 tr_ratecontrol_t * download; 104 tr_ratecontrol_t * upload; 105 105 }; 106 106 … … 163 163 tr_cpDownloaderRem( tor->completion, block ); 164 164 } 165 if( peer->bitfield ) 166 { 167 free( peer->bitfield ); 168 } 169 if( peer->blamefield ) 170 { 171 free( peer->blamefield ); 172 } 173 if( peer->banfield ) 174 { 175 free( peer->banfield ); 176 } 165 tr_bitfieldFree( peer->bitfield ); 166 tr_bitfieldFree( peer->blamefield ); 167 tr_bitfieldFree( peer->banfield ); 177 168 if( peer->buf ) 178 169 { … … 533 524 * 534 525 **********************************************************************/ 535 uint8_t * tr_peerBitfield( tr_peer_t * peer )526 tr_bitfield_t * tr_peerBitfield( tr_peer_t * peer ) 536 527 { 537 528 return peer->bitfield; … … 602 593 /* Assume the peer wasn't responsible for the bad pieces 603 594 we was banned for */ 604 memset( peer->banfield, 0x00, ( tor->info.pieceCount + 7 ) / 8);595 tr_bitfieldClear( peer->banfield ); 605 596 } 606 597 } … … 612 603 if( !peer->banfield ) 613 604 { 614 peer->banfield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1);605 peer->banfield = tr_bitfieldNew( tor->info.pieceCount ); 615 606 } 616 607 tr_bitfieldAdd( peer->banfield, piece ); -
trunk/libtransmission/peer.h
r1422 r1534 46 46 float tr_peerProgress ( tr_peer_t * ); 47 47 int tr_peerPort ( tr_peer_t * ); 48 uint8_t * tr_peerBitfield( tr_peer_t * );48 tr_bitfield_t * tr_peerBitfield ( tr_peer_t * ); 49 49 float tr_peerDownloadRate ( tr_peer_t * ); 50 50 float tr_peerUploadRate ( tr_peer_t * ); -
trunk/libtransmission/peerparse.h
r1442 r1534 105 105 if( !peer->bitfield ) 106 106 { 107 peer->bitfield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1);107 peer->bitfield = tr_bitfieldNew( tor->info.pieceCount ); 108 108 } 109 109 if( !tr_bitfieldHas( peer->bitfield, piece ) ) … … 155 155 if( !peer->bitfield ) 156 156 { 157 peer->bitfield = malloc( bitfieldSize ); 158 } 159 memcpy( peer->bitfield, p, bitfieldSize ); 157 peer->bitfield = tr_bitfieldNew( inf->pieceCount ); 158 } 159 assert( bitfieldSize == peer->bitfield->len ); 160 memcpy( peer->bitfield->bits, p, bitfieldSize ); 160 161 161 162 peer->pieceCount = 0; … … 295 296 if( !peer->blamefield ) 296 297 { 297 peer->blamefield = calloc( ( tor->info.pieceCount + 7 ) / 8, 1);298 peer->blamefield = tr_bitfieldNew( tor->info.pieceCount ); 298 299 } 299 300 tr_bitfieldAdd( peer->blamefield, index ); -
trunk/libtransmission/peerutils.h
r1425 r1534 105 105 static int isInteresting( tr_torrent_t * tor, tr_peer_t * peer ) 106 106 { 107 tr_info_t * inf = &tor->info; 108 109 int i; 110 int bitfieldSize = ( inf->pieceCount + 7 ) / 8; 111 uint8_t * bitfield = tr_cpPieceBitfield( tor->completion ); 107 int ii; 108 tr_bitfield_t * bitfield = tr_cpPieceBitfield( tor->completion ); 112 109 113 110 if( !peer->bitfield ) … … 117 114 } 118 115 119 for( i = 0; i < bitfieldSize; i++ ) 120 { 121 if( ( peer->bitfield[i] & ~(bitfield[i]) ) & 0xFF ) 116 assert( bitfield->len == peer->bitfield->len ); 117 for( ii = 0; ii < bitfield->len; ii++ ) 118 { 119 if( ( peer->bitfield->bits[ii] & ~(bitfield->bits[ii]) ) & 0xFF ) 122 120 { 123 121 return 1; … … 198 196 /* All pieces in 'pool' have 'minMissing' missing blocks. Find 199 197 the rarest ones. */ 200 uint8_t * bitfield;198 tr_bitfield_t * bitfield; 201 199 int piece; 202 200 int min, foo, j; -
trunk/libtransmission/utils.h
r1444 r1534 102 102 } 103 103 104 struct tr_bitfield_s 105 { 106 uint8_t * bits; 107 int len; 108 }; 109 110 /* note that the argument is how many bits are needed, not bytes */ 111 static inline tr_bitfield_t * tr_bitfieldNew( int bitcount ) 112 { 113 tr_bitfield_t * ret; 114 115 ret = calloc( 1, sizeof *ret ); 116 if( NULL == ret ) 117 { 118 return NULL; 119 } 120 ret->len = ( bitcount + 7 ) / 8; 121 ret->bits = calloc( ret->len, 1 ); 122 if( NULL == ret->bits ) 123 { 124 free( ret ); 125 return NULL; 126 } 127 128 return ret; 129 } 130 131 static inline void tr_bitfieldFree( tr_bitfield_t * bitfield ) 132 { 133 if( bitfield ) 134 { 135 free( bitfield->bits ); 136 free( bitfield ); 137 } 138 } 139 140 static inline void tr_bitfieldClear( tr_bitfield_t * bitfield ) 141 { 142 memset( bitfield->bits, 0, bitfield->len ); 143 } 144 104 145 /*********************************************************************** 105 146 * tr_bitfieldHas 106 147 **********************************************************************/ 107 static inline int tr_bitfieldHas( uint8_t * bitfield, int piece ) 108 { 109 return ( bitfield[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) ); 148 static inline int tr_bitfieldHas( tr_bitfield_t * bitfield, int piece ) 149 { 150 assert( piece / 8 < bitfield->len ); 151 return ( bitfield->bits[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) ); 110 152 } 111 153 … … 113 155 * tr_bitfieldAdd 114 156 **********************************************************************/ 115 static inline void tr_bitfieldAdd( uint8_t * bitfield, int piece ) 116 { 117 bitfield[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) ); 118 } 119 120 static inline void tr_bitfieldRem( uint8_t * bitfield, int piece ) 121 { 122 bitfield[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) ); 157 static inline void tr_bitfieldAdd( tr_bitfield_t * bitfield, int piece ) 158 { 159 assert( piece / 8 < bitfield->len ); 160 bitfield->bits[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) ); 161 } 162 163 static inline void tr_bitfieldRem( tr_bitfield_t * bitfield, int piece ) 164 { 165 assert( piece / 8 < bitfield->len ); 166 bitfield->bits[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) ); 123 167 } 124 168
Note: See TracChangeset
for help on using the changeset viewer.