blob: 8e4dbdbee0b909b1d7172d1e5f155e32b4b0dd60 [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
Ian Rogers0c7abda2012-09-19 13:33:42 -070020#include <stdint.h>
21
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
23#include "base/macros.h"
24
Ian Rogers0c7abda2012-09-19 13:33:42 -070025namespace art {
26
27// Lightweight wrapper for native PC offset to reference bit maps.
28class NativePcOffsetToReferenceMap {
29 public:
30 NativePcOffsetToReferenceMap(const uint8_t* data) : data_(data) {
31 CHECK(data_ != NULL);
32 }
33
34 // The number of entries in the table.
35 size_t NumEntries() const {
36 return data_[2] | (data_[3] << 8);
37 }
38
39 // Return address of bitmap encoding what are live references.
40 const uint8_t* GetBitMap(size_t index) const {
41 size_t entry_offset = index * EntryWidth();
42 return &Table()[entry_offset + NativeOffsetWidth()];
43 }
44
45 // Get the native PC encoded in the table at the given index.
46 uintptr_t GetNativePcOffset(size_t index) const {
47 size_t entry_offset = index * EntryWidth();
48 uintptr_t result = 0;
49 for (size_t i = 0; i < NativeOffsetWidth(); ++i) {
50 result |= Table()[entry_offset + i] << (i * 8);
51 }
52 return result;
53 }
54
Ian Rogersb23a7722012-10-09 16:54:26 -070055 // Does the given offset have an entry?
56 bool HasEntry(uintptr_t native_pc_offset) {
57 for (size_t i = 0; i < NumEntries(); ++i) {
58 if (GetNativePcOffset(i) == native_pc_offset) {
59 return true;
60 }
61 }
62 return false;
63 }
64
Ian Rogers0c7abda2012-09-19 13:33:42 -070065 // Finds the bitmap associated with the native pc offset.
66 const uint8_t* FindBitMap(uintptr_t native_pc_offset) {
67 size_t num_entries = NumEntries();
68 size_t index = Hash(native_pc_offset) % num_entries;
69 while (GetNativePcOffset(index) != native_pc_offset) {
70 index = (index + 1) % num_entries;
71 }
72 return GetBitMap(index);
73 }
74
75 static uint32_t Hash(uint32_t native_offset) {
76 uint32_t hash = native_offset;
77 hash ^= (hash >> 20) ^ (hash >> 12);
78 hash ^= (hash >> 7) ^ (hash >> 4);
79 return hash;
80 }
81
82 // The number of bytes used to encode registers.
83 size_t RegWidth() const {
buzbeecbd6d442012-11-17 14:11:25 -080084 return (static_cast<size_t>(data_[0]) | (static_cast<size_t>(data_[1]) << 8)) >> 3;
Ian Rogers0c7abda2012-09-19 13:33:42 -070085 }
86
87 private:
88 // Skip the size information at the beginning of data.
89 const uint8_t* Table() const {
90 return data_ + 4;
91 }
92
93 // Number of bytes used to encode a native offset.
94 size_t NativeOffsetWidth() const {
Ian Rogers000d7242012-09-21 16:07:36 -070095 return data_[0] & 7;
Ian Rogers0c7abda2012-09-19 13:33:42 -070096 }
97
98 // The width of an entry in the table.
99 size_t EntryWidth() const {
100 return NativeOffsetWidth() + RegWidth();
101 }
102
103 const uint8_t* const data_; // The header and table data
104};
105
106} // namespace art
107
108#endif // ART_SRC_GC_MAP_H_