blob: 74bec087a3365349ef3e0de7673b709e5efe6ae4 [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
49 if (UNLIKELY(bit_index_ >= bit_size_)) return -1;
50
51 uint32_t word_index = bit_index_ / 32;
52 uint32_t word = bit_storage_[word_index];
53 // Mask out any bits in the first word we've already considered.
54 word >>= bit_index_ & 0x1f;
55 if (word == 0) {
56 bit_index_ &= ~0x1f;
57 do {
58 word_index++;
59 if (UNLIKELY((word_index * 32) >= bit_size_)) {
60 bit_index_ = bit_size_;
61 return -1;
62 }
63 word = bit_storage_[word_index];
64 bit_index_ += 32;
65 } while (word == 0);
66 }
67 bit_index_ += CTZ(word) + 1;
68 return bit_index_ - 1;
69 }
70
71 static void* operator new(size_t size, Allocator* allocator) {
72 return allocator->Alloc(sizeof(BitVector::Iterator));
73 };
74 static void operator delete(void* p) {
75 Iterator* it = reinterpret_cast<Iterator*>(p);
76 it->p_bits_->allocator_->Free(p);
77 }
78
79 private:
Brian Carlstromba150c32013-08-27 17:31:03 -070080 const BitVector* const p_bits_;
81 const uint32_t* const bit_storage_;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070082 uint32_t bit_index_; // Current index (size in bits).
83 const uint32_t bit_size_; // Size of vector in bits.
84
85 friend class BitVector;
86 };
87
88 BitVector(uint32_t start_bits,
89 bool expandable,
90 Allocator* allocator,
91 uint32_t storage_size = 0,
92 uint32_t* storage = NULL);
93
94 virtual ~BitVector();
95
96 void SetBit(uint32_t num);
97 void ClearBit(uint32_t num);
Brian Carlstromba150c32013-08-27 17:31:03 -070098 bool IsBitSet(uint32_t num) const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070099 void ClearAllBits();
100 void SetInitialBits(uint32_t num_bits);
101 void Copy(BitVector* src) {
102 memcpy(storage_, src->GetRawStorage(), sizeof(uint32_t) * storage_size_);
103 }
104 void Intersect(const BitVector* src2);
105 void Union(const BitVector* src);
106 // Are we equal to another bit vector? Note: expandability attributes must also match.
107 bool Equal(const BitVector* src) {
108 return (storage_size_ == src->GetStorageSize()) &&
109 (expandable_ == src->IsExpandable()) &&
110 (memcmp(storage_, src->GetRawStorage(), storage_size_ * sizeof(uint32_t)) == 0);
111 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700112 uint32_t NumSetBits() const;
113 uint32_t NumSetBits(uint32_t num) const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700114
Brian Carlstromba150c32013-08-27 17:31:03 -0700115 Iterator* GetIterator() const;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700116
117 uint32_t GetStorageSize() const { return storage_size_; }
118 bool IsExpandable() const { return expandable_; }
119 uint32_t GetRawStorageWord(size_t idx) const { return storage_[idx]; }
120 uint32_t* GetRawStorage() { return storage_; }
121 const uint32_t* GetRawStorage() const { return storage_; }
Brian Carlstromba150c32013-08-27 17:31:03 -0700122 size_t GetSizeOf() const { return storage_size_ * sizeof(uint32_t); }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700123
124 private:
125 Allocator* const allocator_;
126 const bool expandable_; // expand bitmap if we run out?
127 uint32_t storage_size_; // current size, in 32-bit words.
128 uint32_t* storage_;
129};
130
131
132} // namespace art
133
Brian Carlstromba150c32013-08-27 17:31:03 -0700134#endif // ART_RUNTIME_BASE_BIT_VECTOR_H_