blob: 7becda5baadb46fac92a1d682d5efcfd31ff3634 [file] [log] [blame]
Ian Rogers0c7abda2012-09-19 13:33:42 -07001/*
2 * Copyright (C) 2012 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_SRC_GC_MAP_H_
18#define ART_SRC_GC_MAP_H_
19
20#include "logging.h"
21#include "macros.h"
22#include <stdint.h>
23
24namespace art {
25
26// Lightweight wrapper for native PC offset to reference bit maps.
27class NativePcOffsetToReferenceMap {
28 public:
29 NativePcOffsetToReferenceMap(const uint8_t* data) : data_(data) {
30 CHECK(data_ != NULL);
31 }
32
33 // The number of entries in the table.
34 size_t NumEntries() const {
35 return data_[2] | (data_[3] << 8);
36 }
37
38 // Return address of bitmap encoding what are live references.
39 const uint8_t* GetBitMap(size_t index) const {
40 size_t entry_offset = index * EntryWidth();
41 return &Table()[entry_offset + NativeOffsetWidth()];
42 }
43
44 // Get the native PC encoded in the table at the given index.
45 uintptr_t GetNativePcOffset(size_t index) const {
46 size_t entry_offset = index * EntryWidth();
47 uintptr_t result = 0;
48 for (size_t i = 0; i < NativeOffsetWidth(); ++i) {
49 result |= Table()[entry_offset + i] << (i * 8);
50 }
51 return result;
52 }
53
54 // Finds the bitmap associated with the native pc offset.
55 const uint8_t* FindBitMap(uintptr_t native_pc_offset) {
56 size_t num_entries = NumEntries();
57 size_t index = Hash(native_pc_offset) % num_entries;
58 while (GetNativePcOffset(index) != native_pc_offset) {
59 index = (index + 1) % num_entries;
60 }
61 return GetBitMap(index);
62 }
63
64 static uint32_t Hash(uint32_t native_offset) {
65 uint32_t hash = native_offset;
66 hash ^= (hash >> 20) ^ (hash >> 12);
67 hash ^= (hash >> 7) ^ (hash >> 4);
68 return hash;
69 }
70
71 // The number of bytes used to encode registers.
72 size_t RegWidth() const {
Ian Rogers000d7242012-09-21 16:07:36 -070073 return ((size_t)data_[0] | ((size_t)data_[1] << 8)) >> 3;
Ian Rogers0c7abda2012-09-19 13:33:42 -070074 }
75
76 private:
77 // Skip the size information at the beginning of data.
78 const uint8_t* Table() const {
79 return data_ + 4;
80 }
81
82 // Number of bytes used to encode a native offset.
83 size_t NativeOffsetWidth() const {
Ian Rogers000d7242012-09-21 16:07:36 -070084 return data_[0] & 7;
Ian Rogers0c7abda2012-09-19 13:33:42 -070085 }
86
87 // The width of an entry in the table.
88 size_t EntryWidth() const {
89 return NativeOffsetWidth() + RegWidth();
90 }
91
92 const uint8_t* const data_; // The header and table data
93};
94
95} // namespace art
96
97#endif // ART_SRC_GC_MAP_H_