source: trunk/libtransmission/bitfield.h @ 11280

Last change on this file since 11280 was 11280, checked in by charles, 11 years ago

(trunk) fix svn properties on several files. Thanks ot Elbandi for suggesting this

  • Property svn:keywords set to Date Rev Author Id
File size: 2.5 KB
Line 
1/*
2 * This file Copyright (C) 2009-2010 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 11280 2010-10-01 13:33:39Z charles $
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 "transmission.h"
21#include "utils.h" /* tr_new0 */
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     bitCount;
28    size_t     byteCount;
29}
30tr_bitfield;
31
32tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
33
34tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );
35
36static inline tr_bitfield* tr_bitfieldNew( size_t bitcount )
37{
38    return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount );
39}
40
41static inline void tr_bitfieldFree( tr_bitfield * b )
42{
43    tr_free( tr_bitfieldDestruct( b ) );
44}
45
46tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
47
48void         tr_bitfieldClear( tr_bitfield* );
49
50int          tr_bitfieldAdd( tr_bitfield*, size_t bit );
51
52int          tr_bitfieldRem( tr_bitfield*, size_t bit );
53
54int          tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
55
56int          tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
57
58void         tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
59
60int          tr_bitfieldIsEmpty( const tr_bitfield* );
61
62size_t       tr_bitfieldCountTrueBits( const tr_bitfield* );
63
64tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
65
66/** A stripped-down version of bitfieldHas to be used
67    for speed when you're looping quickly.  This version
68    has none of tr_bitfieldHas()'s safety checks, so you
69    need to call tr_bitfieldTestFast() first before you
70    start looping. */
71static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )
72{
73    return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;
74}
75
76/** @param high the highest nth bit you're going to access */
77static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
78{
79    return ( b != NULL )
80        && ( b->bits != NULL )
81        && ( high < b->bitCount );
82}
83
84static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )
85{
86    return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );
87}
88
89#endif
Note: See TracBrowser for help on using the repository browser.