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 | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 19 | #include "allocator.h" |
| 20 | #include "bit_vector-inl.h" |
| 21 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 24 | // The number of words necessary to encode bits. |
| 25 | static constexpr uint32_t BitsToWords(uint32_t bits) { |
| 26 | return RoundUp(bits, 32) / 32; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // TODO: replace excessive argument defaulting when we are at gcc 4.7 |
| 30 | // or later on host with delegating constructor support. Specifically, |
| 31 | // starts_bits and storage_size/storage are mutually exclusive. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 32 | BitVector::BitVector(uint32_t start_bits, |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 33 | bool expandable, |
| 34 | Allocator* allocator, |
| 35 | uint32_t storage_size, |
| 36 | uint32_t* storage) |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 37 | : storage_(storage), |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 38 | storage_size_(storage_size), |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 39 | allocator_(allocator), |
| 40 | expandable_(expandable) { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 41 | COMPILE_ASSERT(sizeof(*storage_) == kWordBytes, check_word_bytes); |
| 42 | COMPILE_ASSERT(sizeof(*storage_) * 8u == kWordBits, check_word_bits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 43 | if (storage_ == nullptr) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 44 | storage_size_ = BitsToWords(start_bits); |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 45 | storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * kWordBytes)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | BitVector::~BitVector() { |
| 50 | allocator_->Free(storage_); |
| 51 | } |
| 52 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 53 | bool BitVector::SameBitsSet(const BitVector *src) const { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 54 | int our_highest = GetHighestBitSet(); |
| 55 | int src_highest = src->GetHighestBitSet(); |
| 56 | |
| 57 | // If the highest bit set is different, we are different. |
| 58 | if (our_highest != src_highest) { |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 59 | return false; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // If the highest bit set is -1, both are cleared, we are the same. |
| 63 | // 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] | 64 | if (our_highest <= 0) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 68 | // Get the highest bit set's cell's index |
| 69 | // No need of highest + 1 here because it can't be 0 so BitsToWords will work here. |
| 70 | int our_highest_index = BitsToWords(our_highest); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 71 | |
| 72 | // This memcmp is enough: we know that the highest bit set is the same for both: |
| 73 | // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less. |
| 74 | // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index, |
| 75 | // they are automatically at 0. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 76 | return (memcmp(storage_, src->GetRawStorage(), our_highest_index * kWordBytes) == 0); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 79 | void BitVector::Intersect(const BitVector* src) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 80 | uint32_t src_storage_size = src->storage_size_; |
| 81 | |
| 82 | // Get the minimum size between us and source. |
| 83 | uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size; |
| 84 | |
| 85 | uint32_t idx; |
| 86 | for (idx = 0; idx < min_size; idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 87 | storage_[idx] &= src->GetRawStorageWord(idx); |
| 88 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 89 | |
| 90 | // Now, due to this being an intersection, there are two possibilities: |
| 91 | // - Either src was larger than us: we don't care, all upper bits would thus be 0. |
| 92 | // - Either we are larger than src: we don't care, all upper bits would have been 0 too. |
| 93 | // So all we need to do is set all remaining bits to 0. |
| 94 | for (; idx < storage_size_; idx++) { |
| 95 | storage_[idx] = 0; |
| 96 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 99 | bool BitVector::Union(const BitVector* src) { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 100 | // Get the highest bit to determine how much we need to expand. |
| 101 | int highest_bit = src->GetHighestBitSet(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 102 | bool changed = false; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 103 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 104 | // If src has no bit set, we are done: there is no need for a union with src. |
| 105 | if (highest_bit == -1) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 106 | return changed; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Update src_size to how many cells we actually care about: where the bit is + 1. |
| 110 | uint32_t src_size = BitsToWords(highest_bit + 1); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 111 | |
| 112 | // Is the storage size smaller than src's? |
| 113 | if (storage_size_ < src_size) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 114 | changed = true; |
| 115 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 116 | EnsureSize(highest_bit); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 117 | |
| 118 | // Paranoid: storage size should be big enough to hold this bit now. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 119 | DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 122 | for (uint32_t idx = 0; idx < src_size; idx++) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 123 | uint32_t existing = storage_[idx]; |
| 124 | uint32_t update = existing | src->GetRawStorageWord(idx); |
| 125 | if (existing != update) { |
| 126 | changed = true; |
| 127 | storage_[idx] = update; |
| 128 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 129 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 130 | return changed; |
| 131 | } |
| 132 | |
| 133 | bool BitVector::UnionIfNotIn(const BitVector* union_with, const BitVector* not_in) { |
| 134 | // Get the highest bit to determine how much we need to expand. |
| 135 | int highest_bit = union_with->GetHighestBitSet(); |
| 136 | bool changed = false; |
| 137 | |
| 138 | // If src has no bit set, we are done: there is no need for a union with src. |
| 139 | if (highest_bit == -1) { |
| 140 | return changed; |
| 141 | } |
| 142 | |
| 143 | // Update union_with_size to how many cells we actually care about: where the bit is + 1. |
| 144 | uint32_t union_with_size = BitsToWords(highest_bit + 1); |
| 145 | |
| 146 | // Is the storage size smaller than src's? |
| 147 | if (storage_size_ < union_with_size) { |
| 148 | changed = true; |
| 149 | |
| 150 | // Set it to reallocate. |
| 151 | SetBit(highest_bit); |
| 152 | |
| 153 | // Paranoid: storage size should be big enough to hold this bit now. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 154 | DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | uint32_t not_in_size = not_in->GetStorageSize(); |
| 158 | |
| 159 | uint32_t idx = 0; |
| 160 | for (; idx < std::min(not_in_size, union_with_size); idx++) { |
| 161 | uint32_t existing = storage_[idx]; |
| 162 | uint32_t update = existing | |
| 163 | (union_with->GetRawStorageWord(idx) & ~not_in->GetRawStorageWord(idx)); |
| 164 | if (existing != update) { |
| 165 | changed = true; |
| 166 | storage_[idx] = update; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | for (; idx < union_with_size; idx++) { |
| 171 | uint32_t existing = storage_[idx]; |
| 172 | uint32_t update = existing | union_with->GetRawStorageWord(idx); |
| 173 | if (existing != update) { |
| 174 | changed = true; |
| 175 | storage_[idx] = update; |
| 176 | } |
| 177 | } |
| 178 | return changed; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 181 | void BitVector::Subtract(const BitVector *src) { |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 182 | uint32_t src_size = src->storage_size_; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 183 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 184 | // We only need to operate on bytes up to the smaller of the sizes of the two operands. |
| 185 | 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] | 186 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 187 | // Difference until max, we know both accept it: |
| 188 | // There is no need to do more: |
| 189 | // If we are bigger than src, the upper bits are unchanged. |
| 190 | // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted. |
| 191 | for (uint32_t idx = 0; idx < min_size; idx++) { |
| 192 | storage_[idx] &= (~(src->GetRawStorageWord(idx))); |
| 193 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 196 | uint32_t BitVector::NumSetBits() const { |
| 197 | uint32_t count = 0; |
| 198 | for (uint32_t word = 0; word < storage_size_; word++) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 199 | count += POPCOUNT(storage_[word]); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 200 | } |
| 201 | return count; |
| 202 | } |
| 203 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 204 | uint32_t BitVector::NumSetBits(uint32_t end) const { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 205 | DCHECK_LE(end, storage_size_ * kWordBits); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 206 | return NumSetBits(storage_, end); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 209 | void BitVector::SetInitialBits(uint32_t num_bits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 210 | // If num_bits is 0, clear everything. |
| 211 | if (num_bits == 0) { |
| 212 | ClearAllBits(); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | // Set the highest bit we want to set to get the BitVector allocated if need be. |
| 217 | SetBit(num_bits - 1); |
| 218 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 219 | uint32_t idx; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 220 | // We can set every storage element with -1. |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 221 | for (idx = 0; idx < WordIndex(num_bits); idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 222 | storage_[idx] = -1; |
| 223 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 224 | |
| 225 | // Handle the potentially last few bits. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 226 | uint32_t rem_num_bits = num_bits & 0x1f; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 227 | if (rem_num_bits != 0) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 228 | storage_[idx] = (1 << rem_num_bits) - 1; |
Vladimir Marko | 4812d43 | 2014-03-11 12:42:25 +0000 | [diff] [blame] | 229 | ++idx; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 230 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 231 | |
| 232 | // Now set the upper ones to 0. |
| 233 | for (; idx < storage_size_; idx++) { |
| 234 | storage_[idx] = 0; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | int BitVector::GetHighestBitSet() const { |
| 239 | unsigned int max = storage_size_; |
| 240 | for (int idx = max - 1; idx >= 0; idx--) { |
| 241 | // If not 0, we have more work: check the bits. |
| 242 | uint32_t value = storage_[idx]; |
| 243 | |
| 244 | if (value != 0) { |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 245 | // Return highest bit set in value plus bits from previous storage indexes. |
| 246 | return 31 - CLZ(value) + (idx * kWordBits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | // All zero, therefore return -1. |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | void BitVector::Copy(const BitVector *src) { |
| 255 | // Get highest bit set, we only need to copy till then. |
| 256 | int highest_bit = src->GetHighestBitSet(); |
| 257 | |
| 258 | // If nothing is set, clear everything. |
| 259 | if (highest_bit == -1) { |
| 260 | ClearAllBits(); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | // Set upper bit to ensure right size before copy. |
| 265 | SetBit(highest_bit); |
| 266 | |
| 267 | // Now set until highest bit's storage. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 268 | uint32_t size = 1 + (highest_bit / kWordBits); |
| 269 | memcpy(storage_, src->GetRawStorage(), kWordBytes * size); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 270 | |
| 271 | // Set upper bits to 0. |
| 272 | uint32_t left = storage_size_ - size; |
| 273 | |
| 274 | if (left > 0) { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 275 | memset(storage_ + size, 0, kWordBytes * left); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 276 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 279 | uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) { |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 280 | uint32_t word_end = WordIndex(end); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 281 | uint32_t partial_word_bits = end & 0x1f; |
| 282 | |
| 283 | uint32_t count = 0u; |
| 284 | for (uint32_t word = 0u; word < word_end; word++) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 285 | count += POPCOUNT(storage[word]); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 286 | } |
| 287 | if (partial_word_bits != 0u) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 288 | count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 289 | } |
| 290 | return count; |
| 291 | } |
| 292 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 293 | void BitVector::Dump(std::ostream& os, const char *prefix) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 294 | std::ostringstream buffer; |
Jean Christophe Beyler | 520f37b | 2014-05-22 15:43:50 -0700 | [diff] [blame] | 295 | DumpHelper(prefix, buffer); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 296 | os << buffer.str() << std::endl; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jean Christophe Beyler | 520f37b | 2014-05-22 15:43:50 -0700 | [diff] [blame] | 299 | void BitVector::DumpHelper(const char* prefix, std::ostringstream& buffer) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 300 | // Initialize it. |
| 301 | if (prefix != nullptr) { |
| 302 | buffer << prefix; |
| 303 | } |
| 304 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 305 | buffer << '('; |
Jean Christophe Beyler | 014d77a | 2014-06-02 11:21:21 -0700 | [diff] [blame] | 306 | for (size_t i = 0; i < storage_size_ * kWordBits; i++) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 307 | buffer << IsBitSet(i); |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 308 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 309 | buffer << ')'; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 312 | void BitVector::EnsureSize(uint32_t idx) { |
| 313 | if (idx >= storage_size_ * kWordBits) { |
| 314 | DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << idx; |
| 315 | |
| 316 | /* Round up to word boundaries for "idx+1" bits */ |
| 317 | uint32_t new_size = BitsToWords(idx + 1); |
| 318 | DCHECK_GT(new_size, storage_size_); |
| 319 | uint32_t *new_storage = |
| 320 | static_cast<uint32_t*>(allocator_->Alloc(new_size * kWordBytes)); |
| 321 | memcpy(new_storage, storage_, storage_size_ * kWordBytes); |
| 322 | // Zero out the new storage words. |
| 323 | memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * kWordBytes); |
| 324 | // TOTO: collect stats on space wasted because of resize. |
| 325 | storage_ = new_storage; |
| 326 | storage_size_ = new_size; |
| 327 | } |
| 328 | } |
| 329 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 330 | } // namespace art |