1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/code/oopRecorder.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,136 @@ 1.4 +/* 1.5 + * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +// Recording and retrieval of oop relocations in compiled code. 1.29 + 1.30 +class CodeBlob; 1.31 + 1.32 +class OopRecorder : public ResourceObj { 1.33 + public: 1.34 + // A two-way mapping from positive indexes to oop handles. 1.35 + // The zero index is reserved for a constant (sharable) null. 1.36 + // Indexes may not be negative. 1.37 + 1.38 + // Use the given arena to manage storage, if not NULL. 1.39 + // By default, uses the current ResourceArea. 1.40 + OopRecorder(Arena* arena = NULL); 1.41 + 1.42 + // Generate a new index on which CodeBlob::oop_addr_at will work. 1.43 + // allocate_index and find_index never return the same index, 1.44 + // and allocate_index never returns the same index twice. 1.45 + // In fact, two successive calls to allocate_index return successive ints. 1.46 + int allocate_index(jobject h) { 1.47 + return add_handle(h, false); 1.48 + } 1.49 + 1.50 + // For a given jobject, this will return the same index repeatedly. 1.51 + // The index can later be given to oop_at to retrieve the oop. 1.52 + // However, the oop must not be changed via CodeBlob::oop_addr_at. 1.53 + int find_index(jobject h) { 1.54 + int index = maybe_find_index(h); 1.55 + if (index < 0) { // previously unallocated 1.56 + index = add_handle(h, true); 1.57 + } 1.58 + return index; 1.59 + } 1.60 + 1.61 + // variant of find_index which does not allocate if not found (yields -1) 1.62 + int maybe_find_index(jobject h); 1.63 + 1.64 + // returns the size of the generated oop table, for sizing the CodeBlob. 1.65 + // must be called after all oops are allocated! 1.66 + int oop_size(); 1.67 + 1.68 + // Retrieve the oop handle at a given index. 1.69 + jobject handle_at(int index); 1.70 + 1.71 + int element_count() { 1.72 + // there is always a NULL virtually present as first object 1.73 + return _handles->length() + first_index; 1.74 + } 1.75 + 1.76 + // copy the generated oop table to CodeBlob 1.77 + void copy_to(CodeBlob* code); // => code->copy_oops(_handles) 1.78 + 1.79 + bool is_unused() { return _handles == NULL && !_complete; } 1.80 +#ifdef ASSERT 1.81 + bool is_complete() { return _complete; } 1.82 +#endif 1.83 + 1.84 + private: 1.85 + // leaky hash table of handle => index, to help detect duplicate insertion 1.86 + class IndexCache: public ResourceObj { 1.87 + // This class is only used by the OopRecorder class. 1.88 + friend class OopRecorder; 1.89 + enum { 1.90 + _log_cache_size = 9, 1.91 + _cache_size = (1<<_log_cache_size), 1.92 + // Index entries are ints. The LSBit is a collision indicator. 1.93 + _collision_bit_shift = 0, 1.94 + _collision_bit = 1, 1.95 + _index_shift = _collision_bit_shift+1 1.96 + }; 1.97 + int _cache[_cache_size]; 1.98 + static juint cache_index(jobject handle) { 1.99 + juint ci = (int) (intptr_t) handle; 1.100 + ci ^= ci >> (BitsPerByte*2); 1.101 + ci += ci >> (BitsPerByte*1); 1.102 + return ci & (_cache_size-1); 1.103 + } 1.104 + int* cache_location(jobject handle) { 1.105 + return &_cache[ cache_index(handle) ]; 1.106 + } 1.107 + static bool cache_location_collision(int* cloc) { 1.108 + return ((*cloc) & _collision_bit) != 0; 1.109 + } 1.110 + static int cache_location_index(int* cloc) { 1.111 + return (*cloc) >> _index_shift; 1.112 + } 1.113 + static void set_cache_location_index(int* cloc, int index) { 1.114 + int cval0 = (*cloc); 1.115 + int cval1 = (index << _index_shift); 1.116 + if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit; 1.117 + (*cloc) = cval1; 1.118 + } 1.119 + IndexCache(); 1.120 + }; 1.121 + 1.122 + // Helper function; returns false for NULL or Universe::non_oop_word(). 1.123 + inline bool is_real_jobject(jobject h); 1.124 + 1.125 + void maybe_initialize(); 1.126 + int add_handle(jobject h, bool make_findable); 1.127 + 1.128 + enum { null_index = 0, first_index = 1, index_cache_threshold = 20 }; 1.129 + 1.130 + GrowableArray<jobject>* _handles; // ordered list (first is always NULL) 1.131 + GrowableArray<int>* _no_finds; // all unfindable indexes; usually empty 1.132 + IndexCache* _indexes; // map: jobject -> its probable index 1.133 + Arena* _arena; 1.134 + bool _complete; 1.135 + 1.136 +#ifdef ASSERT 1.137 + static int _find_index_calls, _hit_indexes, _missed_indexes; 1.138 +#endif 1.139 +};