Path: blob/master/src/hotspot/share/gc/g1/g1BiasedArray.hpp
40957 views
/*1* Copyright (c) 2013, 2019, 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_GC_G1_G1BIASEDARRAY_HPP25#define SHARE_GC_G1_G1BIASEDARRAY_HPP2627#include "memory/allocation.hpp"28#include "memory/memRegion.hpp"29#include "utilities/debug.hpp"30#include "utilities/powerOfTwo.hpp"3132// Implements the common base functionality for arrays that contain provisions33// for accessing its elements using a biased index.34// The element type is defined by the instantiating the template.35class G1BiasedMappedArrayBase : public CHeapObj<mtGC> {36friend class VMStructs;3738void* _alloc_base; // the address the unpadded array has been allocated to3940public:41typedef size_t idx_t;4243protected:44address _base; // the real base address45size_t _length; // the length of the array46address _biased_base; // base address biased by "bias" elements47size_t _bias; // the bias, i.e. the offset biased_base is located to the right in elements48uint _shift_by; // the amount of bits to shift right when mapping to an index of the array.4950protected:51G1BiasedMappedArrayBase();5253// Allocate a new array, generic version.54address create_new_base_array(size_t length, size_t elem_size);5556// Initialize the members of this class. The biased start address of this array57// is the bias (in elements) multiplied by the element size.58void initialize_base(address base, size_t length, size_t bias, size_t elem_size, uint shift_by) {59assert(base != NULL, "just checking");60assert(length > 0, "just checking");61assert(shift_by < sizeof(uintptr_t) * 8, "Shifting by %u, larger than word size?", shift_by);62_base = base;63_length = length;64_biased_base = base - (bias * elem_size);65_bias = bias;66_shift_by = shift_by;67}6869// Allocate and initialize this array to cover the heap addresses in the range70// of [bottom, end).71void initialize(HeapWord* bottom, HeapWord* end, size_t target_elem_size_in_bytes, size_t mapping_granularity_in_bytes) {72assert(mapping_granularity_in_bytes > 0, "just checking");73assert(is_power_of_2(mapping_granularity_in_bytes),74"mapping granularity must be power of 2, is " SIZE_FORMAT, mapping_granularity_in_bytes);75assert((uintptr_t)bottom % mapping_granularity_in_bytes == 0,76"bottom mapping area address must be a multiple of mapping granularity " SIZE_FORMAT ", is " PTR_FORMAT,77mapping_granularity_in_bytes, p2i(bottom));78assert((uintptr_t)end % mapping_granularity_in_bytes == 0,79"end mapping area address must be a multiple of mapping granularity " SIZE_FORMAT ", is " PTR_FORMAT,80mapping_granularity_in_bytes, p2i(end));81size_t num_target_elems = pointer_delta(end, bottom, mapping_granularity_in_bytes);82idx_t bias = (uintptr_t)bottom / mapping_granularity_in_bytes;83address base = create_new_base_array(num_target_elems, target_elem_size_in_bytes);84initialize_base(base, num_target_elems, bias, target_elem_size_in_bytes, log2i_exact(mapping_granularity_in_bytes));85}8687size_t bias() const { return _bias; }88uint shift_by() const { return _shift_by; }8990void verify_index(idx_t index) const PRODUCT_RETURN;91void verify_biased_index(idx_t biased_index) const PRODUCT_RETURN;92void verify_biased_index_inclusive_end(idx_t biased_index) const PRODUCT_RETURN;9394public:95virtual ~G1BiasedMappedArrayBase();9697// Return the length of the array in elements.98size_t length() const { return _length; }99};100101// Array that provides biased access and mapping from (valid) addresses in the102// heap into this array.103template<class T>104class G1BiasedMappedArray : public G1BiasedMappedArrayBase {105public:106typedef G1BiasedMappedArrayBase::idx_t idx_t;107108T* base() const { return (T*)G1BiasedMappedArrayBase::_base; }109// Return the element of the given array at the given index. Assume110// the index is valid. This is a convenience method that does sanity111// checking on the index.112T get_by_index(idx_t index) const {113verify_index(index);114return this->base()[index];115}116117// Set the element of the given array at the given index to the118// given value. Assume the index is valid. This is a convenience119// method that does sanity checking on the index.120void set_by_index(idx_t index, T value) {121verify_index(index);122this->base()[index] = value;123}124125// The raw biased base pointer.126T* biased_base() const { return (T*)G1BiasedMappedArrayBase::_biased_base; }127128// Return the element of the given array that covers the given word in the129// heap. Assumes the index is valid.130T get_by_address(HeapWord* value) const {131idx_t biased_index = ((uintptr_t)value) >> this->shift_by();132this->verify_biased_index(biased_index);133return biased_base()[biased_index];134}135136T* get_ref_by_index(uintptr_t index) const {137verify_index(index);138return &this->base()[index];139}140141// Return the index of the element of the given array that covers the given142// word in the heap.143idx_t get_index_by_address(HeapWord* value) const {144idx_t biased_index = ((uintptr_t)value) >> this->shift_by();145this->verify_biased_index(biased_index);146return biased_index - _bias;147}148149// Set the value of the array entry that corresponds to the given array.150void set_by_address(HeapWord * address, T value) {151idx_t biased_index = ((uintptr_t)address) >> this->shift_by();152this->verify_biased_index(biased_index);153biased_base()[biased_index] = value;154}155156// Set the value of all array entries that correspond to addresses157// in the specified MemRegion.158void set_by_address(MemRegion range, T value) {159idx_t biased_start = ((uintptr_t)range.start()) >> this->shift_by();160idx_t biased_last = ((uintptr_t)range.last()) >> this->shift_by();161this->verify_biased_index(biased_start);162this->verify_biased_index(biased_last);163for (idx_t i = biased_start; i <= biased_last; i++) {164biased_base()[i] = value;165}166}167168protected:169// Returns the address of the element the given address maps to170T* address_mapped_to(HeapWord* address) {171idx_t biased_index = ((uintptr_t)address) >> this->shift_by();172this->verify_biased_index_inclusive_end(biased_index);173return biased_base() + biased_index;174}175176public:177// Return the smallest address (inclusive) in the heap that this array covers.178HeapWord* bottom_address_mapped() const {179return (HeapWord*) ((uintptr_t)this->bias() << this->shift_by());180}181182// Return the highest address (exclusive) in the heap that this array covers.183HeapWord* end_address_mapped() const {184return (HeapWord*) ((uintptr_t)(this->bias() + this->length()) << this->shift_by());185}186187protected:188virtual T default_value() const = 0;189// Set all elements of the given array to the given value.190void clear() {191T value = default_value();192for (idx_t i = 0; i < length(); i++) {193set_by_index(i, value);194}195}196public:197G1BiasedMappedArray() {}198199// Allocate and initialize this array to cover the heap addresses in the given MemRegion.200void initialize(MemRegion region, size_t mapping_granularity) {201G1BiasedMappedArrayBase::initialize(region.start(), region.end(), sizeof(T), mapping_granularity);202this->clear();203}204};205206#endif // SHARE_GC_G1_G1BIASEDARRAY_HPP207208209