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