OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/bitmap.h" | 5 #include "net/disk_cache/bitmap.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 // Returns the number of trailing zeros. | 13 // Returns the number of trailing zeros. |
12 int FindLSBSetNonZero(uint32 word) { | 14 int FindLSBSetNonZero(uint32 word) { |
13 // Get the LSB, put it on the exponent of a 32 bit float and remove the | 15 // Get the LSB, put it on the exponent of a 32 bit float and remove the |
14 // mantisa and the bias. This code requires IEEE 32 bit float compliance. | 16 // mantisa and the bias. This code requires IEEE 32 bit float compliance. |
15 float f = static_cast<float>(word & -static_cast<int>(word)); | 17 float f = static_cast<float>(word & -static_cast<int>(word)); |
16 | 18 |
(...skipping 14 matching lines...) Expand all Loading... |
31 if (!value) | 33 if (!value) |
32 word = ~word; | 34 word = ~word; |
33 | 35 |
34 return FindLSBSetNonZero(word); | 36 return FindLSBSetNonZero(word); |
35 } | 37 } |
36 | 38 |
37 } | 39 } |
38 | 40 |
39 namespace disk_cache { | 41 namespace disk_cache { |
40 | 42 |
| 43 Bitmap::Bitmap(int num_bits, bool clear_bits) |
| 44 : num_bits_(num_bits), |
| 45 array_size_(RequiredArraySize(num_bits)), |
| 46 alloc_(true) { |
| 47 map_ = new uint32[array_size_]; |
| 48 |
| 49 // Initialize all of the bits. |
| 50 if (clear_bits) |
| 51 Clear(); |
| 52 } |
| 53 |
| 54 Bitmap::Bitmap(uint32* map, int num_bits, int num_words) |
| 55 : map_(map), |
| 56 num_bits_(num_bits), |
| 57 // If size is larger than necessary, trim because array_size_ is used |
| 58 // as a bound by various methods. |
| 59 array_size_(std::min(RequiredArraySize(num_bits), num_words)), |
| 60 alloc_(false) { |
| 61 } |
| 62 |
| 63 Bitmap::~Bitmap() { |
| 64 if (alloc_) |
| 65 delete [] map_; |
| 66 } |
| 67 |
41 void Bitmap::Resize(int num_bits, bool clear_bits) { | 68 void Bitmap::Resize(int num_bits, bool clear_bits) { |
42 DCHECK(alloc_ || !map_); | 69 DCHECK(alloc_ || !map_); |
43 const int old_maxsize = num_bits_; | 70 const int old_maxsize = num_bits_; |
44 const int old_array_size = array_size_; | 71 const int old_array_size = array_size_; |
45 array_size_ = RequiredArraySize(num_bits); | 72 array_size_ = RequiredArraySize(num_bits); |
46 | 73 |
47 if (array_size_ != old_array_size) { | 74 if (array_size_ != old_array_size) { |
48 uint32* new_map = new uint32[array_size_]; | 75 uint32* new_map = new uint32[array_size_]; |
49 // Always clear the unused bits in the last word. | 76 // Always clear the unused bits in the last word. |
50 new_map[array_size_ - 1] = 0; | 77 new_map[array_size_ - 1] = 0; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 uint32 Bitmap::GetMapElement(int array_index) const { | 125 uint32 Bitmap::GetMapElement(int array_index) const { |
99 DCHECK_LT(array_index, array_size_); | 126 DCHECK_LT(array_index, array_size_); |
100 DCHECK_GE(array_index, 0); | 127 DCHECK_GE(array_index, 0); |
101 return map_[array_index]; | 128 return map_[array_index]; |
102 } | 129 } |
103 | 130 |
104 void Bitmap::SetMap(const uint32* map, int size) { | 131 void Bitmap::SetMap(const uint32* map, int size) { |
105 memcpy(map_, map, std::min(size, array_size_) * sizeof(*map_)); | 132 memcpy(map_, map, std::min(size, array_size_) * sizeof(*map_)); |
106 } | 133 } |
107 | 134 |
108 void Bitmap::SetWordBits(int start, int len, bool value) { | |
109 DCHECK_LT(len, kIntBits); | |
110 DCHECK_GE(len, 0); | |
111 if (!len) | |
112 return; | |
113 | |
114 int word = start / kIntBits; | |
115 int offset = start % kIntBits; | |
116 | |
117 uint32 to_add = 0xffffffff << len; | |
118 to_add = (~to_add) << offset; | |
119 if (value) { | |
120 map_[word] |= to_add; | |
121 } else { | |
122 map_[word] &= ~to_add; | |
123 } | |
124 } | |
125 | |
126 void Bitmap::SetRange(int begin, int end, bool value) { | 135 void Bitmap::SetRange(int begin, int end, bool value) { |
127 DCHECK_LE(begin, end); | 136 DCHECK_LE(begin, end); |
128 int start_offset = begin & (kIntBits - 1); | 137 int start_offset = begin & (kIntBits - 1); |
129 if (start_offset) { | 138 if (start_offset) { |
130 // Set the bits in the first word. | 139 // Set the bits in the first word. |
131 int len = std::min(end - begin, kIntBits - start_offset); | 140 int len = std::min(end - begin, kIntBits - start_offset); |
132 SetWordBits(begin, len, value); | 141 SetWordBits(begin, len, value); |
133 begin += len; | 142 begin += len; |
134 } | 143 } |
135 | 144 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 return false; | 283 return false; |
275 | 284 |
276 // Now see how many bits have the same value. | 285 // Now see how many bits have the same value. |
277 int end = *index; | 286 int end = *index; |
278 if (!FindNextBit(&end, limit, !value)) | 287 if (!FindNextBit(&end, limit, !value)) |
279 return limit - *index; | 288 return limit - *index; |
280 | 289 |
281 return end - *index; | 290 return end - *index; |
282 } | 291 } |
283 | 292 |
| 293 void Bitmap::SetWordBits(int start, int len, bool value) { |
| 294 DCHECK_LT(len, kIntBits); |
| 295 DCHECK_GE(len, 0); |
| 296 if (!len) |
| 297 return; |
| 298 |
| 299 int word = start / kIntBits; |
| 300 int offset = start % kIntBits; |
| 301 |
| 302 uint32 to_add = 0xffffffff << len; |
| 303 to_add = (~to_add) << offset; |
| 304 if (value) { |
| 305 map_[word] |= to_add; |
| 306 } else { |
| 307 map_[word] &= ~to_add; |
| 308 } |
| 309 } |
| 310 |
284 } // namespace disk_cache | 311 } // namespace disk_cache |
OLD | NEW |