duke@435: /* duke@435: * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Recording and retrieval of oop relocations in compiled code. duke@435: duke@435: class CodeBlob; duke@435: duke@435: class OopRecorder : public ResourceObj { duke@435: public: duke@435: // A two-way mapping from positive indexes to oop handles. duke@435: // The zero index is reserved for a constant (sharable) null. duke@435: // Indexes may not be negative. duke@435: duke@435: // Use the given arena to manage storage, if not NULL. duke@435: // By default, uses the current ResourceArea. duke@435: OopRecorder(Arena* arena = NULL); duke@435: duke@435: // Generate a new index on which CodeBlob::oop_addr_at will work. duke@435: // allocate_index and find_index never return the same index, duke@435: // and allocate_index never returns the same index twice. duke@435: // In fact, two successive calls to allocate_index return successive ints. duke@435: int allocate_index(jobject h) { duke@435: return add_handle(h, false); duke@435: } duke@435: duke@435: // For a given jobject, this will return the same index repeatedly. duke@435: // The index can later be given to oop_at to retrieve the oop. duke@435: // However, the oop must not be changed via CodeBlob::oop_addr_at. duke@435: int find_index(jobject h) { duke@435: int index = maybe_find_index(h); duke@435: if (index < 0) { // previously unallocated duke@435: index = add_handle(h, true); duke@435: } duke@435: return index; duke@435: } duke@435: duke@435: // variant of find_index which does not allocate if not found (yields -1) duke@435: int maybe_find_index(jobject h); duke@435: duke@435: // returns the size of the generated oop table, for sizing the CodeBlob. duke@435: // must be called after all oops are allocated! duke@435: int oop_size(); duke@435: duke@435: // Retrieve the oop handle at a given index. duke@435: jobject handle_at(int index); duke@435: duke@435: int element_count() { duke@435: // there is always a NULL virtually present as first object duke@435: return _handles->length() + first_index; duke@435: } duke@435: duke@435: // copy the generated oop table to CodeBlob duke@435: void copy_to(CodeBlob* code); // => code->copy_oops(_handles) duke@435: duke@435: bool is_unused() { return _handles == NULL && !_complete; } duke@435: #ifdef ASSERT duke@435: bool is_complete() { return _complete; } duke@435: #endif duke@435: duke@435: private: duke@435: // leaky hash table of handle => index, to help detect duplicate insertion duke@435: class IndexCache: public ResourceObj { duke@435: // This class is only used by the OopRecorder class. duke@435: friend class OopRecorder; duke@435: enum { duke@435: _log_cache_size = 9, duke@435: _cache_size = (1<<_log_cache_size), duke@435: // Index entries are ints. The LSBit is a collision indicator. duke@435: _collision_bit_shift = 0, duke@435: _collision_bit = 1, duke@435: _index_shift = _collision_bit_shift+1 duke@435: }; duke@435: int _cache[_cache_size]; duke@435: static juint cache_index(jobject handle) { duke@435: juint ci = (int) (intptr_t) handle; duke@435: ci ^= ci >> (BitsPerByte*2); duke@435: ci += ci >> (BitsPerByte*1); duke@435: return ci & (_cache_size-1); duke@435: } duke@435: int* cache_location(jobject handle) { duke@435: return &_cache[ cache_index(handle) ]; duke@435: } duke@435: static bool cache_location_collision(int* cloc) { duke@435: return ((*cloc) & _collision_bit) != 0; duke@435: } duke@435: static int cache_location_index(int* cloc) { duke@435: return (*cloc) >> _index_shift; duke@435: } duke@435: static void set_cache_location_index(int* cloc, int index) { duke@435: int cval0 = (*cloc); duke@435: int cval1 = (index << _index_shift); duke@435: if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit; duke@435: (*cloc) = cval1; duke@435: } duke@435: IndexCache(); duke@435: }; duke@435: duke@435: // Helper function; returns false for NULL or Universe::non_oop_word(). duke@435: inline bool is_real_jobject(jobject h); duke@435: duke@435: void maybe_initialize(); duke@435: int add_handle(jobject h, bool make_findable); duke@435: duke@435: enum { null_index = 0, first_index = 1, index_cache_threshold = 20 }; duke@435: duke@435: GrowableArray* _handles; // ordered list (first is always NULL) duke@435: GrowableArray* _no_finds; // all unfindable indexes; usually empty duke@435: IndexCache* _indexes; // map: jobject -> its probable index duke@435: Arena* _arena; duke@435: bool _complete; duke@435: duke@435: #ifdef ASSERT duke@435: static int _find_index_calls, _hit_indexes, _missed_indexes; duke@435: #endif duke@435: };