source: trunk/libtransmission/utils.h @ 2231

Last change on this file since 2231 was 2231, checked in by charles, 15 years ago

lots of performance improvements. fun!

  • dynamically resize the request queue on a per-peer basis based on its speed
  • fix a huge bug that sabotaged the `swift' performance
  • on startup, unchoke peers much sooner
  • bump MAX_PEERS and PERCENT_PEER_WANTED
  • do a better job of estimating speed on torrents less than 30 seconds old.
  • getting an unrecognized extension ID, ignore it instead of stopping the torrent.
  • Property svn:keywords set to Date Rev Author Id
File size: 7.6 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 2231 2007-06-29 05:45:17Z charles $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef TR_UTILS_H
26#define TR_UTILS_H 1
27
28void tr_msgInit( void );
29
30#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
31#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
32#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
33void tr_msg  ( int level, char * msg, ... ) PRINTF( 2, 3 );
34
35int  tr_rand ( int );
36
37void * tr_memmem( const void *, size_t, const void *, size_t );
38
39/***********************************************************************
40 * tr_mkdir
41 ***********************************************************************
42 * Create a directory and any needed parent directories.
43 * Note that the string passed in must be writable!
44 **********************************************************************/
45int tr_mkdir( char * path );
46
47/***********************************************************************
48 * tr_strcasecmp
49 ***********************************************************************
50 * A case-insensitive strncmp()
51 **********************************************************************/
52#define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), ULONG_MAX ) )
53int tr_strncasecmp( const char * first, const char * second, size_t len );
54
55/***********************************************************************
56 * tr_sprintf
57 ***********************************************************************
58 * Appends to the end of a buffer using printf formatting,
59 * growing the buffer if needed
60 **********************************************************************/
61int tr_sprintf( char ** buf, int * used, int * max,
62                const char * format, ... ) PRINTF( 4, 5 );
63/* gee, it sure would be nice if BeOS had va_copy() */
64int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
65/* this concatenates some binary data onto the end of a buffer */
66int tr_concat( char ** buf, int * used, int * max,
67               const char * data, int len );
68
69/* creates a filename from a series of elements using the
70   correct separator for filenames. */
71void tr_buildPath ( char* buf, size_t buflen,
72                    const char * first_element, ... );
73
74
75int    tr_ioErrorFromErrno( void );
76
77char * tr_errorString( int code );
78
79/***********************************************************************
80 * tr_date
81 ***********************************************************************
82 * Returns the current date in milliseconds
83 **********************************************************************/
84static inline uint64_t tr_date()
85{
86    struct timeval tv;
87    gettimeofday( &tv, NULL );
88    return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
89}
90
91/***********************************************************************
92 * tr_wait
93 ***********************************************************************
94 * Wait 'delay' milliseconds
95 **********************************************************************/
96static inline void tr_wait( uint64_t delay )
97{
98#ifdef SYS_BEOS
99    snooze( 1000 * delay );
100#else
101    usleep( 1000 * delay );
102#endif
103}
104
105
106#define tr_blockPiece(a) _tr_blockPiece(tor,a)
107static inline int _tr_blockPiece( const tr_torrent_t * tor, int block )
108{
109    const tr_info_t * inf = &tor->info;
110    return block / ( inf->pieceSize / tor->blockSize );
111}
112
113#define tr_blockSize(a) _tr_blockSize(tor,a)
114static inline int _tr_blockSize( const tr_torrent_t * tor, int block )
115{
116    const tr_info_t * inf = &tor->info;
117    int dummy;
118
119    if( block != tor->blockCount - 1 ||
120        !( dummy = inf->totalSize % tor->blockSize ) )
121    {
122        return tor->blockSize;
123    }
124
125    return dummy;
126}
127
128#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
129static inline int _tr_blockPosInPiece( const tr_torrent_t * tor, int block )
130{
131    const tr_info_t * inf = &tor->info;
132    return tor->blockSize *
133        ( block % ( inf->pieceSize / tor->blockSize ) );
134}
135
136#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
137static inline int _tr_pieceCountBlocks( const tr_torrent_t * tor, int piece )
138{
139    const tr_info_t * inf = &tor->info;
140    if( piece < inf->pieceCount - 1 ||
141        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
142    {
143        return inf->pieceSize / tor->blockSize;
144    }
145    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
146}
147
148#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
149static inline int _tr_pieceStartBlock( const tr_torrent_t * tor, int piece )
150{
151    const tr_info_t * inf = &tor->info;
152    return piece * ( inf->pieceSize / tor->blockSize );
153}
154
155#define tr_pieceSize(a) _tr_pieceSize(tor,a)
156static inline int _tr_pieceSize( const tr_torrent_t * tor, int piece )
157{
158    const tr_info_t * inf = &tor->info;
159    if( piece < inf->pieceCount - 1 ||
160        !( inf->totalSize % inf->pieceSize ) )
161    {
162        return inf->pieceSize;
163    }
164    return inf->totalSize % inf->pieceSize;
165}
166
167#define tr_block(a,b) _tr_block(tor,a,b)
168static inline int _tr_block( const tr_torrent_t * tor, int index, int begin )
169{
170    const tr_info_t * inf = &tor->info;
171    return index * ( inf->pieceSize / tor->blockSize ) +
172        begin / tor->blockSize;
173}
174
175/***
176****
177***/
178
179#define tr_new(struct_type, n_structs)           \
180    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
181#define tr_new0(struct_type, n_structs)          \
182    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
183#define tr_renew(struct_type, mem, n_structs)    \
184    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
185
186void* tr_malloc  ( size_t );
187void* tr_malloc0 ( size_t );
188void* tr_calloc  ( size_t nmemb, size_t size );
189void  tr_free    ( void* );
190
191char* tr_strdup( const char * str );
192char* tr_strndup( const char * str, int len );
193
194/***
195****
196***/
197
198struct tr_bitfield_s
199{
200    uint8_t * bits;
201    size_t len;
202};
203
204tr_bitfield_t* tr_bitfieldNew( size_t bitcount );
205tr_bitfield_t* tr_bitfieldDup( const tr_bitfield_t* );
206void tr_bitfieldFree( tr_bitfield_t*);
207
208void tr_bitfieldClear( tr_bitfield_t* );
209void tr_bitfieldAdd( tr_bitfield_t*, size_t bit );
210void tr_bitfieldRem( tr_bitfield_t*, size_t bit );
211void tr_bitfieldAddRange( tr_bitfield_t *, size_t first, size_t last );
212void tr_bitfieldRemRange ( tr_bitfield_t*, size_t first, size_t last );
213
214int    tr_bitfieldIsEmpty( const tr_bitfield_t* );
215int    tr_bitfieldHas( const tr_bitfield_t *, size_t bit );
216size_t tr_bitfieldCountTrueBits( const tr_bitfield_t* );
217
218tr_bitfield_t* tr_bitfieldNegate( tr_bitfield_t* );
219tr_bitfield_t* tr_bitfieldAnd( tr_bitfield_t*, const tr_bitfield_t* );
220
221
222
223#endif
Note: See TracBrowser for help on using the repository browser.