blob: 9e1bed9422080071e45c3b8a090ca7bd2c3d2b88 [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
Ian Rogersb23a7722012-10-09 16:54:26 -070054 // Does the given offset have an entry?
55 bool HasEntry(uintptr_t native_pc_offset) {
56 for (size_t i = 0; i < NumEntries(); ++i) {
57 if (GetNativePcOffset(i) == native_pc_offset) {
58 return true;
59 }
60 }
61 return false;
62 }
63
Ian Rogers0c7abda2012-09-19 13:33:42 -070064 // Finds the bitmap associated with the native pc offset.
65 const uint8_t* FindBitMap(uintptr_t native_pc_offset) {
66 size_t num_entries = NumEntries();
67 size_t index = Hash(native_pc_offset) % num_entries;
68 while (GetNativePcOffset(index) != native_pc_offset) {
69 index = (index + 1) % num_entries;
70 }
71 return GetBitMap(index);
72 }
73
74 static uint32_t Hash(uint32_t native_offset) {
75 uint32_t hash = native_offset;
76 hash ^= (hash >> 20) ^ (hash >> 12);
77 hash ^= (hash >> 7) ^ (hash >> 4);
78 return hash;
79 }
80
81 // The number of bytes used to encode registers.
82 size_t RegWidth() const {
Ian Rogers000d7242012-09-21 16:07:36 -070083 return ((size_t)data_[0] | ((size_t)data_[1] << 8)) >> 3;
Ian Rogers0c7abda2012-09-19 13:33:42 -070084 }
85
86 private:
87 // Skip the size information at the beginning of data.
88 const uint8_t* Table() const {
89 return data_ + 4;
90 }
91
92 // Number of bytes used to encode a native offset.
93 size_t NativeOffsetWidth() const {
Ian Rogers000d7242012-09-21 16:07:36 -070094 return data_[0] & 7;
Ian Rogers0c7abda2012-09-19 13:33:42 -070095 }
96
97 // The width of an entry in the table.
98 size_t EntryWidth() const {
99 return NativeOffsetWidth() + RegWidth();
100 }
101
102 const uint8_t* const data_; // The header and table data
103};
104
105} // namespace art
106
107#endif // ART_SRC_GC_MAP_H_