Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1BiasedArray.hpp
38920 views
/*1* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP25#define SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP2627#include "memory/allocation.hpp"28#include "utilities/debug.hpp"2930// Implements the common base functionality for arrays that contain provisions31// for accessing its elements using a biased index.32// The element type is defined by the instantiating the template.33class G1BiasedMappedArrayBase VALUE_OBJ_CLASS_SPEC {34friend class VMStructs;35public:36typedef size_t idx_t;37protected:38address _base; // the real base address39size_t _length; // the length of the array40address _biased_base; // base address biased by "bias" elements41size_t _bias; // the bias, i.e. the offset biased_base is located to the right in elements42uint _shift_by; // the amount of bits to shift right when mapping to an index of the array.4344protected:4546G1BiasedMappedArrayBase() : _base(NULL), _length(0), _biased_base(NULL),47_bias(0), _shift_by(0) { }4849// Allocate a new array, generic version.50static address create_new_base_array(size_t length, size_t elem_size);5152// Initialize the members of this class. The biased start address of this array53// is the bias (in elements) multiplied by the element size.54void initialize_base(address base, size_t length, size_t bias, size_t elem_size, uint shift_by) {55assert(base != NULL, "just checking");56assert(length > 0, "just checking");57assert(shift_by < sizeof(uintptr_t) * 8, err_msg("Shifting by " SSIZE_FORMAT ", larger than word size?", (size_t) shift_by));58_base = base;59_length = length;60_biased_base = base - (bias * elem_size);61_bias = bias;62_shift_by = shift_by;63}6465// Allocate and initialize this array to cover the heap addresses in the range66// of [bottom, end).67void initialize(HeapWord* bottom, HeapWord* end, size_t target_elem_size_in_bytes, size_t mapping_granularity_in_bytes) {68assert(mapping_granularity_in_bytes > 0, "just checking");69assert(is_power_of_2(mapping_granularity_in_bytes),70err_msg("mapping granularity must be power of 2, is %zd", mapping_granularity_in_bytes));71assert((uintptr_t)bottom % mapping_granularity_in_bytes == 0,72err_msg("bottom mapping area address must be a multiple of mapping granularity %zd, is " PTR_FORMAT,73mapping_granularity_in_bytes, p2i(bottom)));74assert((uintptr_t)end % mapping_granularity_in_bytes == 0,75err_msg("end mapping area address must be a multiple of mapping granularity %zd, is " PTR_FORMAT,76mapping_granularity_in_bytes, p2i(end)));77size_t num_target_elems = pointer_delta(end, bottom, mapping_granularity_in_bytes);78idx_t bias = (uintptr_t)bottom / mapping_granularity_in_bytes;79address base = create_new_base_array(num_target_elems, target_elem_size_in_bytes);80initialize_base(base, num_target_elems, bias, target_elem_size_in_bytes, log2_intptr(mapping_granularity_in_bytes));81}8283size_t bias() const { return _bias; }84uint shift_by() const { return _shift_by; }8586void verify_index(idx_t index) const PRODUCT_RETURN;87void verify_biased_index(idx_t biased_index) const PRODUCT_RETURN;88void verify_biased_index_inclusive_end(idx_t biased_index) const PRODUCT_RETURN;8990public:91// Return the length of the array in elements.92size_t length() const { return _length; }93};9495// Array that provides biased access and mapping from (valid) addresses in the96// heap into this array.97template<class T>98class G1BiasedMappedArray : public G1BiasedMappedArrayBase {99public:100typedef G1BiasedMappedArrayBase::idx_t idx_t;101102T* base() const { return (T*)G1BiasedMappedArrayBase::_base; }103// Return the element of the given array at the given index. Assume104// the index is valid. This is a convenience method that does sanity105// checking on the index.106T get_by_index(idx_t index) const {107verify_index(index);108return this->base()[index];109}110111// Set the element of the given array at the given index to the112// given value. Assume the index is valid. This is a convenience113// method that does sanity checking on the index.114void set_by_index(idx_t index, T value) {115verify_index(index);116this->base()[index] = value;117}118119// The raw biased base pointer.120T* biased_base() const { return (T*)G1BiasedMappedArrayBase::_biased_base; }121122// Return the element of the given array that covers the given word in the123// heap. Assumes the index is valid.124T get_by_address(HeapWord* value) const {125idx_t biased_index = ((uintptr_t)value) >> this->shift_by();126this->verify_biased_index(biased_index);127return biased_base()[biased_index];128}129130// Set the value of the array entry that corresponds to the given array.131void set_by_address(HeapWord * address, T value) {132idx_t biased_index = ((uintptr_t)address) >> this->shift_by();133this->verify_biased_index(biased_index);134biased_base()[biased_index] = value;135}136137protected:138// Returns the address of the element the given address maps to139T* address_mapped_to(HeapWord* address) {140idx_t biased_index = ((uintptr_t)address) >> this->shift_by();141this->verify_biased_index_inclusive_end(biased_index);142return biased_base() + biased_index;143}144145public:146// Return the smallest address (inclusive) in the heap that this array covers.147HeapWord* bottom_address_mapped() const {148return (HeapWord*) ((uintptr_t)this->bias() << this->shift_by());149}150151// Return the highest address (exclusive) in the heap that this array covers.152HeapWord* end_address_mapped() const {153return (HeapWord*) ((uintptr_t)(this->bias() + this->length()) << this->shift_by());154}155156protected:157virtual T default_value() const = 0;158// Set all elements of the given array to the given value.159void clear() {160T value = default_value();161for (idx_t i = 0; i < length(); i++) {162set_by_index(i, value);163}164}165public:166G1BiasedMappedArray() {}167168// Allocate and initialize this array to cover the heap addresses in the range169// of [bottom, end).170void initialize(HeapWord* bottom, HeapWord* end, size_t mapping_granularity) {171G1BiasedMappedArrayBase::initialize(bottom, end, sizeof(T), mapping_granularity);172this->clear();173}174};175176#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP177178179