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