blob: 2a05b7709221e0db31aac5d8e79d4d40ad995a24 [file] [log] [blame]
buzbee862a7602013-04-05 10:58:54 -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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_ARENA_BIT_VECTOR_H_
18#define ART_COMPILER_DEX_ARENA_BIT_VECTOR_H_
buzbee862a7602013-04-05 10:58:54 -070019
20#include <stdint.h>
21#include <stddef.h>
22#include "compiler_enums.h"
23#include "arena_allocator.h"
24
25namespace art {
26
buzbee862a7602013-04-05 10:58:54 -070027/*
28 * Expanding bitmap, used for tracking resources. Bits are numbered starting
29 * from zero. All operations on a BitVector are unsynchronized.
30 */
31class ArenaBitVector {
32 public:
buzbee862a7602013-04-05 10:58:54 -070033 class Iterator {
34 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070035 explicit Iterator(ArenaBitVector* bit_vector)
buzbee862a7602013-04-05 10:58:54 -070036 : p_bits_(bit_vector),
37 bit_storage_(bit_vector->GetRawStorage()),
38 bit_index_(0),
39 bit_size_(p_bits_->storage_size_ * sizeof(uint32_t) * 8) {};
40
Ian Rogers48ae54e2013-06-03 16:59:58 -070041 // Return the position of the next set bit. -1 means end-of-element reached.
42 int Next() {
43 // Did anything obviously change since we started?
44 DCHECK_EQ(bit_size_, p_bits_->GetStorageSize() * sizeof(uint32_t) * 8);
45 DCHECK_EQ(bit_storage_, p_bits_->GetRawStorage());
46
47 if (bit_index_ >= bit_size_) return -1;
48
49 uint32_t word_index = bit_index_ / 32;
50 uint32_t word = bit_storage_[word_index];
51 // Mask out any bits in the first word we've already considered.
52 word >>= bit_index_ & 0x1f;
53 if (word == 0) {
54 bit_index_ &= ~0x1f;
55 do {
56 word_index++;
57 if ((word_index * 32) >= bit_size_) {
58 bit_index_ = bit_size_;
59 return -1;
60 }
61 word = bit_storage_[word_index];
62 bit_index_ += 32;
63 } while (word == 0);
64 }
65 bit_index_ += CTZ(word) + 1;
66 return bit_index_ - 1;
67 }
buzbee862a7602013-04-05 10:58:54 -070068
69 static void* operator new(size_t size, ArenaAllocator* arena) {
70 return arena->NewMem(sizeof(ArenaBitVector::Iterator), true,
71 ArenaAllocator::kAllocGrowableBitMap);
72 };
73 static void operator delete(void* p) {}; // Nop.
74
75 private:
76 ArenaBitVector* const p_bits_;
77 uint32_t* const bit_storage_;
78 uint32_t bit_index_; // Current index (size in bits).
79 const uint32_t bit_size_; // Size of vector in bits.
80 };
81
82 ArenaBitVector(ArenaAllocator* arena, unsigned int start_bits, bool expandable,
83 OatBitMapKind kind = kBitMapMisc);
84 ~ArenaBitVector() {};
85
Brian Carlstromdf629502013-07-17 22:39:56 -070086 static void* operator new(size_t size, ArenaAllocator* arena) {
buzbee862a7602013-04-05 10:58:54 -070087 return arena->NewMem(sizeof(ArenaBitVector), true, ArenaAllocator::kAllocGrowableBitMap);
88 }
89 static void operator delete(void* p) {}; // Nop.
90
91 void SetBit(unsigned int num);
92 void ClearBit(unsigned int num);
93 void MarkAllBits(bool set);
94 void DebugBitVector(char* msg, int length);
95 bool IsBitSet(unsigned int num);
96 void ClearAllBits();
97 void SetInitialBits(unsigned int num_bits);
98 void Copy(ArenaBitVector* src);
99 void Intersect(const ArenaBitVector* src2);
100 void Union(const ArenaBitVector* src);
Ian Rogers8d3a1172013-06-04 01:13:28 -0700101 // Are we equal to another bit vector? Note: expandability attributes must also match.
102 bool Equal(const ArenaBitVector* src) {
103 return (storage_size_ == src->GetStorageSize()) &&
104 (expandable_ == src->IsExpandable()) &&
105 (memcmp(storage_, src->GetRawStorage(), storage_size_ * 4) == 0);
106 }
buzbee862a7602013-04-05 10:58:54 -0700107 int NumSetBits();
108
109 uint32_t GetStorageSize() const { return storage_size_; }
110 bool IsExpandable() const { return expandable_; }
111 uint32_t GetRawStorageWord(size_t idx) const { return storage_[idx]; }
112 uint32_t* GetRawStorage() { return storage_; }
Ian Rogers8d3a1172013-06-04 01:13:28 -0700113 const uint32_t* GetRawStorage() const { return storage_; }
buzbee862a7602013-04-05 10:58:54 -0700114
115 private:
116 ArenaAllocator* const arena_;
117 const bool expandable_; // expand bitmap if we run out?
118 const OatBitMapKind kind_; // for memory use tuning.
119 uint32_t storage_size_; // current size, in 32-bit words.
120 uint32_t* storage_;
121};
122
123
124} // namespace art
125
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700126#endif // ART_COMPILER_DEX_ARENA_BIT_VECTOR_H_