~ubuntu-branches/debian/wheezy/libtorrent/wheezy

« back to all changes in this revision

Viewing changes to src/torrent/bitfield.cc

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-09 20:04:41 UTC
  • mfrom: (1.3.4 upstream) (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110309200441-ffi9aaqdyd72d8xl
Tags: 0.12.7-4
* Steal patch from upstream's bug tracking system to enable IPv6.
* Refresh patches to apply cleanly.
* This should, hopefully, be a good step towards addressing #490277.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#include <algorithm>
 
40
 
 
41
#include "bitfield.h"
 
42
#include "exceptions.h"
 
43
 
 
44
namespace torrent {
 
45
 
 
46
// *sigh*
 
47
static const unsigned char bit_count_256[] = 
 
48
{
 
49
  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 
 
50
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
 
51
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
 
52
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
 
53
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
 
54
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
 
55
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
 
56
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
 
57
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
 
58
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
 
59
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
 
60
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
 
61
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
 
62
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
 
63
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
 
64
  4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
 
65
};
 
66
 
 
67
void
 
68
Bitfield::set_size_bits(size_type s) {
 
69
  if (m_data != NULL)
 
70
    throw internal_error("Bitfield::set_size_bits(size_type s) m_data != NULL.");
 
71
 
 
72
  m_size = s;
 
73
}
 
74
 
 
75
void
 
76
Bitfield::set_size_set(size_type s) {
 
77
  if (s > m_size)
 
78
    throw internal_error("Bitfield::set_size_set(size_type s) s > m_size.");
 
79
 
 
80
  m_set = s;
 
81
}
 
82
 
 
83
void
 
84
Bitfield::update() {
 
85
  // Clears the unused bits.
 
86
  clear_tail();
 
87
 
 
88
  m_set = 0;
 
89
 
 
90
  // Some archs have bitcounting instructions, look into writing a
 
91
  // wrapper for those.
 
92
  for (iterator itr = m_data, last = end(); itr != last; ++itr)
 
93
    m_set += bit_count_256[*itr];
 
94
}
 
95
 
 
96
void
 
97
Bitfield::copy(const Bitfield& bf) {
 
98
  m_size = bf.m_size;
 
99
  m_set = bf.m_set;
 
100
 
 
101
  if (bf.m_data == NULL) {
 
102
    m_data = NULL;
 
103
 
 
104
  } else {
 
105
    m_data = new value_type[size_bytes()];
 
106
 
 
107
    std::memcpy(m_data, bf.m_data, size_bytes());
 
108
  }
 
109
}
 
110
 
 
111
void
 
112
Bitfield::swap(Bitfield& bf) {
 
113
  std::swap(m_size, bf.m_size);
 
114
  std::swap(m_set, bf.m_set);
 
115
  std::swap(m_data, bf.m_data);
 
116
}
 
117
 
 
118
void
 
119
Bitfield::set_all() {
 
120
  m_set = m_size;
 
121
 
 
122
  std::memset(m_data, ~value_type(), size_bytes());
 
123
  clear_tail();
 
124
}
 
125
 
 
126
void
 
127
Bitfield::unset_all() {
 
128
  m_set = 0;
 
129
 
 
130
  std::memset(m_data, value_type(), size_bytes());
 
131
}
 
132
 
 
133
// Quick hack. Speed improvements would require that m_set is kept
 
134
// up-to-date.
 
135
void
 
136
Bitfield::set_range(size_type first, size_type last) {
 
137
  while (first != last)
 
138
    set(first++);
 
139
}
 
140
 
 
141
void
 
142
Bitfield::unset_range(size_type first, size_type last) {
 
143
  while (first != last)
 
144
    unset(first++);
 
145
}
 
146
 
 
147
}