1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2009 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <stdint.h> 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker #include <iosfwd> 23*795d594fSAndroid Build Coastguard Worker #include <limits> 24*795d594fSAndroid Build Coastguard Worker #include <string> 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h> 27*795d594fSAndroid Build Coastguard Worker 28*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h" 29*795d594fSAndroid Build Coastguard Worker #include "base/locks.h" 30*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 31*795d594fSAndroid Build Coastguard Worker #include "base/mem_map.h" 32*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h" 33*795d594fSAndroid Build Coastguard Worker #include "gc_root.h" 34*795d594fSAndroid Build Coastguard Worker #include "obj_ptr.h" 35*795d594fSAndroid Build Coastguard Worker #include "offsets.h" 36*795d594fSAndroid Build Coastguard Worker #include "read_barrier_option.h" 37*795d594fSAndroid Build Coastguard Worker 38*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 39*795d594fSAndroid Build Coastguard Worker 40*795d594fSAndroid Build Coastguard Worker class IsMarkedVisitor; 41*795d594fSAndroid Build Coastguard Worker class RootInfo; 42*795d594fSAndroid Build Coastguard Worker 43*795d594fSAndroid Build Coastguard Worker namespace mirror { 44*795d594fSAndroid Build Coastguard Worker class Object; 45*795d594fSAndroid Build Coastguard Worker } // namespace mirror 46*795d594fSAndroid Build Coastguard Worker 47*795d594fSAndroid Build Coastguard Worker // Indirect reference definition. This must be interchangeable with JNI's jobject, and it's 48*795d594fSAndroid Build Coastguard Worker // convenient to let null be null, so we use void*. 49*795d594fSAndroid Build Coastguard Worker // 50*795d594fSAndroid Build Coastguard Worker // We need a 2-bit reference kind (global, local, weak global) and the rest of the `IndirectRef` 51*795d594fSAndroid Build Coastguard Worker // is used to locate the actual reference storage. 52*795d594fSAndroid Build Coastguard Worker // 53*795d594fSAndroid Build Coastguard Worker // For global and weak global references, we need a (potentially) large table index and we also 54*795d594fSAndroid Build Coastguard Worker // reserve some bits to be used to detect stale indirect references: we put a serial number in 55*795d594fSAndroid Build Coastguard Worker // the extra bits, and keep a copy of the serial number in the table. This requires more memory 56*795d594fSAndroid Build Coastguard Worker // and additional memory accesses on add/get, but is moving-GC safe. It will catch additional 57*795d594fSAndroid Build Coastguard Worker // problems, e.g.: create iref1 for obj, delete iref1, create iref2 for same obj, lookup iref1. 58*795d594fSAndroid Build Coastguard Worker // A pattern based on object bits will miss this. 59*795d594fSAndroid Build Coastguard Worker // 60*795d594fSAndroid Build Coastguard Worker // Local references use the same bits for the reference kind but the rest of their `IndirectRef` 61*795d594fSAndroid Build Coastguard Worker // encoding is different, see `LocalReferenceTable` for details. 62*795d594fSAndroid Build Coastguard Worker using IndirectRef = void*; 63*795d594fSAndroid Build Coastguard Worker 64*795d594fSAndroid Build Coastguard Worker // Indirect reference kind, used as the two low bits of IndirectRef. 65*795d594fSAndroid Build Coastguard Worker // 66*795d594fSAndroid Build Coastguard Worker // For convenience these match up with enum jobjectRefType from jni.h, except that 67*795d594fSAndroid Build Coastguard Worker // we use value 0 for JNI transitions instead of marking invalid reference type. 68*795d594fSAndroid Build Coastguard Worker enum IndirectRefKind { 69*795d594fSAndroid Build Coastguard Worker kJniTransition = 0, // <<JNI transition frame reference>> 70*795d594fSAndroid Build Coastguard Worker kLocal = 1, // <<local reference>> 71*795d594fSAndroid Build Coastguard Worker kGlobal = 2, // <<global reference>> 72*795d594fSAndroid Build Coastguard Worker kWeakGlobal = 3, // <<weak global reference>> 73*795d594fSAndroid Build Coastguard Worker kLastKind = kWeakGlobal 74*795d594fSAndroid Build Coastguard Worker }; 75*795d594fSAndroid Build Coastguard Worker EXPORT std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs); 76*795d594fSAndroid Build Coastguard Worker const char* GetIndirectRefKindString(IndirectRefKind kind); 77*795d594fSAndroid Build Coastguard Worker 78*795d594fSAndroid Build Coastguard Worker // Maintain a table of indirect references. Used for global and weak global JNI references. 79*795d594fSAndroid Build Coastguard Worker // 80*795d594fSAndroid Build Coastguard Worker // The table contains object references, where the strong global references are part of the 81*795d594fSAndroid Build Coastguard Worker // GC root set (but not the weak global references). When an object is added we return an 82*795d594fSAndroid Build Coastguard Worker // `IndirectRef` that is not a valid pointer but can be used to find the original value in O(1) 83*795d594fSAndroid Build Coastguard Worker // time. Conversions to and from indirect references are performed in JNI functions and when 84*795d594fSAndroid Build Coastguard Worker // returning from native methods to managed code, so they need to be very fast. 85*795d594fSAndroid Build Coastguard Worker // 86*795d594fSAndroid Build Coastguard Worker // The GC must be able to scan the entire table quickly. 87*795d594fSAndroid Build Coastguard Worker // 88*795d594fSAndroid Build Coastguard Worker // In summary, these must be very fast: 89*795d594fSAndroid Build Coastguard Worker // - adding references 90*795d594fSAndroid Build Coastguard Worker // - removing references 91*795d594fSAndroid Build Coastguard Worker // - converting an indirect reference back to an Object 92*795d594fSAndroid Build Coastguard Worker // These can be a little slower, but must still be pretty quick: 93*795d594fSAndroid Build Coastguard Worker // - scanning the entire table straight through 94*795d594fSAndroid Build Coastguard Worker 95*795d594fSAndroid Build Coastguard Worker // Table definition. 96*795d594fSAndroid Build Coastguard Worker // 97*795d594fSAndroid Build Coastguard Worker // For the global reference tables, the expected common operations are adding a new entry and 98*795d594fSAndroid Build Coastguard Worker // removing a recently-added entry (usually the most-recently-added entry). 99*795d594fSAndroid Build Coastguard Worker // 100*795d594fSAndroid Build Coastguard Worker // If we delete entries from the middle of the list, we will be left with "holes". We track the 101*795d594fSAndroid Build Coastguard Worker // number of holes so that, when adding new elements, we can quickly decide to do a trivial append 102*795d594fSAndroid Build Coastguard Worker // or go slot-hunting. 103*795d594fSAndroid Build Coastguard Worker // 104*795d594fSAndroid Build Coastguard Worker // When the top-most entry is removed, any holes immediately below it are also removed. Thus, 105*795d594fSAndroid Build Coastguard Worker // deletion of an entry may reduce "top_index" by more than one. 106*795d594fSAndroid Build Coastguard Worker // 107*795d594fSAndroid Build Coastguard Worker // Common alternative implementation: make IndirectRef a pointer to the actual reference slot. 108*795d594fSAndroid Build Coastguard Worker // Instead of getting a table and doing a lookup, the lookup can be done instantly. Operations like 109*795d594fSAndroid Build Coastguard Worker // determining the type and deleting the reference are more expensive because the table must be 110*795d594fSAndroid Build Coastguard Worker // hunted for (i.e. you have to do a pointer comparison to see which table it's in), you can't move 111*795d594fSAndroid Build Coastguard Worker // the table when expanding it (so realloc() is out), and tricks like serial number checking to 112*795d594fSAndroid Build Coastguard Worker // detect stale references aren't possible (though we may be able to get similar benefits with other 113*795d594fSAndroid Build Coastguard Worker // approaches). 114*795d594fSAndroid Build Coastguard Worker // 115*795d594fSAndroid Build Coastguard Worker // TODO: consider a "lastDeleteIndex" for quick hole-filling when an add immediately follows a 116*795d594fSAndroid Build Coastguard Worker // delete. 117*795d594fSAndroid Build Coastguard Worker 118*795d594fSAndroid Build Coastguard Worker // We associate a few bits of serial number with each reference, for error checking. 119*795d594fSAndroid Build Coastguard Worker static constexpr unsigned int kIRTSerialBits = 3; 120*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kIRTMaxSerial = ((1 << kIRTSerialBits) - 1); 121*795d594fSAndroid Build Coastguard Worker 122*795d594fSAndroid Build Coastguard Worker class IrtEntry { 123*795d594fSAndroid Build Coastguard Worker public: 124*795d594fSAndroid Build Coastguard Worker void Add(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_); 125*795d594fSAndroid Build Coastguard Worker GetReference()126*795d594fSAndroid Build Coastguard Worker GcRoot<mirror::Object>* GetReference() { 127*795d594fSAndroid Build Coastguard Worker DCHECK_LE(serial_, kIRTMaxSerial); 128*795d594fSAndroid Build Coastguard Worker return &reference_; 129*795d594fSAndroid Build Coastguard Worker } 130*795d594fSAndroid Build Coastguard Worker GetReference()131*795d594fSAndroid Build Coastguard Worker const GcRoot<mirror::Object>* GetReference() const { 132*795d594fSAndroid Build Coastguard Worker DCHECK_LE(serial_, kIRTMaxSerial); 133*795d594fSAndroid Build Coastguard Worker return &reference_; 134*795d594fSAndroid Build Coastguard Worker } 135*795d594fSAndroid Build Coastguard Worker GetSerial()136*795d594fSAndroid Build Coastguard Worker uint32_t GetSerial() const { 137*795d594fSAndroid Build Coastguard Worker return serial_; 138*795d594fSAndroid Build Coastguard Worker } 139*795d594fSAndroid Build Coastguard Worker 140*795d594fSAndroid Build Coastguard Worker void SetReference(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_); 141*795d594fSAndroid Build Coastguard Worker 142*795d594fSAndroid Build Coastguard Worker private: 143*795d594fSAndroid Build Coastguard Worker uint32_t serial_; // Incremented for each reuse; checked against reference. 144*795d594fSAndroid Build Coastguard Worker GcRoot<mirror::Object> reference_; 145*795d594fSAndroid Build Coastguard Worker }; 146*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(IrtEntry) == 2 * sizeof(uint32_t), "Unexpected sizeof(IrtEntry)"); 147*795d594fSAndroid Build Coastguard Worker static_assert(IsPowerOfTwo(sizeof(IrtEntry)), "Unexpected sizeof(IrtEntry)"); 148*795d594fSAndroid Build Coastguard Worker 149*795d594fSAndroid Build Coastguard Worker class IndirectReferenceTable { 150*795d594fSAndroid Build Coastguard Worker public: 151*795d594fSAndroid Build Coastguard Worker // Constructs an uninitialized indirect reference table. Use `Initialize()` to initialize it. 152*795d594fSAndroid Build Coastguard Worker explicit IndirectReferenceTable(IndirectRefKind kind); 153*795d594fSAndroid Build Coastguard Worker 154*795d594fSAndroid Build Coastguard Worker // Initialize the indirect reference table. 155*795d594fSAndroid Build Coastguard Worker // 156*795d594fSAndroid Build Coastguard Worker // Max_count is the requested total capacity (not resizable). The actual total capacity 157*795d594fSAndroid Build Coastguard Worker // can be higher to utilize all allocated memory (rounding up to whole pages). 158*795d594fSAndroid Build Coastguard Worker bool Initialize(size_t max_count, std::string* error_msg); 159*795d594fSAndroid Build Coastguard Worker 160*795d594fSAndroid Build Coastguard Worker ~IndirectReferenceTable(); 161*795d594fSAndroid Build Coastguard Worker 162*795d594fSAndroid Build Coastguard Worker // Add a new entry. "obj" must be a valid non-null object reference. This function will 163*795d594fSAndroid Build Coastguard Worker // return null if an error happened (with an appropriate error message set). 164*795d594fSAndroid Build Coastguard Worker IndirectRef Add(ObjPtr<mirror::Object> obj, std::string* error_msg) 165*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 166*795d594fSAndroid Build Coastguard Worker 167*795d594fSAndroid Build Coastguard Worker // Given an IndirectRef in the table, return the Object it refers to. 168*795d594fSAndroid Build Coastguard Worker // 169*795d594fSAndroid Build Coastguard Worker // This function may abort under error conditions. 170*795d594fSAndroid Build Coastguard Worker template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 171*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> Get(IndirectRef iref) const REQUIRES_SHARED(Locks::mutator_lock_) 172*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE; 173*795d594fSAndroid Build Coastguard Worker 174*795d594fSAndroid Build Coastguard Worker // Updates an existing indirect reference to point to a new object. 175*795d594fSAndroid Build Coastguard Worker void Update(IndirectRef iref, ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_); 176*795d594fSAndroid Build Coastguard Worker 177*795d594fSAndroid Build Coastguard Worker // Remove an existing entry. 178*795d594fSAndroid Build Coastguard Worker // 179*795d594fSAndroid Build Coastguard Worker // If the entry is not between the current top index and the bottom index 180*795d594fSAndroid Build Coastguard Worker // specified by the cookie, we don't remove anything. This is the behavior 181*795d594fSAndroid Build Coastguard Worker // required by JNI's DeleteLocalRef function. 182*795d594fSAndroid Build Coastguard Worker // 183*795d594fSAndroid Build Coastguard Worker // Returns "false" if nothing was removed. 184*795d594fSAndroid Build Coastguard Worker bool Remove(IndirectRef iref); 185*795d594fSAndroid Build Coastguard Worker 186*795d594fSAndroid Build Coastguard Worker void Dump(std::ostream& os) const 187*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) 188*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::alloc_tracker_lock_); 189*795d594fSAndroid Build Coastguard Worker GetKind()190*795d594fSAndroid Build Coastguard Worker IndirectRefKind GetKind() const { 191*795d594fSAndroid Build Coastguard Worker return kind_; 192*795d594fSAndroid Build Coastguard Worker } 193*795d594fSAndroid Build Coastguard Worker 194*795d594fSAndroid Build Coastguard Worker // Return the #of entries in the entire table. This includes holes, and 195*795d594fSAndroid Build Coastguard Worker // so may be larger than the actual number of "live" entries. Capacity()196*795d594fSAndroid Build Coastguard Worker size_t Capacity() const { 197*795d594fSAndroid Build Coastguard Worker return top_index_; 198*795d594fSAndroid Build Coastguard Worker } 199*795d594fSAndroid Build Coastguard Worker 200*795d594fSAndroid Build Coastguard Worker // Return the number of non-null entries in the table. Only reliable for a 201*795d594fSAndroid Build Coastguard Worker // single segment table. NEntriesForGlobal()202*795d594fSAndroid Build Coastguard Worker int32_t NEntriesForGlobal() { 203*795d594fSAndroid Build Coastguard Worker return top_index_ - current_num_holes_; 204*795d594fSAndroid Build Coastguard Worker } 205*795d594fSAndroid Build Coastguard Worker 206*795d594fSAndroid Build Coastguard Worker // We'll only state here how much is trivially free, without recovering holes. 207*795d594fSAndroid Build Coastguard Worker // Thus this is a conservative estimate. 208*795d594fSAndroid Build Coastguard Worker size_t FreeCapacity() const; 209*795d594fSAndroid Build Coastguard Worker 210*795d594fSAndroid Build Coastguard Worker void VisitRoots(RootVisitor* visitor, const RootInfo& root_info) 211*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 212*795d594fSAndroid Build Coastguard Worker 213*795d594fSAndroid Build Coastguard Worker // Release pages past the end of the table that may have previously held references. 214*795d594fSAndroid Build Coastguard Worker void Trim() REQUIRES_SHARED(Locks::mutator_lock_); 215*795d594fSAndroid Build Coastguard Worker 216*795d594fSAndroid Build Coastguard Worker // Determine what kind of indirect reference this is. Opposite of EncodeIndirectRefKind. GetIndirectRefKind(IndirectRef iref)217*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE static inline IndirectRefKind GetIndirectRefKind(IndirectRef iref) { 218*795d594fSAndroid Build Coastguard Worker return DecodeIndirectRefKind(reinterpret_cast<uintptr_t>(iref)); 219*795d594fSAndroid Build Coastguard Worker } 220*795d594fSAndroid Build Coastguard Worker GetGlobalOrWeakGlobalMask()221*795d594fSAndroid Build Coastguard Worker static constexpr uintptr_t GetGlobalOrWeakGlobalMask() { 222*795d594fSAndroid Build Coastguard Worker constexpr uintptr_t mask = enum_cast<uintptr_t>(kGlobal); 223*795d594fSAndroid Build Coastguard Worker static_assert(IsPowerOfTwo(mask)); 224*795d594fSAndroid Build Coastguard Worker static_assert((mask & kJniTransition) == 0u); 225*795d594fSAndroid Build Coastguard Worker static_assert((mask & kLocal) == 0u); 226*795d594fSAndroid Build Coastguard Worker static_assert((mask & kGlobal) != 0u); 227*795d594fSAndroid Build Coastguard Worker static_assert((mask & kWeakGlobal) != 0u); 228*795d594fSAndroid Build Coastguard Worker return mask; 229*795d594fSAndroid Build Coastguard Worker } 230*795d594fSAndroid Build Coastguard Worker IsGlobalOrWeakGlobalReference(IndirectRef iref)231*795d594fSAndroid Build Coastguard Worker static bool IsGlobalOrWeakGlobalReference(IndirectRef iref) { 232*795d594fSAndroid Build Coastguard Worker return (reinterpret_cast<uintptr_t>(iref) & GetGlobalOrWeakGlobalMask()) != 0u; 233*795d594fSAndroid Build Coastguard Worker } 234*795d594fSAndroid Build Coastguard Worker IsJniTransitionOrLocalReference(IndirectRef iref)235*795d594fSAndroid Build Coastguard Worker static bool IsJniTransitionOrLocalReference(IndirectRef iref) { 236*795d594fSAndroid Build Coastguard Worker return !IsGlobalOrWeakGlobalReference(iref); 237*795d594fSAndroid Build Coastguard Worker } 238*795d594fSAndroid Build Coastguard Worker 239*795d594fSAndroid Build Coastguard Worker template <typename T> ClearIndirectRefKind(IndirectRef iref)240*795d594fSAndroid Build Coastguard Worker static T ClearIndirectRefKind(IndirectRef iref) { 241*795d594fSAndroid Build Coastguard Worker static_assert(std::is_pointer_v<T>); 242*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<T>( 243*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(iref) & ~static_cast<uintptr_t>(kKindMask)); 244*795d594fSAndroid Build Coastguard Worker } 245*795d594fSAndroid Build Coastguard Worker GetIndirectRefKindMask()246*795d594fSAndroid Build Coastguard Worker static constexpr uintptr_t GetIndirectRefKindMask() { 247*795d594fSAndroid Build Coastguard Worker return kKindMask; 248*795d594fSAndroid Build Coastguard Worker } 249*795d594fSAndroid Build Coastguard Worker 250*795d594fSAndroid Build Coastguard Worker /* Reference validation for CheckJNI. */ 251*795d594fSAndroid Build Coastguard Worker bool IsValidReference(IndirectRef, /*out*/std::string* error_msg) const 252*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 253*795d594fSAndroid Build Coastguard Worker 254*795d594fSAndroid Build Coastguard Worker EXPORT void SweepJniWeakGlobals(IsMarkedVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) 255*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::jni_weak_globals_lock_); 256*795d594fSAndroid Build Coastguard Worker 257*795d594fSAndroid Build Coastguard Worker private: 258*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kShiftedSerialMask = (1u << kIRTSerialBits) - 1; 259*795d594fSAndroid Build Coastguard Worker 260*795d594fSAndroid Build Coastguard Worker static constexpr size_t kKindBits = MinimumBitsToStore( 261*795d594fSAndroid Build Coastguard Worker static_cast<uint32_t>(IndirectRefKind::kLastKind)); 262*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kKindMask = (1u << kKindBits) - 1; 263*795d594fSAndroid Build Coastguard Worker EncodeIndex(uint32_t table_index)264*795d594fSAndroid Build Coastguard Worker static constexpr uintptr_t EncodeIndex(uint32_t table_index) { 265*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(IndirectRef) == sizeof(uintptr_t), "Unexpected IndirectRef size"); 266*795d594fSAndroid Build Coastguard Worker DCHECK_LE(MinimumBitsToStore(table_index), BitSizeOf<uintptr_t>() - kIRTSerialBits - kKindBits); 267*795d594fSAndroid Build Coastguard Worker return (static_cast<uintptr_t>(table_index) << kKindBits << kIRTSerialBits); 268*795d594fSAndroid Build Coastguard Worker } DecodeIndex(uintptr_t uref)269*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t DecodeIndex(uintptr_t uref) { 270*795d594fSAndroid Build Coastguard Worker return static_cast<uint32_t>((uref >> kKindBits) >> kIRTSerialBits); 271*795d594fSAndroid Build Coastguard Worker } 272*795d594fSAndroid Build Coastguard Worker EncodeIndirectRefKind(IndirectRefKind kind)273*795d594fSAndroid Build Coastguard Worker static constexpr uintptr_t EncodeIndirectRefKind(IndirectRefKind kind) { 274*795d594fSAndroid Build Coastguard Worker return static_cast<uintptr_t>(kind); 275*795d594fSAndroid Build Coastguard Worker } DecodeIndirectRefKind(uintptr_t uref)276*795d594fSAndroid Build Coastguard Worker static constexpr IndirectRefKind DecodeIndirectRefKind(uintptr_t uref) { 277*795d594fSAndroid Build Coastguard Worker return static_cast<IndirectRefKind>(uref & kKindMask); 278*795d594fSAndroid Build Coastguard Worker } 279*795d594fSAndroid Build Coastguard Worker EncodeSerial(uint32_t serial)280*795d594fSAndroid Build Coastguard Worker static constexpr uintptr_t EncodeSerial(uint32_t serial) { 281*795d594fSAndroid Build Coastguard Worker DCHECK_LE(MinimumBitsToStore(serial), kIRTSerialBits); 282*795d594fSAndroid Build Coastguard Worker return serial << kKindBits; 283*795d594fSAndroid Build Coastguard Worker } DecodeSerial(uintptr_t uref)284*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t DecodeSerial(uintptr_t uref) { 285*795d594fSAndroid Build Coastguard Worker return static_cast<uint32_t>(uref >> kKindBits) & kShiftedSerialMask; 286*795d594fSAndroid Build Coastguard Worker } 287*795d594fSAndroid Build Coastguard Worker EncodeIndirectRef(uint32_t table_index,uint32_t serial)288*795d594fSAndroid Build Coastguard Worker constexpr uintptr_t EncodeIndirectRef(uint32_t table_index, uint32_t serial) const { 289*795d594fSAndroid Build Coastguard Worker DCHECK_LT(table_index, max_entries_); 290*795d594fSAndroid Build Coastguard Worker return EncodeIndex(table_index) | EncodeSerial(serial) | EncodeIndirectRefKind(kind_); 291*795d594fSAndroid Build Coastguard Worker } 292*795d594fSAndroid Build Coastguard Worker 293*795d594fSAndroid Build Coastguard Worker static void ConstexprChecks(); 294*795d594fSAndroid Build Coastguard Worker 295*795d594fSAndroid Build Coastguard Worker // Extract the table index from an indirect reference. ExtractIndex(IndirectRef iref)296*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE static uint32_t ExtractIndex(IndirectRef iref) { 297*795d594fSAndroid Build Coastguard Worker return DecodeIndex(reinterpret_cast<uintptr_t>(iref)); 298*795d594fSAndroid Build Coastguard Worker } 299*795d594fSAndroid Build Coastguard Worker ToIndirectRef(uint32_t table_index)300*795d594fSAndroid Build Coastguard Worker IndirectRef ToIndirectRef(uint32_t table_index) const { 301*795d594fSAndroid Build Coastguard Worker DCHECK_LT(table_index, max_entries_); 302*795d594fSAndroid Build Coastguard Worker uint32_t serial = table_[table_index].GetSerial(); 303*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<IndirectRef>(EncodeIndirectRef(table_index, serial)); 304*795d594fSAndroid Build Coastguard Worker } 305*795d594fSAndroid Build Coastguard Worker 306*795d594fSAndroid Build Coastguard Worker // Abort if check_jni is not enabled. Otherwise, just log as an error. 307*795d594fSAndroid Build Coastguard Worker static void AbortIfNoCheckJNI(const std::string& msg); 308*795d594fSAndroid Build Coastguard Worker 309*795d594fSAndroid Build Coastguard Worker /* extra debugging checks */ 310*795d594fSAndroid Build Coastguard Worker bool CheckEntry(const char*, IndirectRef, uint32_t) const; 311*795d594fSAndroid Build Coastguard Worker 312*795d594fSAndroid Build Coastguard Worker // Mem map where we store the indirect refs. 313*795d594fSAndroid Build Coastguard Worker MemMap table_mem_map_; 314*795d594fSAndroid Build Coastguard Worker // Bottom of the stack. Do not directly access the object references 315*795d594fSAndroid Build Coastguard Worker // in this as they are roots. Use Get() that has a read barrier. 316*795d594fSAndroid Build Coastguard Worker IrtEntry* table_; 317*795d594fSAndroid Build Coastguard Worker // Bit mask, ORed into all irefs. 318*795d594fSAndroid Build Coastguard Worker const IndirectRefKind kind_; 319*795d594fSAndroid Build Coastguard Worker 320*795d594fSAndroid Build Coastguard Worker // The "top of stack" index where new references are added. 321*795d594fSAndroid Build Coastguard Worker size_t top_index_; 322*795d594fSAndroid Build Coastguard Worker 323*795d594fSAndroid Build Coastguard Worker // Maximum number of entries allowed. 324*795d594fSAndroid Build Coastguard Worker size_t max_entries_; 325*795d594fSAndroid Build Coastguard Worker 326*795d594fSAndroid Build Coastguard Worker // Some values to retain old behavior with holes. 327*795d594fSAndroid Build Coastguard Worker // Description of the algorithm is in the .cc file. 328*795d594fSAndroid Build Coastguard Worker // TODO: Consider other data structures for compact tables, e.g., free lists. 329*795d594fSAndroid Build Coastguard Worker size_t current_num_holes_; // Number of holes in the current / top segment. 330*795d594fSAndroid Build Coastguard Worker }; 331*795d594fSAndroid Build Coastguard Worker 332*795d594fSAndroid Build Coastguard Worker } // namespace art 333*795d594fSAndroid Build Coastguard Worker 334*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_H_ 335