aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "ci/ciEnv.hpp" aoqi@0: #include "ci/ciInstance.hpp" aoqi@0: #include "ci/ciMetadata.hpp" aoqi@0: #include "code/oopRecorder.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: template int ValueRecorder::_find_index_calls = 0; aoqi@0: template int ValueRecorder::_hit_indexes = 0; aoqi@0: template int ValueRecorder::_missed_indexes = 0; aoqi@0: #endif //ASSERT aoqi@0: aoqi@0: aoqi@0: template ValueRecorder::ValueRecorder(Arena* arena) { aoqi@0: _handles = NULL; aoqi@0: _indexes = NULL; aoqi@0: _arena = arena; aoqi@0: _complete = false; aoqi@0: } aoqi@0: aoqi@0: template template ValueRecorder::IndexCache::IndexCache() { aoqi@0: assert(first_index > 0, "initial zero state of cache must be invalid index"); aoqi@0: Copy::zero_to_bytes(&_cache[0], sizeof(_cache)); aoqi@0: } aoqi@0: aoqi@0: template int ValueRecorder::size() { aoqi@0: _complete = true; aoqi@0: if (_handles == NULL) return 0; aoqi@0: return _handles->length() * sizeof(T); aoqi@0: } aoqi@0: aoqi@0: template void ValueRecorder::copy_values_to(nmethod* nm) { aoqi@0: assert(_complete, "must be frozen"); aoqi@0: maybe_initialize(); // get non-null handles, even if we have no oops aoqi@0: nm->copy_values(_handles); aoqi@0: } aoqi@0: aoqi@0: template void ValueRecorder::maybe_initialize() { aoqi@0: if (_handles == NULL) { aoqi@0: if (_arena != NULL) { aoqi@0: _handles = new(_arena) GrowableArray(_arena, 10, 0, 0); aoqi@0: _no_finds = new(_arena) GrowableArray( _arena, 10, 0, 0); aoqi@0: } else { aoqi@0: _handles = new GrowableArray(10, 0, 0); aoqi@0: _no_finds = new GrowableArray( 10, 0, 0); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: template T ValueRecorder::at(int index) { aoqi@0: // there is always a NULL virtually present as first object aoqi@0: if (index == null_index) return NULL; aoqi@0: return _handles->at(index - first_index); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: template int ValueRecorder::add_handle(T h, bool make_findable) { aoqi@0: assert(!_complete, "cannot allocate more elements after size query"); aoqi@0: maybe_initialize(); aoqi@0: // indexing uses 1 as an origin--0 means null aoqi@0: int index = _handles->length() + first_index; aoqi@0: _handles->append(h); aoqi@0: aoqi@0: // Support correct operation of find_index(). aoqi@0: assert(!(make_findable && !is_real(h)), "nulls are not findable"); aoqi@0: if (make_findable) { aoqi@0: // This index may be returned from find_index(). aoqi@0: if (_indexes != NULL) { aoqi@0: int* cloc = _indexes->cache_location(h); aoqi@0: _indexes->set_cache_location_index(cloc, index); aoqi@0: } else if (index == index_cache_threshold && _arena != NULL) { aoqi@0: _indexes = new(_arena) IndexCache(); aoqi@0: for (int i = 0; i < _handles->length(); i++) { aoqi@0: // Load the cache with pre-existing elements. aoqi@0: int index0 = i + first_index; aoqi@0: if (_no_finds->contains(index0)) continue; aoqi@0: int* cloc = _indexes->cache_location(_handles->at(i)); aoqi@0: _indexes->set_cache_location_index(cloc, index0); aoqi@0: } aoqi@0: } aoqi@0: } else if (is_real(h)) { aoqi@0: // Remember that this index is not to be returned from find_index(). aoqi@0: // This case is rare, because most or all uses of allocate_index pass aoqi@0: // an argument of NULL or Universe::non_oop_word. aoqi@0: // Thus, the expected length of _no_finds is zero. aoqi@0: _no_finds->append(index); aoqi@0: } aoqi@0: aoqi@0: return index; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: template int ValueRecorder::maybe_find_index(T h) { aoqi@0: debug_only(_find_index_calls++); aoqi@0: assert(!_complete, "cannot allocate more elements after size query"); aoqi@0: maybe_initialize(); aoqi@0: if (h == NULL) return null_index; aoqi@0: assert(is_real(h), "must be valid"); aoqi@0: int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h); aoqi@0: if (cloc != NULL) { aoqi@0: int cindex = _indexes->cache_location_index(cloc); aoqi@0: if (cindex == 0) { aoqi@0: return -1; // We know this handle is completely new. aoqi@0: } aoqi@0: if (cindex >= first_index && _handles->at(cindex - first_index) == h) { aoqi@0: debug_only(_hit_indexes++); aoqi@0: return cindex; aoqi@0: } aoqi@0: if (!_indexes->cache_location_collision(cloc)) { aoqi@0: return -1; // We know the current cache occupant is unique to that cloc. aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Not found in cache, due to a cache collision. (Or, no cache at all.) aoqi@0: // Do a linear search, most recent to oldest. aoqi@0: for (int i = _handles->length() - 1; i >= 0; i--) { aoqi@0: if (_handles->at(i) == h) { aoqi@0: int findex = i + first_index; aoqi@0: if (_no_finds->contains(findex)) continue; // oops; skip this one aoqi@0: if (cloc != NULL) { aoqi@0: _indexes->set_cache_location_index(cloc, findex); aoqi@0: } aoqi@0: debug_only(_missed_indexes++); aoqi@0: return findex; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: // Explicitly instantiate these types aoqi@0: template class ValueRecorder; aoqi@0: template class ValueRecorder;