blob: 81a639a05011cbee44b39b5b2de3e4303779df1a [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -07001/*
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
19namespace art {
20
21// TODO: profile to make sure this is still a win relative to just using shifted masks.
22static uint32_t check_masks[32] = {
23 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010,
24 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200,
25 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000,
26 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000,
27 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000,
28 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
29 0x40000000, 0x80000000 };
30
31static inline uint32_t BitsToWords(unsigned int bits) {
32 return (bits + 31) >> 5;
33}
34
35// TODO: replace excessive argument defaulting when we are at gcc 4.7
36// or later on host with delegating constructor support. Specifically,
37// starts_bits and storage_size/storage are mutually exclusive.
38BitVector::BitVector(unsigned int start_bits,
39 bool expandable,
40 Allocator* allocator,
41 uint32_t storage_size,
42 uint32_t* storage)
43 : allocator_(allocator),
44 expandable_(expandable),
45 storage_size_(storage_size),
46 storage_(storage) {
47 DCHECK_EQ(sizeof(storage_[0]), 4U); // Assuming 32-bit units.
48 if (storage_ == NULL) {
49 storage_size_ = BitsToWords(start_bits);
50 storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * sizeof(uint32_t)));
51 }
52}
53
54BitVector::~BitVector() {
55 allocator_->Free(storage_);
56}
57
58/*
59 * Determine whether or not the specified bit is set.
60 */
61bool BitVector::IsBitSet(unsigned int num) {
62 DCHECK_LT(num, storage_size_ * sizeof(uint32_t) * 8);
63
64 unsigned int val = storage_[num >> 5] & check_masks[num & 0x1f];
65 return (val != 0);
66}
67
68// Mark all bits bit as "clear".
69void BitVector::ClearAllBits() {
70 memset(storage_, 0, storage_size_ * sizeof(uint32_t));
71}
72
73// Mark the specified bit as "set".
74/*
75 * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're
76 * not using it badly or change resize mechanism.
77 */
78void BitVector::SetBit(unsigned int num) {
79 if (num >= storage_size_ * sizeof(uint32_t) * 8) {
80 DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num;
81
82 /* Round up to word boundaries for "num+1" bits */
83 unsigned int new_size = BitsToWords(num + 1);
84 DCHECK_GT(new_size, storage_size_);
85 uint32_t *new_storage =
86 static_cast<uint32_t*>(allocator_->Alloc(new_size * sizeof(uint32_t)));
87 memcpy(new_storage, storage_, storage_size_ * sizeof(uint32_t));
88 // Zero out the new storage words.
89 memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * sizeof(uint32_t));
90 // TOTO: collect stats on space wasted because of resize.
91 storage_ = new_storage;
92 storage_size_ = new_size;
93 }
94
95 storage_[num >> 5] |= check_masks[num & 0x1f];
96}
97
98// Mark the specified bit as "unset".
99void BitVector::ClearBit(unsigned int num) {
100 DCHECK_LT(num, storage_size_ * sizeof(uint32_t) * 8);
101 storage_[num >> 5] &= ~check_masks[num & 0x1f];
102}
103
104// Intersect with another bit vector. Sizes and expandability must be the same.
105void BitVector::Intersect(const BitVector* src) {
106 DCHECK_EQ(storage_size_, src->GetStorageSize());
107 DCHECK_EQ(expandable_, src->IsExpandable());
108 for (unsigned int idx = 0; idx < storage_size_; idx++) {
109 storage_[idx] &= src->GetRawStorageWord(idx);
110 }
111}
112
113/*
114 * Union with another bit vector. Sizes and expandability must be the same.
115 */
116void BitVector::Union(const BitVector* src) {
117 DCHECK_EQ(storage_size_, src->GetStorageSize());
118 DCHECK_EQ(expandable_, src->IsExpandable());
119 for (unsigned int idx = 0; idx < storage_size_; idx++) {
120 storage_[idx] |= src->GetRawStorageWord(idx);
121 }
122}
123
124// Count the number of bits that are set.
125int BitVector::NumSetBits() {
126 unsigned int count = 0;
127
128 for (unsigned int word = 0; word < storage_size_; word++) {
129 count += __builtin_popcount(storage_[word]);
130 }
131 return count;
132}
133
134BitVector::Iterator* BitVector::GetIterator() {
135 return new (allocator_) Iterator(this);
136}
137
138/*
139 * Mark specified number of bits as "set". Cannot set all bits like ClearAll
140 * since there might be unused bits - setting those to one will confuse the
141 * iterator.
142 */
143void BitVector::SetInitialBits(unsigned int num_bits) {
144 DCHECK_LE(BitsToWords(num_bits), storage_size_);
145 unsigned int idx;
146 for (idx = 0; idx < (num_bits >> 5); idx++) {
147 storage_[idx] = -1;
148 }
149 unsigned int rem_num_bits = num_bits & 0x1f;
150 if (rem_num_bits) {
151 storage_[idx] = (1 << rem_num_bits) - 1;
152 }
153}
154
155} // namespace art