blob: 17835f56108c74029ea99593ea70629a36c249c7 [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -07001/*
2 * Copyright (C) 2013 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
Brian Carlstromba150c32013-08-27 17:31:03 -070017#ifndef ART_RUNTIME_BASE_BIT_VECTOR_H_
18#define ART_RUNTIME_BASE_BIT_VECTOR_H_
Brian Carlstrom413e89f2013-10-21 23:53:49 -070019
20#include <stdint.h>
Ian Rogerse77493c2014-08-20 15:08:45 -070021#include <iterator>
Brian Carlstrom413e89f2013-10-21 23:53:49 -070022
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/bit_utils.h"
Vladimir Marko83d46ef2015-05-12 18:27:20 +010024
Brian Carlstrom413e89f2013-10-21 23:53:49 -070025namespace art {
26
Ian Rogerse77493c2014-08-20 15:08:45 -070027class Allocator;
28
Brian Carlstrom413e89f2013-10-21 23:53:49 -070029/*
30 * Expanding bitmap, used for tracking resources. Bits are numbered starting
31 * from zero. All operations on a BitVector are unsynchronized.
32 */
33class BitVector {
Ian Rogerse77493c2014-08-20 15:08:45 -070034 public:
35 class IndexContainer;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010036
Ian Rogerse77493c2014-08-20 15:08:45 -070037 /**
38 * @brief Convenient iterator across the indexes of the BitVector's set bits.
39 *
40 * @details IndexIterator is a Forward iterator (C++11: 24.2.5) from the lowest
41 * to the highest index of the BitVector's set bits. Instances can be retrieved
42 * only through BitVector::Indexes() which returns an IndexContainer wrapper
43 * object with begin() and end() suitable for range-based loops:
44 * for (uint32_t idx : bit_vector.Indexes()) {
45 * // Use idx.
46 * }
47 */
48 class IndexIterator :
49 std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, uint32_t> {
50 public:
51 bool operator==(const IndexIterator& other) const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070052
Ian Rogerse77493c2014-08-20 15:08:45 -070053 bool operator!=(const IndexIterator& other) const {
54 return !(*this == other);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070055 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080056
Vladimir Marko04071962015-01-02 17:00:44 +000057 uint32_t operator*() const;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080058
Ian Rogerse77493c2014-08-20 15:08:45 -070059 IndexIterator& operator++();
Vladimir Markod3c5beb2014-04-11 16:32:51 +010060
Ian Rogerse77493c2014-08-20 15:08:45 -070061 IndexIterator operator++(int);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070062
Ian Rogerse77493c2014-08-20 15:08:45 -070063 // Helper function to check for end without comparing with bit_vector.Indexes().end().
64 bool Done() const {
65 return bit_index_ == BitSize();
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010066 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -070067
Ian Rogerse77493c2014-08-20 15:08:45 -070068 private:
69 struct begin_tag { };
70 struct end_tag { };
Brian Carlstrom413e89f2013-10-21 23:53:49 -070071
Ian Rogerse77493c2014-08-20 15:08:45 -070072 IndexIterator(const BitVector* bit_vector, begin_tag)
73 : bit_storage_(bit_vector->GetRawStorage()),
74 storage_size_(bit_vector->storage_size_),
75 bit_index_(FindIndex(0u)) { }
76
77 IndexIterator(const BitVector* bit_vector, end_tag)
78 : bit_storage_(bit_vector->GetRawStorage()),
79 storage_size_(bit_vector->storage_size_),
80 bit_index_(BitSize()) { }
81
82 uint32_t BitSize() const {
83 return storage_size_ * kWordBits;
84 }
85
86 uint32_t FindIndex(uint32_t start_index) const;
87 const uint32_t* const bit_storage_;
88 const uint32_t storage_size_; // Size of vector in words.
89 uint32_t bit_index_; // Current index (size in bits).
90
91 friend class BitVector::IndexContainer;
92 };
93
94 /**
95 * @brief BitVector wrapper class for iteration across indexes of set bits.
96 */
97 class IndexContainer {
98 public:
99 explicit IndexContainer(const BitVector* bit_vector) : bit_vector_(bit_vector) { }
100
101 IndexIterator begin() const {
102 return IndexIterator(bit_vector_, IndexIterator::begin_tag());
103 }
104
105 IndexIterator end() const {
106 return IndexIterator(bit_vector_, IndexIterator::end_tag());
107 }
108
109 private:
110 const BitVector* const bit_vector_;
111 };
112
113 BitVector(uint32_t start_bits,
114 bool expandable,
115 Allocator* allocator,
116 uint32_t storage_size = 0,
117 uint32_t* storage = nullptr);
118
119 virtual ~BitVector();
120
Vladimir Marko83d46ef2015-05-12 18:27:20 +0100121 // The number of words necessary to encode bits.
122 static constexpr uint32_t BitsToWords(uint32_t bits) {
123 return RoundUp(bits, kWordBits) / kWordBits;
124 }
125
Ian Rogerse77493c2014-08-20 15:08:45 -0700126 // Mark the specified bit as "set".
127 void SetBit(uint32_t idx) {
128 /*
129 * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're
130 * not using it badly or change resize mechanism.
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800131 */
Ian Rogerse77493c2014-08-20 15:08:45 -0700132 if (idx >= storage_size_ * kWordBits) {
133 EnsureSize(idx);
134 }
135 storage_[WordIndex(idx)] |= BitMask(idx);
136 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800137
Ian Rogerse77493c2014-08-20 15:08:45 -0700138 // Mark the specified bit as "unset".
139 void ClearBit(uint32_t idx) {
140 // If the index is over the size, we don't have to do anything, it is cleared.
141 if (idx < storage_size_ * kWordBits) {
142 // Otherwise, go ahead and clear it.
143 storage_[WordIndex(idx)] &= ~BitMask(idx);
144 }
145 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100146
Ian Rogerse77493c2014-08-20 15:08:45 -0700147 // Determine whether or not the specified bit is set.
148 bool IsBitSet(uint32_t idx) const {
149 // If the index is over the size, whether it is expandable or not, this bit does not exist:
150 // thus it is not set.
151 return (idx < (storage_size_ * kWordBits)) && IsBitSet(storage_, idx);
152 }
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700153
Ian Rogerse77493c2014-08-20 15:08:45 -0700154 // Mark all bits bit as "clear".
155 void ClearAllBits();
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700156
Ian Rogerse77493c2014-08-20 15:08:45 -0700157 // Mark specified number of bits as "set". Cannot set all bits like ClearAll since there might
158 // be unused bits - setting those to one will confuse the iterator.
159 void SetInitialBits(uint32_t num_bits);
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700160
Ian Rogerse77493c2014-08-20 15:08:45 -0700161 void Copy(const BitVector* src);
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700162
Ian Rogerse77493c2014-08-20 15:08:45 -0700163 // Intersect with another bit vector.
164 void Intersect(const BitVector* src2);
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700165
Ian Rogerse77493c2014-08-20 15:08:45 -0700166 // Union with another bit vector.
167 bool Union(const BitVector* src);
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700168
Ian Rogerse77493c2014-08-20 15:08:45 -0700169 // Set bits of union_with that are not in not_in.
170 bool UnionIfNotIn(const BitVector* union_with, const BitVector* not_in);
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700171
Ian Rogerse77493c2014-08-20 15:08:45 -0700172 void Subtract(const BitVector* src);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100173
Ian Rogerse77493c2014-08-20 15:08:45 -0700174 // Are we equal to another bit vector? Note: expandability attributes must also match.
175 bool Equal(const BitVector* src) const;
176
177 /**
178 * @brief Are all the bits set the same?
179 * @details expandability and size can differ as long as the same bits are set.
180 */
181 bool SameBitsSet(const BitVector *src) const;
182
David Brazdil7d275372015-04-21 16:36:35 +0100183 bool IsSubsetOf(const BitVector *other) const;
184
Ian Rogerse77493c2014-08-20 15:08:45 -0700185 // Count the number of bits that are set.
186 uint32_t NumSetBits() const;
187
188 // Count the number of bits that are set in range [0, end).
189 uint32_t NumSetBits(uint32_t end) const;
190
191 IndexContainer Indexes() const {
192 return IndexContainer(this);
193 }
194
195 uint32_t GetStorageSize() const {
196 return storage_size_;
197 }
198
199 bool IsExpandable() const {
200 return expandable_;
201 }
202
203 uint32_t GetRawStorageWord(size_t idx) const {
204 return storage_[idx];
205 }
206
207 uint32_t* GetRawStorage() {
208 return storage_;
209 }
210
211 const uint32_t* GetRawStorage() const {
212 return storage_;
213 }
214
215 size_t GetSizeOf() const {
216 return storage_size_ * kWordBytes;
217 }
218
219 /**
220 * @return the highest bit set, -1 if none are set
221 */
222 int GetHighestBitSet() const;
223
224 // Is bit set in storage. (No range check.)
225 static bool IsBitSet(const uint32_t* storage, uint32_t idx) {
226 return (storage[WordIndex(idx)] & BitMask(idx)) != 0;
227 }
228
229 // Number of bits set in range [0, end) in storage. (No range check.)
230 static uint32_t NumSetBits(const uint32_t* storage, uint32_t end);
231
232 void Dump(std::ostream& os, const char* prefix) const;
233
234 private:
235 /**
236 * @brief Dump the bitvector into buffer in a 00101..01 format.
237 * @param buffer the ostringstream used to dump the bitvector into.
238 */
239 void DumpHelper(const char* prefix, std::ostringstream& buffer) const;
240
241 // Ensure there is space for a bit at idx.
242 void EnsureSize(uint32_t idx);
243
244 // The index of the word within storage.
245 static constexpr uint32_t WordIndex(uint32_t idx) {
246 return idx >> 5;
247 }
248
249 // A bit mask to extract the bit for the given index.
250 static constexpr uint32_t BitMask(uint32_t idx) {
251 return 1 << (idx & 0x1f);
252 }
253
254 static constexpr uint32_t kWordBytes = sizeof(uint32_t);
255 static constexpr uint32_t kWordBits = kWordBytes * 8;
256
257 uint32_t* storage_; // The storage for the bit vector.
258 uint32_t storage_size_; // Current size, in 32-bit words.
259 Allocator* const allocator_; // Allocator if expandable.
260 const bool expandable_; // Should the bitmap expand if too small?
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700261};
262
263
264} // namespace art
265
Brian Carlstromba150c32013-08-27 17:31:03 -0700266#endif // ART_RUNTIME_BASE_BIT_VECTOR_H_