blob: 5a7a9e00fd7b3b6c7c37a37482dc645057571b41 [file] [log] [blame]
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +00001/*
2 * Copyright (C) 2014 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#ifndef ART_COMPILER_GC_MAP_BUILDER_H_
18#define ART_COMPILER_GC_MAP_BUILDER_H_
19
20#include <vector>
21
22#include "gc_map.h"
23
24namespace art {
25
26class GcMapBuilder {
27 public:
28 GcMapBuilder(std::vector<uint8_t>* table,
29 size_t entries, uint32_t max_native_offset,
30 size_t references_width) : entries_(entries),
31 references_width_(references_width), in_use_(entries),
32 table_(table) {
33 // Compute width in bytes needed to hold max_native_offset.
34 native_offset_width_ = 0;
35 while (max_native_offset != 0) {
36 native_offset_width_++;
37 max_native_offset >>= 8;
38 }
39 // Resize table and set up header.
40 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
41 CHECK_LT(native_offset_width_, 1U << 3);
42 (*table)[0] = native_offset_width_ & 7;
43 CHECK_LT(references_width_, 1U << 13);
44 (*table)[0] |= (references_width_ << 3) & 0xFF;
45 (*table)[1] = (references_width_ >> 5) & 0xFF;
46 CHECK_LT(entries, 1U << 16);
47 (*table)[2] = entries & 0xFF;
48 (*table)[3] = (entries >> 8) & 0xFF;
49 }
50
51 void AddEntry(uint32_t native_offset, const uint8_t* references) {
52 size_t table_index = TableIndex(native_offset);
53 while (in_use_[table_index]) {
54 table_index = (table_index + 1) % entries_;
55 }
56 in_use_[table_index] = true;
57 SetCodeOffset(table_index, native_offset);
58 DCHECK_EQ(native_offset, GetCodeOffset(table_index));
59 SetReferences(table_index, references);
60 }
61
62 private:
63 size_t TableIndex(uint32_t native_offset) {
64 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
65 }
66
67 uint32_t GetCodeOffset(size_t table_index) {
68 uint32_t native_offset = 0;
69 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
70 for (size_t i = 0; i < native_offset_width_; i++) {
71 native_offset |= (*table_)[table_offset + i] << (i * 8);
72 }
73 return native_offset;
74 }
75
76 void SetCodeOffset(size_t table_index, uint32_t native_offset) {
77 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
78 for (size_t i = 0; i < native_offset_width_; i++) {
79 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
80 }
81 }
82
83 void SetReferences(size_t table_index, const uint8_t* references) {
84 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
85 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
86 }
87
88 size_t EntryWidth() const {
89 return native_offset_width_ + references_width_;
90 }
91
92 // Number of entries in the table.
93 const size_t entries_;
94 // Number of bytes used to encode the reference bitmap.
95 const size_t references_width_;
96 // Number of bytes used to encode a native offset.
97 size_t native_offset_width_;
98 // Entries that are in use.
99 std::vector<bool> in_use_;
100 // The table we're building.
101 std::vector<uint8_t>* const table_;
102};
103
104} // namespace art
105
106#endif // ART_COMPILER_GC_MAP_BUILDER_H_