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