1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include "common.h"
20 #include "dex_format.h"
21 
22 #include <vector>
23 
24 namespace ir {
25 
26 // A simple index tracking and allocator
27 class IndexMap {
28  public:
AllocateIndex()29   dex::u4 AllocateIndex() {
30     const auto size = indexes_map_.size();
31     while (alloc_pos_ < size && indexes_map_[alloc_pos_]) {
32       ++alloc_pos_;
33     }
34     MarkUsedIndex(alloc_pos_);
35     return alloc_pos_++;
36   }
37 
MarkUsedIndex(dex::u4 index)38   void MarkUsedIndex(dex::u4 index) {
39     if (index >= indexes_map_.size()) {
40       indexes_map_.resize(index + 1);
41     }
42     SLICER_CHECK(!indexes_map_[index]);
43     indexes_map_[index] = true;
44   }
45 
46  private:
47   std::vector<bool> indexes_map_;
48   dex::u4 alloc_pos_ = 0;
49 };
50 
51 }  // namespace ir
52