1 /*
   2  * Copyright (c) 2013, 2014, 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 #include "precompiled.hpp"
  26 #include "gc_implementation/g1/g1BiasedArray.hpp"
  27 #include "memory/padded.inline.hpp"
  28 
  29 // Allocate a new array, generic version.
  30 address G1BiasedMappedArrayBase::create_new_base_array(size_t length, size_t elem_size) {
  31   assert(length > 0, "just checking");
  32   assert(elem_size > 0, "just checking");
  33   return PaddedPrimitiveArray<u_char, mtGC>::create_unfreeable(length * elem_size);
  34 }
  35 
  36 #ifndef PRODUCT
  37 void G1BiasedMappedArrayBase::verify_index(idx_t index) const {
  38   guarantee(_base != NULL, "Array not initialized");
  39   guarantee(index < length(), err_msg("Index out of bounds index: "SIZE_FORMAT" length: "SIZE_FORMAT, index, length()));
  40 }
  41 
  42 void G1BiasedMappedArrayBase::verify_biased_index(idx_t biased_index) const {
  43   guarantee(_biased_base != NULL, "Array not initialized");
  44   guarantee(biased_index >= bias() && biased_index < (bias() + length()),
  45     err_msg("Biased index out of bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length()));
  46 }
  47 
  48 void G1BiasedMappedArrayBase::verify_biased_index_inclusive_end(idx_t biased_index) const {
  49   guarantee(_biased_base != NULL, "Array not initialized");
  50   guarantee(biased_index >= bias() && biased_index <= (bias() + length()),
  51     err_msg("Biased index out of inclusive bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length()));
  52 }
  53 
  54 class TestMappedArray : public G1BiasedMappedArray<int> {
  55 protected:
  56   virtual int default_value() const { return 0xBAADBABE; }
  57 public:
  58   static void test_biasedarray() {
  59     const size_t REGION_SIZE_IN_WORDS = 512;
  60     const size_t NUM_REGIONS = 20;
  61     HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero
  62 
  63     TestMappedArray array;
  64     array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS,
  65             REGION_SIZE_IN_WORDS * HeapWordSize);
  66     // Check address calculation (bounds)
  67     assert(array.bottom_address_mapped() == fake_heap,
  68       err_msg("bottom mapped address should be " PTR_FORMAT ", but is " PTR_FORMAT, p2i(fake_heap), p2i(array.bottom_address_mapped())));
  69     assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be");
  70 
  71     int* bottom = array.address_mapped_to(fake_heap);
  72     assert((void*)bottom == (void*) array.base(), "must be");
  73     int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS);
  74     assert((void*)end == (void*)(array.base() + array.length()), "must be");
  75     // The entire array should contain default value elements
  76     for (int* current = bottom; current < end; current++) {
  77       assert(*current == array.default_value(), "must be");
  78     }
  79 
  80     // Test setting values in the table
  81 
  82     HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2);
  83     HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1);
  84 
  85     // Set/get by address tests: invert some value; first retrieve one
  86     int actual_value = array.get_by_index(NUM_REGIONS / 2);
  87     array.set_by_index(NUM_REGIONS / 2, ~actual_value);
  88     // Get the same value by address, should correspond to the start of the "region"
  89     int value = array.get_by_address(region_start_address);
  90     assert(value == ~actual_value, "must be");
  91     // Get the same value by address, at one HeapWord before the start
  92     value = array.get_by_address(region_start_address - 1);
  93     assert(value == array.default_value(), "must be");
  94     // Get the same value by address, at the end of the "region"
  95     value = array.get_by_address(region_end_address);
  96     assert(value == ~actual_value, "must be");
  97     // Make sure the next value maps to another index
  98     value = array.get_by_address(region_end_address + 1);
  99     assert(value == array.default_value(), "must be");
 100 
 101     // Reset the value in the array
 102     array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value);
 103 
 104     // The entire array should have the default value again
 105     for (int* current = bottom; current < end; current++) {
 106       assert(*current == array.default_value(), "must be");
 107     }
 108 
 109     // Set/get by index tests: invert some value
 110     idx_t index = NUM_REGIONS / 2;
 111     actual_value = array.get_by_index(index);
 112     array.set_by_index(index, ~actual_value);
 113 
 114     value = array.get_by_index(index);
 115     assert(value == ~actual_value, "must be");
 116 
 117     value = array.get_by_index(index - 1);
 118     assert(value == array.default_value(), "must be");
 119 
 120     value = array.get_by_index(index + 1);
 121     assert(value == array.default_value(), "must be");
 122 
 123     array.set_by_index(0, 0);
 124     value = array.get_by_index(0);
 125     assert(value == 0, "must be");
 126 
 127     array.set_by_index(array.length() - 1, 0);
 128     value = array.get_by_index(array.length() - 1);
 129     assert(value == 0, "must be");
 130 
 131     array.set_by_index(index, 0);
 132 
 133     // The array should have three zeros, and default values otherwise
 134     size_t num_zeros = 0;
 135     for (int* current = bottom; current < end; current++) {
 136       assert(*current == array.default_value() || *current == 0, "must be");
 137       if (*current == 0) {
 138         num_zeros++;
 139       }
 140     }
 141     assert(num_zeros == 3, "must be");
 142   }
 143 };
 144 
 145 void TestG1BiasedArray_test() {
 146   TestMappedArray::test_biasedarray();
 147 }
 148 
 149 #endif