source: trunk/libtransmission/bitfield.h

Last change on this file was 14724, checked in by jordan, 5 years ago

use '#pragma once' instead of #ifndef..#define..#endif guards

  • Property svn:keywords set to Date Rev Author Id
File size: 2.1 KB
Line 
1/*
2 * This file Copyright (C) 2008-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: bitfield.h 14724 2016-03-29 16:37:21Z mikedld $
8 */
9
10#ifndef __TRANSMISSION__
11 #error only libtransmission should #include this header.
12#endif
13
14#pragma once
15
16#include "transmission.h"
17
18/** @brief Implementation of the BitTorrent spec's Bitfield array of bits */
19typedef struct tr_bitfield
20{
21  uint8_t *  bits;
22  size_t     alloc_count;
23
24  size_t     bit_count;
25
26  size_t     true_count;
27
28  /* Special cases for when full or empty but we don't know the bitCount.
29     This occurs when a magnet link's peers send have all / have none */
30  bool       have_all_hint;
31  bool       have_none_hint;
32}
33tr_bitfield;
34
35/***
36****
37***/
38
39void   tr_bitfieldSetHasAll  (tr_bitfield*);
40
41void   tr_bitfieldSetHasNone (tr_bitfield*);
42
43void   tr_bitfieldAdd        (tr_bitfield*, size_t bit);
44
45void   tr_bitfieldRem        (tr_bitfield*, size_t bit);
46
47void   tr_bitfieldAddRange   (tr_bitfield*, size_t begin, size_t end);
48
49void   tr_bitfieldRemRange   (tr_bitfield*, size_t begin, size_t end);
50
51/***
52****  life cycle
53***/
54
55extern const tr_bitfield TR_BITFIELD_INIT;
56
57void   tr_bitfieldConstruct (tr_bitfield*, size_t bit_count);
58
59static inline void
60tr_bitfieldDestruct (tr_bitfield * b)
61{
62  tr_bitfieldSetHasNone (b);
63}
64
65/***
66****
67***/
68
69void   tr_bitfieldSetFromFlags (tr_bitfield*, const bool * bytes, size_t n);
70
71void   tr_bitfieldSetFromBitfield (tr_bitfield*, const tr_bitfield*);
72
73void   tr_bitfieldSetRaw (tr_bitfield*, const void * bits, size_t byte_count, bool bounded);
74
75void*  tr_bitfieldGetRaw (const tr_bitfield * b, size_t * byte_count);
76
77/***
78****
79***/
80
81size_t  tr_bitfieldCountRange (const tr_bitfield*, size_t begin, size_t end);
82
83size_t  tr_bitfieldCountTrueBits (const tr_bitfield * b);
84
85static inline bool
86tr_bitfieldHasAll (const tr_bitfield * b)
87{
88  return b->bit_count ? (b->true_count == b->bit_count) : b->have_all_hint;
89}
90
91static inline bool
92tr_bitfieldHasNone (const tr_bitfield * b)
93{
94  return b->bit_count ? (b->true_count == 0) : b->have_none_hint;
95}
96
97bool tr_bitfieldHas (const tr_bitfield * b, size_t n);
98
Note: See TracBrowser for help on using the repository browser.