Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "bit_vector.h" |
| 18 | |
Ian Rogers | 5a2e4cc | 2014-10-30 15:13:22 -0700 | [diff] [blame] | 19 | #include <limits> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 20 | #include <sstream> |
| 21 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 22 | #include "allocator.h" |
| 23 | #include "bit_vector-inl.h" |
| 24 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Andreas Gampe | 067f1ed | 2015-08-07 08:29:13 -0700 | [diff] [blame] | 27 | BitVector::BitVector(bool expandable, |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 28 | Allocator* allocator, |
| 29 | uint32_t storage_size, |
| 30 | uint32_t* storage) |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 31 | : storage_(storage), |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 32 | storage_size_(storage_size), |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 33 | allocator_(allocator), |
| 34 | expandable_(expandable) { |
Andreas Gampe | 067f1ed | 2015-08-07 08:29:13 -0700 | [diff] [blame] | 35 | DCHECK(storage_ != nullptr); |
| 36 | |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 37 | static_assert(sizeof(*storage_) == kWordBytes, "word bytes"); |
| 38 | static_assert(sizeof(*storage_) * 8u == kWordBits, "word bits"); |
Andreas Gampe | 067f1ed | 2015-08-07 08:29:13 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | BitVector::BitVector(uint32_t start_bits, |
| 42 | bool expandable, |
| 43 | Allocator* allocator) |
| 44 | : BitVector(expandable, |
| 45 | allocator, |
| 46 | BitsToWords(start_bits), |
| 47 | static_cast<uint32_t*>(allocator->Alloc(BitsToWords(start_bits) * kWordBytes))) { |
| 48 | } |
| 49 | |
| 50 | |
| 51 | BitVector::BitVector(const BitVector& src, |
| 52 | bool expandable, |
| 53 | Allocator* allocator) |
| 54 | : BitVector(expandable, |
| 55 | allocator, |
| 56 | src.storage_size_, |
| 57 | static_cast<uint32_t*>(allocator->Alloc(src.storage_size_ * kWordBytes))) { |
| 58 | // Direct memcpy would be faster, but this should be fine too and is cleaner. |
| 59 | Copy(&src); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | BitVector::~BitVector() { |
| 63 | allocator_->Free(storage_); |
| 64 | } |
| 65 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 66 | bool BitVector::SameBitsSet(const BitVector *src) const { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 67 | int our_highest = GetHighestBitSet(); |
| 68 | int src_highest = src->GetHighestBitSet(); |
| 69 | |
| 70 | // If the highest bit set is different, we are different. |
| 71 | if (our_highest != src_highest) { |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 72 | return false; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // If the highest bit set is -1, both are cleared, we are the same. |
| 76 | // If the highest bit set is 0, both have a unique bit set, we are the same. |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 77 | if (our_highest <= 0) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 81 | // Get the highest bit set's cell's index |
| 82 | // No need of highest + 1 here because it can't be 0 so BitsToWords will work here. |
| 83 | int our_highest_index = BitsToWords(our_highest); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 84 | |
| 85 | // This memcmp is enough: we know that the highest bit set is the same for both: |
| 86 | // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less. |
| 87 | // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index, |
| 88 | // they are automatically at 0. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 89 | return (memcmp(storage_, src->GetRawStorage(), our_highest_index * kWordBytes) == 0); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 90 | } |
| 91 | |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 92 | bool BitVector::IsSubsetOf(const BitVector *other) const { |
| 93 | int this_highest = GetHighestBitSet(); |
| 94 | int other_highest = other->GetHighestBitSet(); |
| 95 | |
| 96 | // If the highest bit set is -1, this is empty and a trivial subset. |
| 97 | if (this_highest < 0) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // If the highest bit set is higher, this cannot be a subset. |
| 102 | if (this_highest > other_highest) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Compare each 32-bit word. |
| 107 | size_t this_highest_index = BitsToWords(this_highest + 1); |
| 108 | for (size_t i = 0; i < this_highest_index; ++i) { |
| 109 | uint32_t this_storage = storage_[i]; |
| 110 | uint32_t other_storage = other->storage_[i]; |
| 111 | if ((this_storage | other_storage) != other_storage) { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 118 | void BitVector::Intersect(const BitVector* src) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 119 | uint32_t src_storage_size = src->storage_size_; |
| 120 | |
| 121 | // Get the minimum size between us and source. |
| 122 | uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size; |
| 123 | |
| 124 | uint32_t idx; |
| 125 | for (idx = 0; idx < min_size; idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 126 | storage_[idx] &= src->GetRawStorageWord(idx); |
| 127 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 128 | |
| 129 | // Now, due to this being an intersection, there are two possibilities: |
| 130 | // - Either src was larger than us: we don't care, all upper bits would thus be 0. |
| 131 | // - Either we are larger than src: we don't care, all upper bits would have been 0 too. |
| 132 | // So all we need to do is set all remaining bits to 0. |
| 133 | for (; idx < storage_size_; idx++) { |
| 134 | storage_[idx] = 0; |
| 135 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 138 | bool BitVector::Union(const BitVector* src) { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 139 | // Get the highest bit to determine how much we need to expand. |
| 140 | int highest_bit = src->GetHighestBitSet(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 141 | bool changed = false; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 142 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 143 | // If src has no bit set, we are done: there is no need for a union with src. |
| 144 | if (highest_bit == -1) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 145 | return changed; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Update src_size to how many cells we actually care about: where the bit is + 1. |
| 149 | uint32_t src_size = BitsToWords(highest_bit + 1); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 150 | |
| 151 | // Is the storage size smaller than src's? |
| 152 | if (storage_size_ < src_size) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 153 | changed = true; |
| 154 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 155 | EnsureSize(highest_bit); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 156 | |
| 157 | // Paranoid: storage size should be big enough to hold this bit now. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 158 | DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 161 | for (uint32_t idx = 0; idx < src_size; idx++) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 162 | uint32_t existing = storage_[idx]; |
| 163 | uint32_t update = existing | src->GetRawStorageWord(idx); |
| 164 | if (existing != update) { |
| 165 | changed = true; |
| 166 | storage_[idx] = update; |
| 167 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 168 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 169 | return changed; |
| 170 | } |
| 171 | |
| 172 | bool BitVector::UnionIfNotIn(const BitVector* union_with, const BitVector* not_in) { |
| 173 | // Get the highest bit to determine how much we need to expand. |
| 174 | int highest_bit = union_with->GetHighestBitSet(); |
| 175 | bool changed = false; |
| 176 | |
| 177 | // If src has no bit set, we are done: there is no need for a union with src. |
| 178 | if (highest_bit == -1) { |
| 179 | return changed; |
| 180 | } |
| 181 | |
| 182 | // Update union_with_size to how many cells we actually care about: where the bit is + 1. |
| 183 | uint32_t union_with_size = BitsToWords(highest_bit + 1); |
| 184 | |
| 185 | // Is the storage size smaller than src's? |
| 186 | if (storage_size_ < union_with_size) { |
Nicolas Geoffray | b556761 | 2014-10-22 10:25:24 +0100 | [diff] [blame] | 187 | EnsureSize(highest_bit); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 188 | |
| 189 | // Paranoid: storage size should be big enough to hold this bit now. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 190 | DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | uint32_t not_in_size = not_in->GetStorageSize(); |
| 194 | |
| 195 | uint32_t idx = 0; |
| 196 | for (; idx < std::min(not_in_size, union_with_size); idx++) { |
| 197 | uint32_t existing = storage_[idx]; |
| 198 | uint32_t update = existing | |
| 199 | (union_with->GetRawStorageWord(idx) & ~not_in->GetRawStorageWord(idx)); |
| 200 | if (existing != update) { |
| 201 | changed = true; |
| 202 | storage_[idx] = update; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | for (; idx < union_with_size; idx++) { |
| 207 | uint32_t existing = storage_[idx]; |
| 208 | uint32_t update = existing | union_with->GetRawStorageWord(idx); |
| 209 | if (existing != update) { |
| 210 | changed = true; |
| 211 | storage_[idx] = update; |
| 212 | } |
| 213 | } |
| 214 | return changed; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 217 | void BitVector::Subtract(const BitVector *src) { |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 218 | uint32_t src_size = src->storage_size_; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 219 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 220 | // We only need to operate on bytes up to the smaller of the sizes of the two operands. |
| 221 | unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 222 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 223 | // Difference until max, we know both accept it: |
| 224 | // There is no need to do more: |
| 225 | // If we are bigger than src, the upper bits are unchanged. |
| 226 | // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted. |
| 227 | for (uint32_t idx = 0; idx < min_size; idx++) { |
| 228 | storage_[idx] &= (~(src->GetRawStorageWord(idx))); |
| 229 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 232 | uint32_t BitVector::NumSetBits() const { |
| 233 | uint32_t count = 0; |
| 234 | for (uint32_t word = 0; word < storage_size_; word++) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 235 | count += POPCOUNT(storage_[word]); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 236 | } |
| 237 | return count; |
| 238 | } |
| 239 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 240 | uint32_t BitVector::NumSetBits(uint32_t end) const { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 241 | DCHECK_LE(end, storage_size_ * kWordBits); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 242 | return NumSetBits(storage_, end); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 245 | void BitVector::SetInitialBits(uint32_t num_bits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 246 | // If num_bits is 0, clear everything. |
| 247 | if (num_bits == 0) { |
| 248 | ClearAllBits(); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | // Set the highest bit we want to set to get the BitVector allocated if need be. |
| 253 | SetBit(num_bits - 1); |
| 254 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 255 | uint32_t idx; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 256 | // We can set every storage element with -1. |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 257 | for (idx = 0; idx < WordIndex(num_bits); idx++) { |
Ian Rogers | 5a2e4cc | 2014-10-30 15:13:22 -0700 | [diff] [blame] | 258 | storage_[idx] = std::numeric_limits<uint32_t>::max(); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 259 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 260 | |
| 261 | // Handle the potentially last few bits. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 262 | uint32_t rem_num_bits = num_bits & 0x1f; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 263 | if (rem_num_bits != 0) { |
Ian Rogers | 5a2e4cc | 2014-10-30 15:13:22 -0700 | [diff] [blame] | 264 | storage_[idx] = (1U << rem_num_bits) - 1; |
Vladimir Marko | 4812d43 | 2014-03-11 12:42:25 +0000 | [diff] [blame] | 265 | ++idx; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 266 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 267 | |
| 268 | // Now set the upper ones to 0. |
| 269 | for (; idx < storage_size_; idx++) { |
| 270 | storage_[idx] = 0; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | int BitVector::GetHighestBitSet() const { |
| 275 | unsigned int max = storage_size_; |
| 276 | for (int idx = max - 1; idx >= 0; idx--) { |
| 277 | // If not 0, we have more work: check the bits. |
| 278 | uint32_t value = storage_[idx]; |
| 279 | |
| 280 | if (value != 0) { |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 281 | // Return highest bit set in value plus bits from previous storage indexes. |
| 282 | return 31 - CLZ(value) + (idx * kWordBits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | // All zero, therefore return -1. |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | void BitVector::Copy(const BitVector *src) { |
| 291 | // Get highest bit set, we only need to copy till then. |
| 292 | int highest_bit = src->GetHighestBitSet(); |
| 293 | |
| 294 | // If nothing is set, clear everything. |
| 295 | if (highest_bit == -1) { |
| 296 | ClearAllBits(); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | // Set upper bit to ensure right size before copy. |
| 301 | SetBit(highest_bit); |
| 302 | |
| 303 | // Now set until highest bit's storage. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 304 | uint32_t size = 1 + (highest_bit / kWordBits); |
| 305 | memcpy(storage_, src->GetRawStorage(), kWordBytes * size); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 306 | |
| 307 | // Set upper bits to 0. |
| 308 | uint32_t left = storage_size_ - size; |
| 309 | |
| 310 | if (left > 0) { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 311 | memset(storage_ + size, 0, kWordBytes * left); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 312 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 315 | uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) { |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 316 | uint32_t word_end = WordIndex(end); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 317 | uint32_t partial_word_bits = end & 0x1f; |
| 318 | |
| 319 | uint32_t count = 0u; |
| 320 | for (uint32_t word = 0u; word < word_end; word++) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 321 | count += POPCOUNT(storage[word]); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 322 | } |
| 323 | if (partial_word_bits != 0u) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 324 | count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 325 | } |
| 326 | return count; |
| 327 | } |
| 328 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 329 | void BitVector::Dump(std::ostream& os, const char *prefix) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 330 | std::ostringstream buffer; |
Jean Christophe Beyler | 520f37b | 2014-05-22 15:43:50 -0700 | [diff] [blame] | 331 | DumpHelper(prefix, buffer); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 332 | os << buffer.str() << std::endl; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jean Christophe Beyler | 520f37b | 2014-05-22 15:43:50 -0700 | [diff] [blame] | 335 | void BitVector::DumpHelper(const char* prefix, std::ostringstream& buffer) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 336 | // Initialize it. |
| 337 | if (prefix != nullptr) { |
| 338 | buffer << prefix; |
| 339 | } |
| 340 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 341 | buffer << '('; |
Jean Christophe Beyler | 014d77a | 2014-06-02 11:21:21 -0700 | [diff] [blame] | 342 | for (size_t i = 0; i < storage_size_ * kWordBits; i++) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 343 | buffer << IsBitSet(i); |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 344 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 345 | buffer << ')'; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 348 | void BitVector::EnsureSize(uint32_t idx) { |
| 349 | if (idx >= storage_size_ * kWordBits) { |
| 350 | DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << idx; |
| 351 | |
| 352 | /* Round up to word boundaries for "idx+1" bits */ |
| 353 | uint32_t new_size = BitsToWords(idx + 1); |
| 354 | DCHECK_GT(new_size, storage_size_); |
| 355 | uint32_t *new_storage = |
| 356 | static_cast<uint32_t*>(allocator_->Alloc(new_size * kWordBytes)); |
| 357 | memcpy(new_storage, storage_, storage_size_ * kWordBytes); |
| 358 | // Zero out the new storage words. |
| 359 | memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * kWordBytes); |
Andreas Gampe | dc8aa69 | 2014-10-24 21:32:07 -0700 | [diff] [blame] | 360 | // TODO: collect stats on space wasted because of resize. |
| 361 | |
| 362 | // Free old storage. |
| 363 | allocator_->Free(storage_); |
| 364 | |
| 365 | // Set fields. |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 366 | storage_ = new_storage; |
| 367 | storage_size_ = new_size; |
| 368 | } |
| 369 | } |
| 370 | |
Andreas Gampe | 067f1ed | 2015-08-07 08:29:13 -0700 | [diff] [blame] | 371 | Allocator* BitVector::GetAllocator() const { |
| 372 | return allocator_; |
| 373 | } |
| 374 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 375 | } // namespace art |