source: trunk/libtransmission/bitfield.h @ 12248

Last change on this file since 12248 was 12248, checked in by jordan, 11 years ago

(trunk libT) break the mac build and introduce new crashes.

This is partially to address #4145 "Downloads stuck at 100%" by refactoring the bitset, bitfield, and tr_completion; however, the ripple effect is larger than usual so things may get worse in the short term before getting better.

livings124: to fix the mac build, remove bitset.[ch] from xcode

  • Property svn:keywords set to Date Rev Author Id
File size: 3.1 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: bitfield.h 12248 2011-03-28 16:31:05Z jordan $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_BITFIELD_H
18#define TR_BITFIELD_H 1
19
20#include <assert.h>
21#include "transmission.h"
22
23/** @brief Implementation of the BitTorrent spec's Bitfield array of bits */
24typedef struct tr_bitfield
25{
26    uint8_t *  bits;
27    size_t     bit_count;
28    size_t     byte_count;
29    size_t     true_count;
30
31    /* Special cases for when full or empty but we don't know the bitCount.
32       This occurs when a magnet link's peers send have all / have none */
33    bool       have_all_hint;
34    bool       have_none_hint;
35}
36tr_bitfield;
37
38/***
39****  life cycle
40***/
41
42extern const tr_bitfield TR_BITFIELD_INIT;
43
44void   tr_bitfieldConstruct( tr_bitfield*, size_t bit_count );
45
46void   tr_bitfieldDestruct( tr_bitfield* );
47
48/***
49****
50***/
51
52void   tr_bitfieldSetHasAll( tr_bitfield* );
53
54void   tr_bitfieldSetHasNone( tr_bitfield* );
55
56void   tr_bitfieldAdd( tr_bitfield*, size_t bit );
57
58void   tr_bitfieldRem( tr_bitfield*, size_t bit );
59
60void   tr_bitfieldAddRange( tr_bitfield*, size_t begin, size_t end );
61
62void   tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
63
64/***
65****
66***/
67
68bool   tr_bitfieldSetFromBitfield( tr_bitfield*, const tr_bitfield* );
69
70bool   tr_bitfieldSetRaw( tr_bitfield*, const void * bits, size_t byte_count );
71
72void*  tr_bitfieldGetRaw( const tr_bitfield * b, size_t * byte_count );
73
74/***
75****
76***/
77
78size_t  tr_bitfieldCountRange( const tr_bitfield*, size_t begin, size_t end );
79
80static inline size_t
81tr_bitfieldCountTrueBits( const tr_bitfield * b )
82{
83    assert( b->true_count == tr_bitfieldCountRange( b, 0, b->bit_count ) );
84    return b->true_count;
85}
86
87static inline bool
88tr_bitfieldHasAll( const tr_bitfield * b )
89{
90    return b->bit_count ? ( b->true_count == b->bit_count ) : b->have_all_hint;
91}
92
93static inline bool
94tr_bitfieldHasNone( const tr_bitfield * b )
95{
96    return b->bit_count ? ( b->true_count == 0 ) : b->have_none_hint;
97}
98
99/** A stripped-down version of bitfieldHas to be used
100    for speed when you're looping quickly. This version
101    has none of tr_bitfieldHas()'s safety checks, so you
102    need to call tr_bitfieldTestFast() first before you
103    start looping. */
104static inline bool
105tr_bitfieldHasFast( const tr_bitfield * b, const size_t n )
106{
107    if( tr_bitfieldHasAll( b ) ) return true;
108    if( tr_bitfieldHasNone( b ) ) return false;
109    return ( b->bits[n>>3u] << ( n & 7u ) & 0x80 ) != 0;
110}
111
112/** @param high the highest nth bit you're going to access */
113static inline bool
114tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
115{
116    return ( b != NULL )
117        && ( high < b->bit_count );
118}
119
120static inline bool
121tr_bitfieldHas( const tr_bitfield * b, size_t n )
122{
123    return tr_bitfieldTestFast( b, n )
124        && tr_bitfieldHasFast( b, n );
125}
126
127#endif
Note: See TracBrowser for help on using the repository browser.