tschatzl@5773: /* tschatzl@5773: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. tschatzl@5773: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@5773: * tschatzl@5773: * This code is free software; you can redistribute it and/or modify it tschatzl@5773: * under the terms of the GNU General Public License version 2 only, as tschatzl@5773: * published by the Free Software Foundation. tschatzl@5773: * tschatzl@5773: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@5773: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@5773: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@5773: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@5773: * accompanied this code). tschatzl@5773: * tschatzl@5773: * You should have received a copy of the GNU General Public License version tschatzl@5773: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@5773: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@5773: * tschatzl@5773: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@5773: * or visit www.oracle.com if you need additional information or have any tschatzl@5773: * questions. tschatzl@5773: * tschatzl@5773: */ tschatzl@5773: tschatzl@5773: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP tschatzl@5773: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP tschatzl@5773: tschatzl@6409: #include "memory/allocation.hpp" tschatzl@5773: #include "utilities/debug.hpp" tschatzl@5773: tschatzl@5773: // Implements the common base functionality for arrays that contain provisions tschatzl@5773: // for accessing its elements using a biased index. tschatzl@5773: // The element type is defined by the instantiating the template. tschatzl@5773: class G1BiasedMappedArrayBase VALUE_OBJ_CLASS_SPEC { tschatzl@5773: friend class VMStructs; tschatzl@5773: public: tschatzl@5773: typedef size_t idx_t; tschatzl@5773: protected: tschatzl@5773: address _base; // the real base address tschatzl@5773: size_t _length; // the length of the array tschatzl@5773: address _biased_base; // base address biased by "bias" elements tschatzl@5773: size_t _bias; // the bias, i.e. the offset biased_base is located to the right in elements tschatzl@5773: uint _shift_by; // the amount of bits to shift right when mapping to an index of the array. tschatzl@5773: tschatzl@5773: protected: tschatzl@5773: tschatzl@5773: G1BiasedMappedArrayBase() : _base(NULL), _length(0), _biased_base(NULL), tschatzl@5773: _bias(0), _shift_by(0) { } tschatzl@5773: tschatzl@5773: // Allocate a new array, generic version. tschatzl@6409: static address create_new_base_array(size_t length, size_t elem_size); tschatzl@5773: tschatzl@5773: // Initialize the members of this class. The biased start address of this array tschatzl@5773: // is the bias (in elements) multiplied by the element size. tschatzl@5773: void initialize_base(address base, size_t length, size_t bias, size_t elem_size, uint shift_by) { tschatzl@5773: assert(base != NULL, "just checking"); tschatzl@5773: assert(length > 0, "just checking"); tschatzl@5773: assert(shift_by < sizeof(uintptr_t) * 8, err_msg("Shifting by %zd, larger than word size?", shift_by)); tschatzl@5773: _base = base; tschatzl@5773: _length = length; tschatzl@5773: _biased_base = base - (bias * elem_size); tschatzl@5773: _bias = bias; tschatzl@5773: _shift_by = shift_by; tschatzl@5773: } tschatzl@5773: tschatzl@5773: // Allocate and initialize this array to cover the heap addresses in the range tschatzl@5773: // of [bottom, end). tschatzl@5773: void initialize(HeapWord* bottom, HeapWord* end, size_t target_elem_size_in_bytes, size_t mapping_granularity_in_bytes) { tschatzl@5773: assert(mapping_granularity_in_bytes > 0, "just checking"); tschatzl@5773: assert(is_power_of_2(mapping_granularity_in_bytes), tschatzl@5773: err_msg("mapping granularity must be power of 2, is %zd", mapping_granularity_in_bytes)); tschatzl@5773: assert((uintptr_t)bottom % mapping_granularity_in_bytes == 0, tschatzl@5773: err_msg("bottom mapping area address must be a multiple of mapping granularity %zd, is "PTR_FORMAT, tschatzl@5773: mapping_granularity_in_bytes, bottom)); tschatzl@5773: assert((uintptr_t)end % mapping_granularity_in_bytes == 0, tschatzl@5773: err_msg("end mapping area address must be a multiple of mapping granularity %zd, is "PTR_FORMAT, tschatzl@5773: mapping_granularity_in_bytes, end)); tschatzl@5773: size_t num_target_elems = (end - bottom) / (mapping_granularity_in_bytes / HeapWordSize); tschatzl@5773: idx_t bias = (uintptr_t)bottom / mapping_granularity_in_bytes; tschatzl@5773: address base = create_new_base_array(num_target_elems, target_elem_size_in_bytes); tschatzl@5773: initialize_base(base, num_target_elems, bias, target_elem_size_in_bytes, log2_intptr(mapping_granularity_in_bytes)); tschatzl@5773: } tschatzl@5773: tschatzl@5773: size_t bias() const { return _bias; } tschatzl@5773: uint shift_by() const { return _shift_by; } tschatzl@5773: tschatzl@5773: void verify_index(idx_t index) const PRODUCT_RETURN; tschatzl@5773: void verify_biased_index(idx_t biased_index) const PRODUCT_RETURN; tschatzl@5773: void verify_biased_index_inclusive_end(idx_t biased_index) const PRODUCT_RETURN; tschatzl@5773: tschatzl@5773: public: tschatzl@5773: // Return the length of the array in elements. tschatzl@5773: size_t length() const { return _length; } tschatzl@5773: }; tschatzl@5773: tschatzl@5773: // Array that provides biased access and mapping from (valid) addresses in the tschatzl@5773: // heap into this array. tschatzl@5773: template tschatzl@5773: class G1BiasedMappedArray : public G1BiasedMappedArrayBase { tschatzl@5773: public: tschatzl@5773: typedef G1BiasedMappedArrayBase::idx_t idx_t; tschatzl@5773: tschatzl@5773: T* base() const { return (T*)G1BiasedMappedArrayBase::_base; } tschatzl@5773: // Return the element of the given array at the given index. Assume tschatzl@5773: // the index is valid. This is a convenience method that does sanity tschatzl@5773: // checking on the index. tschatzl@5773: T get_by_index(idx_t index) const { tschatzl@5773: verify_index(index); tschatzl@5773: return this->base()[index]; tschatzl@5773: } tschatzl@5773: tschatzl@5773: // Set the element of the given array at the given index to the tschatzl@5773: // given value. Assume the index is valid. This is a convenience tschatzl@5773: // method that does sanity checking on the index. tschatzl@5773: void set_by_index(idx_t index, T value) { tschatzl@5773: verify_index(index); tschatzl@5773: this->base()[index] = value; tschatzl@5773: } tschatzl@5773: tschatzl@5773: // The raw biased base pointer. tschatzl@5773: T* biased_base() const { return (T*)G1BiasedMappedArrayBase::_biased_base; } tschatzl@5773: tschatzl@5773: // Return the element of the given array that covers the given word in the tschatzl@5773: // heap. Assumes the index is valid. tschatzl@5773: T get_by_address(HeapWord* value) const { tschatzl@5773: idx_t biased_index = ((uintptr_t)value) >> this->shift_by(); tschatzl@5773: this->verify_biased_index(biased_index); tschatzl@5773: return biased_base()[biased_index]; tschatzl@5773: } tschatzl@5773: tschatzl@5773: // Set the value of the array entry that corresponds to the given array. tschatzl@5773: void set_by_address(HeapWord * address, T value) { tschatzl@5773: idx_t biased_index = ((uintptr_t)address) >> this->shift_by(); tschatzl@5773: this->verify_biased_index(biased_index); tschatzl@5773: biased_base()[biased_index] = value; tschatzl@5773: } tschatzl@5773: tschatzl@5773: protected: tschatzl@5773: // Returns the address of the element the given address maps to tschatzl@5773: T* address_mapped_to(HeapWord* address) { tschatzl@5773: idx_t biased_index = ((uintptr_t)address) >> this->shift_by(); tschatzl@5773: this->verify_biased_index_inclusive_end(biased_index); tschatzl@5773: return biased_base() + biased_index; tschatzl@5773: } tschatzl@5773: tschatzl@5773: public: tschatzl@5773: // Return the smallest address (inclusive) in the heap that this array covers. tschatzl@5773: HeapWord* bottom_address_mapped() const { tschatzl@5773: return (HeapWord*) ((uintptr_t)this->bias() << this->shift_by()); tschatzl@5773: } tschatzl@5773: tschatzl@5773: // Return the highest address (exclusive) in the heap that this array covers. tschatzl@5773: HeapWord* end_address_mapped() const { tschatzl@5773: return (HeapWord*) ((uintptr_t)(this->bias() + this->length()) << this->shift_by()); tschatzl@5773: } tschatzl@5773: tschatzl@5773: protected: tschatzl@5773: virtual T default_value() const = 0; tschatzl@5773: // Set all elements of the given array to the given value. tschatzl@5773: void clear() { tschatzl@5773: T value = default_value(); tschatzl@5773: for (idx_t i = 0; i < length(); i++) { tschatzl@5773: set_by_index(i, value); tschatzl@5773: } tschatzl@5773: } tschatzl@5773: public: tschatzl@5773: G1BiasedMappedArray() {} tschatzl@5773: tschatzl@5773: // Allocate and initialize this array to cover the heap addresses in the range tschatzl@5773: // of [bottom, end). tschatzl@5773: void initialize(HeapWord* bottom, HeapWord* end, size_t mapping_granularity) { tschatzl@5773: G1BiasedMappedArrayBase::initialize(bottom, end, sizeof(T), mapping_granularity); tschatzl@5773: this->clear(); tschatzl@5773: } tschatzl@5773: }; tschatzl@5773: tschatzl@5773: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP