src/share/vm/gc_implementation/g1/g1BiasedArray.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP
aoqi@0 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "utilities/debug.hpp"
aoqi@0 30
aoqi@0 31 // Implements the common base functionality for arrays that contain provisions
aoqi@0 32 // for accessing its elements using a biased index.
aoqi@0 33 // The element type is defined by the instantiating the template.
aoqi@0 34 class G1BiasedMappedArrayBase VALUE_OBJ_CLASS_SPEC {
aoqi@0 35 friend class VMStructs;
aoqi@0 36 public:
aoqi@0 37 typedef size_t idx_t;
aoqi@0 38 protected:
aoqi@0 39 address _base; // the real base address
aoqi@0 40 size_t _length; // the length of the array
aoqi@0 41 address _biased_base; // base address biased by "bias" elements
aoqi@0 42 size_t _bias; // the bias, i.e. the offset biased_base is located to the right in elements
aoqi@0 43 uint _shift_by; // the amount of bits to shift right when mapping to an index of the array.
aoqi@0 44
aoqi@0 45 protected:
aoqi@0 46
aoqi@0 47 G1BiasedMappedArrayBase() : _base(NULL), _length(0), _biased_base(NULL),
aoqi@0 48 _bias(0), _shift_by(0) { }
aoqi@0 49
aoqi@0 50 // Allocate a new array, generic version.
aoqi@0 51 static address create_new_base_array(size_t length, size_t elem_size);
aoqi@0 52
aoqi@0 53 // Initialize the members of this class. The biased start address of this array
aoqi@0 54 // is the bias (in elements) multiplied by the element size.
aoqi@0 55 void initialize_base(address base, size_t length, size_t bias, size_t elem_size, uint shift_by) {
aoqi@0 56 assert(base != NULL, "just checking");
aoqi@0 57 assert(length > 0, "just checking");
aoqi@0 58 assert(shift_by < sizeof(uintptr_t) * 8, err_msg("Shifting by " SSIZE_FORMAT ", larger than word size?", (size_t) shift_by));
aoqi@0 59 _base = base;
aoqi@0 60 _length = length;
aoqi@0 61 _biased_base = base - (bias * elem_size);
aoqi@0 62 _bias = bias;
aoqi@0 63 _shift_by = shift_by;
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 // Allocate and initialize this array to cover the heap addresses in the range
aoqi@0 67 // of [bottom, end).
aoqi@0 68 void initialize(HeapWord* bottom, HeapWord* end, size_t target_elem_size_in_bytes, size_t mapping_granularity_in_bytes) {
aoqi@0 69 assert(mapping_granularity_in_bytes > 0, "just checking");
aoqi@0 70 assert(is_power_of_2(mapping_granularity_in_bytes),
aoqi@0 71 err_msg("mapping granularity must be power of 2, is %zd", mapping_granularity_in_bytes));
aoqi@0 72 assert((uintptr_t)bottom % mapping_granularity_in_bytes == 0,
aoqi@0 73 err_msg("bottom mapping area address must be a multiple of mapping granularity %zd, is " PTR_FORMAT,
aoqi@0 74 mapping_granularity_in_bytes, p2i(bottom)));
aoqi@0 75 assert((uintptr_t)end % mapping_granularity_in_bytes == 0,
aoqi@0 76 err_msg("end mapping area address must be a multiple of mapping granularity %zd, is " PTR_FORMAT,
aoqi@0 77 mapping_granularity_in_bytes, p2i(end)));
aoqi@0 78 size_t num_target_elems = (end - bottom) / (mapping_granularity_in_bytes / HeapWordSize);
aoqi@0 79 idx_t bias = (uintptr_t)bottom / mapping_granularity_in_bytes;
aoqi@0 80 address base = create_new_base_array(num_target_elems, target_elem_size_in_bytes);
aoqi@0 81 initialize_base(base, num_target_elems, bias, target_elem_size_in_bytes, log2_intptr(mapping_granularity_in_bytes));
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 size_t bias() const { return _bias; }
aoqi@0 85 uint shift_by() const { return _shift_by; }
aoqi@0 86
aoqi@0 87 void verify_index(idx_t index) const PRODUCT_RETURN;
aoqi@0 88 void verify_biased_index(idx_t biased_index) const PRODUCT_RETURN;
aoqi@0 89 void verify_biased_index_inclusive_end(idx_t biased_index) const PRODUCT_RETURN;
aoqi@0 90
aoqi@0 91 public:
aoqi@0 92 // Return the length of the array in elements.
aoqi@0 93 size_t length() const { return _length; }
aoqi@0 94 };
aoqi@0 95
aoqi@0 96 // Array that provides biased access and mapping from (valid) addresses in the
aoqi@0 97 // heap into this array.
aoqi@0 98 template<class T>
aoqi@0 99 class G1BiasedMappedArray : public G1BiasedMappedArrayBase {
aoqi@0 100 public:
aoqi@0 101 typedef G1BiasedMappedArrayBase::idx_t idx_t;
aoqi@0 102
aoqi@0 103 T* base() const { return (T*)G1BiasedMappedArrayBase::_base; }
aoqi@0 104 // Return the element of the given array at the given index. Assume
aoqi@0 105 // the index is valid. This is a convenience method that does sanity
aoqi@0 106 // checking on the index.
aoqi@0 107 T get_by_index(idx_t index) const {
aoqi@0 108 verify_index(index);
aoqi@0 109 return this->base()[index];
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 // Set the element of the given array at the given index to the
aoqi@0 113 // given value. Assume the index is valid. This is a convenience
aoqi@0 114 // method that does sanity checking on the index.
aoqi@0 115 void set_by_index(idx_t index, T value) {
aoqi@0 116 verify_index(index);
aoqi@0 117 this->base()[index] = value;
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 // The raw biased base pointer.
aoqi@0 121 T* biased_base() const { return (T*)G1BiasedMappedArrayBase::_biased_base; }
aoqi@0 122
aoqi@0 123 // Return the element of the given array that covers the given word in the
aoqi@0 124 // heap. Assumes the index is valid.
aoqi@0 125 T get_by_address(HeapWord* value) const {
aoqi@0 126 idx_t biased_index = ((uintptr_t)value) >> this->shift_by();
aoqi@0 127 this->verify_biased_index(biased_index);
aoqi@0 128 return biased_base()[biased_index];
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 // Set the value of the array entry that corresponds to the given array.
aoqi@0 132 void set_by_address(HeapWord * address, T value) {
aoqi@0 133 idx_t biased_index = ((uintptr_t)address) >> this->shift_by();
aoqi@0 134 this->verify_biased_index(biased_index);
aoqi@0 135 biased_base()[biased_index] = value;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 protected:
aoqi@0 139 // Returns the address of the element the given address maps to
aoqi@0 140 T* address_mapped_to(HeapWord* address) {
aoqi@0 141 idx_t biased_index = ((uintptr_t)address) >> this->shift_by();
aoqi@0 142 this->verify_biased_index_inclusive_end(biased_index);
aoqi@0 143 return biased_base() + biased_index;
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public:
aoqi@0 147 // Return the smallest address (inclusive) in the heap that this array covers.
aoqi@0 148 HeapWord* bottom_address_mapped() const {
aoqi@0 149 return (HeapWord*) ((uintptr_t)this->bias() << this->shift_by());
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 // Return the highest address (exclusive) in the heap that this array covers.
aoqi@0 153 HeapWord* end_address_mapped() const {
aoqi@0 154 return (HeapWord*) ((uintptr_t)(this->bias() + this->length()) << this->shift_by());
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 protected:
aoqi@0 158 virtual T default_value() const = 0;
aoqi@0 159 // Set all elements of the given array to the given value.
aoqi@0 160 void clear() {
aoqi@0 161 T value = default_value();
aoqi@0 162 for (idx_t i = 0; i < length(); i++) {
aoqi@0 163 set_by_index(i, value);
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 public:
aoqi@0 167 G1BiasedMappedArray() {}
aoqi@0 168
aoqi@0 169 // Allocate and initialize this array to cover the heap addresses in the range
aoqi@0 170 // of [bottom, end).
aoqi@0 171 void initialize(HeapWord* bottom, HeapWord* end, size_t mapping_granularity) {
aoqi@0 172 G1BiasedMappedArrayBase::initialize(bottom, end, sizeof(T), mapping_granularity);
aoqi@0 173 this->clear();
aoqi@0 174 }
aoqi@0 175 };
aoqi@0 176
aoqi@0 177 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1BIASEDARRAY_HPP

mercurial