aoqi@0: /* aoqi@0: * Copyright (c) 2013, 2014, 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: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/debug.hpp" aoqi@0: aoqi@0: // Implements the common base functionality for arrays that contain provisions aoqi@0: // for accessing its elements using a biased index. aoqi@0: // The element type is defined by the instantiating the template. aoqi@0: class G1BiasedMappedArrayBase VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class VMStructs; aoqi@0: public: aoqi@0: typedef size_t idx_t; aoqi@0: protected: aoqi@0: address _base; // the real base address aoqi@0: size_t _length; // the length of the array aoqi@0: address _biased_base; // base address biased by "bias" elements aoqi@0: size_t _bias; // the bias, i.e. the offset biased_base is located to the right in elements aoqi@0: uint _shift_by; // the amount of bits to shift right when mapping to an index of the array. aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: G1BiasedMappedArrayBase() : _base(NULL), _length(0), _biased_base(NULL), aoqi@0: _bias(0), _shift_by(0) { } aoqi@0: aoqi@0: // Allocate a new array, generic version. aoqi@0: static address create_new_base_array(size_t length, size_t elem_size); aoqi@0: aoqi@0: // Initialize the members of this class. The biased start address of this array aoqi@0: // is the bias (in elements) multiplied by the element size. aoqi@0: void initialize_base(address base, size_t length, size_t bias, size_t elem_size, uint shift_by) { aoqi@0: assert(base != NULL, "just checking"); aoqi@0: assert(length > 0, "just checking"); aoqi@0: assert(shift_by < sizeof(uintptr_t) * 8, err_msg("Shifting by " SSIZE_FORMAT ", larger than word size?", (size_t) shift_by)); aoqi@0: _base = base; aoqi@0: _length = length; aoqi@0: _biased_base = base - (bias * elem_size); aoqi@0: _bias = bias; aoqi@0: _shift_by = shift_by; aoqi@0: } aoqi@0: aoqi@0: // Allocate and initialize this array to cover the heap addresses in the range aoqi@0: // of [bottom, end). aoqi@0: void initialize(HeapWord* bottom, HeapWord* end, size_t target_elem_size_in_bytes, size_t mapping_granularity_in_bytes) { aoqi@0: assert(mapping_granularity_in_bytes > 0, "just checking"); aoqi@0: assert(is_power_of_2(mapping_granularity_in_bytes), aoqi@0: err_msg("mapping granularity must be power of 2, is %zd", mapping_granularity_in_bytes)); aoqi@0: assert((uintptr_t)bottom % mapping_granularity_in_bytes == 0, aoqi@0: err_msg("bottom mapping area address must be a multiple of mapping granularity %zd, is " PTR_FORMAT, aoqi@0: mapping_granularity_in_bytes, p2i(bottom))); aoqi@0: assert((uintptr_t)end % mapping_granularity_in_bytes == 0, aoqi@0: err_msg("end mapping area address must be a multiple of mapping granularity %zd, is " PTR_FORMAT, aoqi@0: mapping_granularity_in_bytes, p2i(end))); aoqi@0: size_t num_target_elems = (end - bottom) / (mapping_granularity_in_bytes / HeapWordSize); aoqi@0: idx_t bias = (uintptr_t)bottom / mapping_granularity_in_bytes; aoqi@0: address base = create_new_base_array(num_target_elems, target_elem_size_in_bytes); aoqi@0: initialize_base(base, num_target_elems, bias, target_elem_size_in_bytes, log2_intptr(mapping_granularity_in_bytes)); aoqi@0: } aoqi@0: aoqi@0: size_t bias() const { return _bias; } aoqi@0: uint shift_by() const { return _shift_by; } aoqi@0: aoqi@0: void verify_index(idx_t index) const PRODUCT_RETURN; aoqi@0: void verify_biased_index(idx_t biased_index) const PRODUCT_RETURN; aoqi@0: void verify_biased_index_inclusive_end(idx_t biased_index) const PRODUCT_RETURN; aoqi@0: aoqi@0: public: aoqi@0: // Return the length of the array in elements. aoqi@0: size_t length() const { return _length; } aoqi@0: }; aoqi@0: aoqi@0: // Array that provides biased access and mapping from (valid) addresses in the aoqi@0: // heap into this array. aoqi@0: template aoqi@0: class G1BiasedMappedArray : public G1BiasedMappedArrayBase { aoqi@0: public: aoqi@0: typedef G1BiasedMappedArrayBase::idx_t idx_t; aoqi@0: aoqi@0: T* base() const { return (T*)G1BiasedMappedArrayBase::_base; } aoqi@0: // Return the element of the given array at the given index. Assume aoqi@0: // the index is valid. This is a convenience method that does sanity aoqi@0: // checking on the index. aoqi@0: T get_by_index(idx_t index) const { aoqi@0: verify_index(index); aoqi@0: return this->base()[index]; aoqi@0: } aoqi@0: aoqi@0: // Set the element of the given array at the given index to the aoqi@0: // given value. Assume the index is valid. This is a convenience aoqi@0: // method that does sanity checking on the index. aoqi@0: void set_by_index(idx_t index, T value) { aoqi@0: verify_index(index); aoqi@0: this->base()[index] = value; aoqi@0: } aoqi@0: aoqi@0: // The raw biased base pointer. aoqi@0: T* biased_base() const { return (T*)G1BiasedMappedArrayBase::_biased_base; } aoqi@0: aoqi@0: // Return the element of the given array that covers the given word in the aoqi@0: // heap. Assumes the index is valid. aoqi@0: T get_by_address(HeapWord* value) const { aoqi@0: idx_t biased_index = ((uintptr_t)value) >> this->shift_by(); aoqi@0: this->verify_biased_index(biased_index); aoqi@0: return biased_base()[biased_index]; aoqi@0: } aoqi@0: aoqi@0: // Set the value of the array entry that corresponds to the given array. aoqi@0: void set_by_address(HeapWord * address, T value) { aoqi@0: idx_t biased_index = ((uintptr_t)address) >> this->shift_by(); aoqi@0: this->verify_biased_index(biased_index); aoqi@0: biased_base()[biased_index] = value; aoqi@0: } aoqi@0: aoqi@0: protected: aoqi@0: // Returns the address of the element the given address maps to aoqi@0: T* address_mapped_to(HeapWord* address) { aoqi@0: idx_t biased_index = ((uintptr_t)address) >> this->shift_by(); aoqi@0: this->verify_biased_index_inclusive_end(biased_index); aoqi@0: return biased_base() + biased_index; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: // Return the smallest address (inclusive) in the heap that this array covers. aoqi@0: HeapWord* bottom_address_mapped() const { aoqi@0: return (HeapWord*) ((uintptr_t)this->bias() << this->shift_by()); aoqi@0: } aoqi@0: aoqi@0: // Return the highest address (exclusive) in the heap that this array covers. aoqi@0: HeapWord* end_address_mapped() const { aoqi@0: return (HeapWord*) ((uintptr_t)(this->bias() + this->length()) << this->shift_by()); aoqi@0: } aoqi@0: aoqi@0: protected: aoqi@0: virtual T default_value() const = 0; aoqi@0: // Set all elements of the given array to the given value. aoqi@0: void clear() { aoqi@0: T value = default_value(); aoqi@0: for (idx_t i = 0; i < length(); i++) { aoqi@0: set_by_index(i, value); aoqi@0: } aoqi@0: } aoqi@0: public: aoqi@0: G1BiasedMappedArray() {} aoqi@0: aoqi@0: // Allocate and initialize this array to cover the heap addresses in the range aoqi@0: // of [bottom, end). aoqi@0: void initialize(HeapWord* bottom, HeapWord* end, size_t mapping_granularity) { aoqi@0: G1BiasedMappedArrayBase::initialize(bottom, end, sizeof(T), mapping_granularity); aoqi@0: this->clear(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP