blob: a496dbd4d413808b3e698e3775c31ac875c3e8f9 [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>
21#include <stddef.h>
22
23#include "allocator.h"
24#include "base/logging.h"
25#include "utils.h"
26
27namespace art {
28
29/*
30 * Expanding bitmap, used for tracking resources. Bits are numbered starting
31 * from zero. All operations on a BitVector are unsynchronized.
32 */
33class BitVector {
34 public:
35 class Iterator {
36 public:
Brian Carlstromba150c32013-08-27 17:31:03 -070037 explicit Iterator(const BitVector* bit_vector)
Brian Carlstrom413e89f2013-10-21 23:53:49 -070038 : p_bits_(bit_vector),
39 bit_storage_(bit_vector->GetRawStorage()),
40 bit_index_(0),
41 bit_size_(p_bits_->storage_size_ * sizeof(uint32_t) * 8) {}
42
43 // Return the position of the next set bit. -1 means end-of-element reached.
44 int32_t Next() {
45 // Did anything obviously change since we started?
46 DCHECK_EQ(bit_size_, p_bits_->GetStorageSize() * sizeof(uint32_t) * 8);
47 DCHECK_EQ(bit_storage_, p_bits_->GetRawStorage());
48
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080049 if (UNLIKELY(bit_index_ >= bit_size_)) {
50 return -1;
51 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -070052
53 uint32_t word_index = bit_index_ / 32;
54 uint32_t word = bit_storage_[word_index];
55 // Mask out any bits in the first word we've already considered.
56 word >>= bit_index_ & 0x1f;
57 if (word == 0) {
58 bit_index_ &= ~0x1f;
59 do {
60 word_index++;
61 if (UNLIKELY((word_index * 32) >= bit_size_)) {
62 bit_index_ = bit_size_;
63 return -1;
64 }
65 word = bit_storage_[word_index];
66 bit_index_ += 32;
67 } while (word == 0);
68 }
69 bit_index_ += CTZ(word) + 1;
70 return bit_index_ - 1;
71 }
72
73 static void* operator new(size_t size, Allocator* allocator) {
74 return allocator->Alloc(sizeof(BitVector::Iterator));
75 };
76 static void operator delete(void* p) {
77 Iterator* it = reinterpret_cast<Iterator*>(p);
78 it->p_bits_->allocator_->Free(p);
79 }
80
81 private:
Brian Carlstromba150c32013-08-27 17:31:03 -070082 const BitVector* const p_bits_;
83 const uint32_t* const bit_storage_;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070084 uint32_t bit_index_; // Current index (size in bits).
85 const uint32_t bit_size_; // Size of vector in bits.
86
87 friend class BitVector;
88 };
89
90 BitVector(uint32_t start_bits,
91 bool expandable,
92 Allocator* allocator,
93 uint32_t storage_size = 0,
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080094 uint32_t* storage = nullptr);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070095
96 virtual ~BitVector();
97
98 void SetBit(uint32_t num);
99 void ClearBit(uint32_t num);
Brian Carlstromba150c32013-08-27 17:31:03 -0700100 bool IsBitSet(uint32_t num) const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700101 void ClearAllBits();
102 void SetInitialBits(uint32_t num_bits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800103
104 void Copy(const BitVector* src);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700105 void Intersect(const BitVector* src2);
106 void Union(const BitVector* src);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800107 void Subtract(const BitVector* src);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700108 // Are we equal to another bit vector? Note: expandability attributes must also match.
109 bool Equal(const BitVector* src) {
110 return (storage_size_ == src->GetStorageSize()) &&
111 (expandable_ == src->IsExpandable()) &&
112 (memcmp(storage_, src->GetRawStorage(), storage_size_ * sizeof(uint32_t)) == 0);
113 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800114
115 /**
116 * @brief Are all the bits set the same?
117 * @details expandability and size can differ as long as the same bits are set.
118 */
119 bool SameBitsSet(const BitVector *src);
120
Brian Carlstromba150c32013-08-27 17:31:03 -0700121 uint32_t NumSetBits() const;
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100122
123 // Number of bits set in range [0, end).
124 uint32_t NumSetBits(uint32_t end) const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700125
Brian Carlstromba150c32013-08-27 17:31:03 -0700126 Iterator* GetIterator() const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700127
128 uint32_t GetStorageSize() const { return storage_size_; }
129 bool IsExpandable() const { return expandable_; }
130 uint32_t GetRawStorageWord(size_t idx) const { return storage_[idx]; }
131 uint32_t* GetRawStorage() { return storage_; }
132 const uint32_t* GetRawStorage() const { return storage_; }
Brian Carlstromba150c32013-08-27 17:31:03 -0700133 size_t GetSizeOf() const { return storage_size_ * sizeof(uint32_t); }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700134
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800135 /**
136 * @return the highest bit set, -1 if none are set
137 */
138 int GetHighestBitSet() const;
139
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100140 // Is bit set in storage. (No range check.)
141 static bool IsBitSet(const uint32_t* storage, uint32_t num);
142 // Number of bits set in range [0, end) in storage. (No range check.)
143 static uint32_t NumSetBits(const uint32_t* storage, uint32_t end);
144
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700145 private:
146 Allocator* const allocator_;
147 const bool expandable_; // expand bitmap if we run out?
148 uint32_t storage_size_; // current size, in 32-bit words.
149 uint32_t* storage_;
150};
151
152
153} // namespace art
154
Brian Carlstromba150c32013-08-27 17:31:03 -0700155#endif // ART_RUNTIME_BASE_BIT_VECTOR_H_