1 /* 2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_CODE_OOPRECORDER_HPP 26 #define SHARE_VM_CODE_OOPRECORDER_HPP 27 28 #include "runtime/handles.hpp" 29 #include "utilities/growableArray.hpp" 30 31 // Recording and retrieval of oop relocations in compiled code. 32 33 class CodeBlob; 34 35 class OopRecorder : public ResourceObj { 36 public: 37 // A two-way mapping from positive indexes to oop handles. 38 // The zero index is reserved for a constant (sharable) null. 39 // Indexes may not be negative. 40 41 // Use the given arena to manage storage, if not NULL. 42 // By default, uses the current ResourceArea. 43 OopRecorder(Arena* arena = NULL); 44 45 // Generate a new index on which CodeBlob::oop_addr_at will work. 46 // allocate_index and find_index never return the same index, 47 // and allocate_index never returns the same index twice. 48 // In fact, two successive calls to allocate_index return successive ints. 49 int allocate_index(jobject h) { 50 return add_handle(h, false); 51 } 52 53 // For a given jobject, this will return the same index repeatedly. 54 // The index can later be given to oop_at to retrieve the oop. 55 // However, the oop must not be changed via CodeBlob::oop_addr_at. 56 int find_index(jobject h) { 57 int index = maybe_find_index(h); 58 if (index < 0) { // previously unallocated 59 index = add_handle(h, true); 60 } 61 return index; 62 } 63 64 // variant of find_index which does not allocate if not found (yields -1) 65 int maybe_find_index(jobject h); 66 67 // returns the size of the generated oop table, for sizing the CodeBlob. 68 // must be called after all oops are allocated! 69 int oop_size(); 70 71 // Retrieve the oop handle at a given index. 72 jobject handle_at(int index); 73 74 int element_count() { 75 // there is always a NULL virtually present as first object 76 return _handles->length() + first_index; 77 } 78 79 // copy the generated oop table to nmethod 80 void copy_to(nmethod* nm); // => nm->copy_oops(_handles) 81 82 bool is_unused() { return _handles == NULL && !_complete; } 83 #ifdef ASSERT 84 bool is_complete() { return _complete; } 85 #endif 86 87 private: 88 // leaky hash table of handle => index, to help detect duplicate insertion 89 class IndexCache: public ResourceObj { 90 // This class is only used by the OopRecorder class. 91 friend class OopRecorder; 92 enum { 93 _log_cache_size = 9, 94 _cache_size = (1<<_log_cache_size), 95 // Index entries are ints. The LSBit is a collision indicator. 96 _collision_bit_shift = 0, 97 _collision_bit = 1, 98 _index_shift = _collision_bit_shift+1 99 }; 100 int _cache[_cache_size]; 101 static juint cache_index(jobject handle) { 102 juint ci = (int) (intptr_t) handle; 103 ci ^= ci >> (BitsPerByte*2); 104 ci += ci >> (BitsPerByte*1); 105 return ci & (_cache_size-1); 106 } 107 int* cache_location(jobject handle) { 108 return &_cache[ cache_index(handle) ]; 109 } 110 static bool cache_location_collision(int* cloc) { 111 return ((*cloc) & _collision_bit) != 0; 112 } 113 static int cache_location_index(int* cloc) { 114 return (*cloc) >> _index_shift; 115 } 116 static void set_cache_location_index(int* cloc, int index) { 117 int cval0 = (*cloc); 118 int cval1 = (index << _index_shift); 119 if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit; 120 (*cloc) = cval1; 121 } 122 IndexCache(); 123 }; 124 125 // Helper function; returns false for NULL or Universe::non_oop_word(). 126 inline bool is_real_jobject(jobject h); 127 128 void maybe_initialize(); 129 int add_handle(jobject h, bool make_findable); 130 131 enum { null_index = 0, first_index = 1, index_cache_threshold = 20 }; 132 133 GrowableArray<jobject>* _handles; // ordered list (first is always NULL) 134 GrowableArray<int>* _no_finds; // all unfindable indexes; usually empty 135 IndexCache* _indexes; // map: jobject -> its probable index 136 Arena* _arena; 137 bool _complete; 138 139 #ifdef ASSERT 140 static int _find_index_calls, _hit_indexes, _missed_indexes; 141 #endif 142 }; 143 144 #endif // SHARE_VM_CODE_OOPRECORDER_HPP