blob: 53e6eb65124b3fb4c8583a08664d51d6e09625af [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),
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070039 bit_size_(p_bits_->storage_size_ * sizeof(uint32_t) * 8) {}
buzbee862a7602013-04-05 10:58:54 -070040
Ian Rogers48ae54e2013-06-03 16:59:58 -070041 // Return the position of the next set bit. -1 means end-of-element reached.
buzbee0d829482013-10-11 15:24:55 -070042 int32_t Next() {
Ian Rogers48ae54e2013-06-03 16:59:58 -070043 // 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
buzbee28c23002013-09-07 09:12:27 -070047 if (UNLIKELY(bit_index_ >= bit_size_)) return -1;
Ian Rogers48ae54e2013-06-03 16:59:58 -070048
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++;
buzbee28c23002013-09-07 09:12:27 -070057 if (UNLIKELY((word_index * 32) >= bit_size_)) {
Ian Rogers48ae54e2013-06-03 16:59:58 -070058 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) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070070 return arena->Alloc(sizeof(ArenaBitVector::Iterator),
71 ArenaAllocator::kAllocGrowableBitMap);
buzbee862a7602013-04-05 10:58:54 -070072 };
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070073 static void operator delete(void* p) {} // Nop.
buzbee862a7602013-04-05 10:58:54 -070074
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
buzbee0d829482013-10-11 15:24:55 -070082 ArenaBitVector(ArenaAllocator* arena, uint32_t start_bits, bool expandable,
buzbee862a7602013-04-05 10:58:54 -070083 OatBitMapKind kind = kBitMapMisc);
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070084 ~ArenaBitVector() {}
buzbee862a7602013-04-05 10:58:54 -070085
Brian Carlstromdf629502013-07-17 22:39:56 -070086 static void* operator new(size_t size, ArenaAllocator* arena) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070087 return arena->Alloc(sizeof(ArenaBitVector), ArenaAllocator::kAllocGrowableBitMap);
buzbee862a7602013-04-05 10:58:54 -070088 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070089 static void operator delete(void* p) {} // Nop.
buzbee862a7602013-04-05 10:58:54 -070090
buzbee0d829482013-10-11 15:24:55 -070091 void SetBit(uint32_t num);
92 void ClearBit(uint32_t num);
buzbee862a7602013-04-05 10:58:54 -070093 void MarkAllBits(bool set);
94 void DebugBitVector(char* msg, int length);
buzbee0d829482013-10-11 15:24:55 -070095 bool IsBitSet(uint32_t num);
buzbee862a7602013-04-05 10:58:54 -070096 void ClearAllBits();
buzbee0d829482013-10-11 15:24:55 -070097 void SetInitialBits(uint32_t num_bits);
buzbee28c23002013-09-07 09:12:27 -070098 void Copy(ArenaBitVector* src) {
99 memcpy(storage_, src->GetRawStorage(), sizeof(uint32_t) * storage_size_);
100 }
buzbee862a7602013-04-05 10:58:54 -0700101 void Intersect(const ArenaBitVector* src2);
102 void Union(const ArenaBitVector* src);
Ian Rogers8d3a1172013-06-04 01:13:28 -0700103 // Are we equal to another bit vector? Note: expandability attributes must also match.
104 bool Equal(const ArenaBitVector* src) {
105 return (storage_size_ == src->GetStorageSize()) &&
106 (expandable_ == src->IsExpandable()) &&
107 (memcmp(storage_, src->GetRawStorage(), storage_size_ * 4) == 0);
108 }
buzbee0d829482013-10-11 15:24:55 -0700109 int32_t NumSetBits();
buzbee862a7602013-04-05 10:58:54 -0700110
111 uint32_t GetStorageSize() const { return storage_size_; }
112 bool IsExpandable() const { return expandable_; }
113 uint32_t GetRawStorageWord(size_t idx) const { return storage_[idx]; }
114 uint32_t* GetRawStorage() { return storage_; }
Ian Rogers8d3a1172013-06-04 01:13:28 -0700115 const uint32_t* GetRawStorage() const { return storage_; }
buzbee862a7602013-04-05 10:58:54 -0700116
117 private:
118 ArenaAllocator* const arena_;
119 const bool expandable_; // expand bitmap if we run out?
120 const OatBitMapKind kind_; // for memory use tuning.
121 uint32_t storage_size_; // current size, in 32-bit words.
122 uint32_t* storage_;
123};
124
125
126} // namespace art
127
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700128#endif // ART_COMPILER_DEX_ARENA_BIT_VECTOR_H_