source: trunk/libtransmission/utils.h @ 3

Last change on this file since 3 was 3, checked in by root, 16 years ago

Update 2005-11-17

File size: 5.0 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
36/***********************************************************************
37 * tr_date
38 ***********************************************************************
39 * Returns the current date in milliseconds
40 **********************************************************************/
41static inline uint64_t tr_date()
42{
43    struct timeval tv;
44    gettimeofday( &tv, NULL );
45    return( (uint64_t) tv.tv_sec * 1000 + (uint64_t) tv.tv_usec / 1000 );
46}
47
48/***********************************************************************
49 * tr_wait
50 ***********************************************************************
51 * Wait 'delay' milliseconds
52 **********************************************************************/
53static inline void tr_wait( uint64_t delay )
54{
55#ifdef SYS_BEOS
56    snooze( 1000 * delay );
57#else
58    usleep( 1000 * delay );
59#endif
60}
61
62/***********************************************************************
63 * tr_bitfieldHas
64 **********************************************************************/
65static inline int tr_bitfieldHas( uint8_t * bitfield, int piece )
66{
67    return ( bitfield[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
68}
69
70/***********************************************************************
71 * tr_bitfieldAdd
72 **********************************************************************/
73static inline void tr_bitfieldAdd( uint8_t * bitfield, int piece )
74{
75    bitfield[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
76}
77
78static inline void tr_bitfieldRem( uint8_t * bitfield, int piece )
79{
80    bitfield[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
81}
82
83#define tr_blockPiece(a) _tr_blockPiece(tor,a)
84static inline int _tr_blockPiece( tr_torrent_t * tor, int block )
85{
86    tr_info_t * inf = &tor->info;
87    return block / ( inf->pieceSize / tor->blockSize );
88}
89
90#define tr_blockSize(a) _tr_blockSize(tor,a)
91static inline int _tr_blockSize( tr_torrent_t * tor, int block )
92{
93    tr_info_t * inf = &tor->info;
94    int dummy;
95
96    if( block != tor->blockCount - 1 ||
97        !( dummy = inf->totalSize % tor->blockSize ) )
98    {
99        return tor->blockSize;
100    }
101
102    return dummy;
103}
104
105#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
106static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block )
107{
108    tr_info_t * inf = &tor->info;
109    return tor->blockSize *
110        ( block % ( inf->pieceSize / tor->blockSize ) );
111}
112
113#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
114static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece )
115{
116    tr_info_t * inf = &tor->info;
117    if( piece < inf->pieceCount - 1 ||
118        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
119    {
120        return inf->pieceSize / tor->blockSize;
121    }
122    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
123}
124
125#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
126static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece )
127{
128    tr_info_t * inf = &tor->info;
129    return piece * ( inf->pieceSize / tor->blockSize );
130}
131
132#define tr_pieceSize(a) _tr_pieceSize(tor,a)
133static inline int _tr_pieceSize( tr_torrent_t * tor, int piece )
134{
135    tr_info_t * inf = &tor->info;
136    if( piece < inf->pieceCount - 1 ||
137        !( inf->totalSize % inf->pieceSize ) )
138    {
139        return inf->pieceSize;
140    }
141    return inf->totalSize % inf->pieceSize;
142}
143
144#define tr_block(a,b) _tr_block(tor,a,b)
145static inline int _tr_block( tr_torrent_t * tor, int index, int begin )
146{
147    tr_info_t * inf = &tor->info;
148    return index * ( inf->pieceSize / tor->blockSize ) +
149        begin / tor->blockSize;
150}
151
152#endif
Note: See TracBrowser for help on using the repository browser.