source: trunk/libtransmission/utils.h @ 230

Last change on this file since 230 was 230, checked in by titer, 16 years ago

Merged simple_http_parsing branch

File size: 5.1 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005 Eric Petit
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#ifndef TR_UTILS_H
24#define TR_UTILS_H 1
25
26#define TR_MSG_ERR 1
27#define TR_MSG_INF 2
28#define TR_MSG_DBG 4
29#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
30#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
31#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
32void tr_msg  ( int level, char * msg, ... );
33
34int  tr_rand ( int );
35
36void * tr_memmem( const void *, size_t, const void *, size_t );
37
38/***********************************************************************
39 * tr_date
40 ***********************************************************************
41 * Returns the current date in milliseconds
42 **********************************************************************/
43static inline uint64_t tr_date()
44{
45    struct timeval tv;
46    gettimeofday( &tv, NULL );
47    return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
48}
49
50/***********************************************************************
51 * tr_wait
52 ***********************************************************************
53 * Wait 'delay' milliseconds
54 **********************************************************************/
55static inline void tr_wait( uint64_t delay )
56{
57#ifdef SYS_BEOS
58    snooze( 1000 * delay );
59#else
60    usleep( 1000 * delay );
61#endif
62}
63
64/***********************************************************************
65 * tr_bitfieldHas
66 **********************************************************************/
67static inline int tr_bitfieldHas( uint8_t * bitfield, int piece )
68{
69    return ( bitfield[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
70}
71
72/***********************************************************************
73 * tr_bitfieldAdd
74 **********************************************************************/
75static inline void tr_bitfieldAdd( uint8_t * bitfield, int piece )
76{
77    bitfield[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
78}
79
80static inline void tr_bitfieldRem( uint8_t * bitfield, int piece )
81{
82    bitfield[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
83}
84
85#define tr_blockPiece(a) _tr_blockPiece(tor,a)
86static inline int _tr_blockPiece( tr_torrent_t * tor, int block )
87{
88    tr_info_t * inf = &tor->info;
89    return block / ( inf->pieceSize / tor->blockSize );
90}
91
92#define tr_blockSize(a) _tr_blockSize(tor,a)
93static inline int _tr_blockSize( tr_torrent_t * tor, int block )
94{
95    tr_info_t * inf = &tor->info;
96    int dummy;
97
98    if( block != tor->blockCount - 1 ||
99        !( dummy = inf->totalSize % tor->blockSize ) )
100    {
101        return tor->blockSize;
102    }
103
104    return dummy;
105}
106
107#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
108static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block )
109{
110    tr_info_t * inf = &tor->info;
111    return tor->blockSize *
112        ( block % ( inf->pieceSize / tor->blockSize ) );
113}
114
115#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
116static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece )
117{
118    tr_info_t * inf = &tor->info;
119    if( piece < inf->pieceCount - 1 ||
120        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
121    {
122        return inf->pieceSize / tor->blockSize;
123    }
124    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
125}
126
127#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
128static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece )
129{
130    tr_info_t * inf = &tor->info;
131    return piece * ( inf->pieceSize / tor->blockSize );
132}
133
134#define tr_pieceSize(a) _tr_pieceSize(tor,a)
135static inline int _tr_pieceSize( tr_torrent_t * tor, int piece )
136{
137    tr_info_t * inf = &tor->info;
138    if( piece < inf->pieceCount - 1 ||
139        !( inf->totalSize % inf->pieceSize ) )
140    {
141        return inf->pieceSize;
142    }
143    return inf->totalSize % inf->pieceSize;
144}
145
146#define tr_block(a,b) _tr_block(tor,a,b)
147static inline int _tr_block( tr_torrent_t * tor, int index, int begin )
148{
149    tr_info_t * inf = &tor->info;
150    return index * ( inf->pieceSize / tor->blockSize ) +
151        begin / tor->blockSize;
152}
153
154#endif
Note: See TracBrowser for help on using the repository browser.