source: trunk/libtransmission/utils.h @ 1

Last change on this file since 1 was 1, checked in by root, 15 years ago

Import from 2005-10-26

File size: 4.9 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
78#define tr_blockPiece(a) _tr_blockPiece(tor,a)
79static inline int _tr_blockPiece( tr_torrent_t * tor, int block )
80{
81    tr_info_t * inf = &tor->info;
82    return block / ( inf->pieceSize / tor->blockSize );
83}
84
85#define tr_blockSize(a) _tr_blockSize(tor,a)
86static inline int _tr_blockSize( tr_torrent_t * tor, int block )
87{
88    tr_info_t * inf = &tor->info;
89    int dummy;
90
91    if( block != tor->blockCount - 1 ||
92        !( dummy = inf->totalSize % tor->blockSize ) )
93    {
94        return tor->blockSize;
95    }
96
97    return dummy;
98}
99
100#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
101static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block )
102{
103    tr_info_t * inf = &tor->info;
104    return tor->blockSize *
105        ( block % ( inf->pieceSize / tor->blockSize ) );
106}
107
108#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
109static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece )
110{
111    tr_info_t * inf = &tor->info;
112    if( piece < inf->pieceCount - 1 ||
113        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
114    {
115        return inf->pieceSize / tor->blockSize;
116    }
117    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
118}
119
120#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
121static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece )
122{
123    tr_info_t * inf = &tor->info;
124    return piece * ( inf->pieceSize / tor->blockSize );
125}
126
127#define tr_pieceSize(a) _tr_pieceSize(tor,a)
128static inline int _tr_pieceSize( tr_torrent_t * tor, int piece )
129{
130    tr_info_t * inf = &tor->info;
131    if( piece < inf->pieceCount - 1 ||
132        !( inf->totalSize % inf->pieceSize ) )
133    {
134        return inf->pieceSize;
135    }
136    return inf->totalSize % inf->pieceSize;
137}
138
139#define tr_block(a,b) _tr_block(tor,a,b)
140static inline int _tr_block( tr_torrent_t * tor, int index, int begin )
141{
142    tr_info_t * inf = &tor->info;
143    return index * ( inf->pieceSize / tor->blockSize ) +
144        begin / tor->blockSize;
145}
146
147#endif
Note: See TracBrowser for help on using the repository browser.