hotspot/src/share/vm/gc/g1/g1BiasedArray.cpp
changeset 41732 25dcacf3d009
parent 33105 294e48b4f704
equal deleted inserted replaced
41722:07042d1b3876 41732:25dcacf3d009
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    51   guarantee(biased_index >= bias() && biased_index <= (bias() + length()),
    51   guarantee(biased_index >= bias() && biased_index <= (bias() + length()),
    52             "Biased index out of inclusive bounds, index: " SIZE_FORMAT " bias: " SIZE_FORMAT " length: " SIZE_FORMAT,
    52             "Biased index out of inclusive bounds, index: " SIZE_FORMAT " bias: " SIZE_FORMAT " length: " SIZE_FORMAT,
    53             biased_index, bias(), length());
    53             biased_index, bias(), length());
    54 }
    54 }
    55 
    55 
    56 class TestMappedArray : public G1BiasedMappedArray<int> {
       
    57 protected:
       
    58   virtual int default_value() const { return 0xBAADBABE; }
       
    59 public:
       
    60   static void test_biasedarray() {
       
    61     const size_t REGION_SIZE_IN_WORDS = 512;
       
    62     const size_t NUM_REGIONS = 20;
       
    63     HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero
       
    64 
       
    65     TestMappedArray array;
       
    66     array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS,
       
    67             REGION_SIZE_IN_WORDS * HeapWordSize);
       
    68     // Check address calculation (bounds)
       
    69     assert(array.bottom_address_mapped() == fake_heap,
       
    70            "bottom mapped address should be " PTR_FORMAT ", but is " PTR_FORMAT, p2i(fake_heap), p2i(array.bottom_address_mapped()));
       
    71     assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be");
       
    72 
       
    73     int* bottom = array.address_mapped_to(fake_heap);
       
    74     assert((void*)bottom == (void*) array.base(), "must be");
       
    75     int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS);
       
    76     assert((void*)end == (void*)(array.base() + array.length()), "must be");
       
    77     // The entire array should contain default value elements
       
    78     for (int* current = bottom; current < end; current++) {
       
    79       assert(*current == array.default_value(), "must be");
       
    80     }
       
    81 
       
    82     // Test setting values in the table
       
    83 
       
    84     HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2);
       
    85     HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1);
       
    86 
       
    87     // Set/get by address tests: invert some value; first retrieve one
       
    88     int actual_value = array.get_by_index(NUM_REGIONS / 2);
       
    89     array.set_by_index(NUM_REGIONS / 2, ~actual_value);
       
    90     // Get the same value by address, should correspond to the start of the "region"
       
    91     int value = array.get_by_address(region_start_address);
       
    92     assert(value == ~actual_value, "must be");
       
    93     // Get the same value by address, at one HeapWord before the start
       
    94     value = array.get_by_address(region_start_address - 1);
       
    95     assert(value == array.default_value(), "must be");
       
    96     // Get the same value by address, at the end of the "region"
       
    97     value = array.get_by_address(region_end_address);
       
    98     assert(value == ~actual_value, "must be");
       
    99     // Make sure the next value maps to another index
       
   100     value = array.get_by_address(region_end_address + 1);
       
   101     assert(value == array.default_value(), "must be");
       
   102 
       
   103     // Reset the value in the array
       
   104     array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value);
       
   105 
       
   106     // The entire array should have the default value again
       
   107     for (int* current = bottom; current < end; current++) {
       
   108       assert(*current == array.default_value(), "must be");
       
   109     }
       
   110 
       
   111     // Set/get by index tests: invert some value
       
   112     idx_t index = NUM_REGIONS / 2;
       
   113     actual_value = array.get_by_index(index);
       
   114     array.set_by_index(index, ~actual_value);
       
   115 
       
   116     value = array.get_by_index(index);
       
   117     assert(value == ~actual_value, "must be");
       
   118 
       
   119     value = array.get_by_index(index - 1);
       
   120     assert(value == array.default_value(), "must be");
       
   121 
       
   122     value = array.get_by_index(index + 1);
       
   123     assert(value == array.default_value(), "must be");
       
   124 
       
   125     array.set_by_index(0, 0);
       
   126     value = array.get_by_index(0);
       
   127     assert(value == 0, "must be");
       
   128 
       
   129     array.set_by_index(array.length() - 1, 0);
       
   130     value = array.get_by_index(array.length() - 1);
       
   131     assert(value == 0, "must be");
       
   132 
       
   133     array.set_by_index(index, 0);
       
   134 
       
   135     // The array should have three zeros, and default values otherwise
       
   136     size_t num_zeros = 0;
       
   137     for (int* current = bottom; current < end; current++) {
       
   138       assert(*current == array.default_value() || *current == 0, "must be");
       
   139       if (*current == 0) {
       
   140         num_zeros++;
       
   141       }
       
   142     }
       
   143     assert(num_zeros == 3, "must be");
       
   144   }
       
   145 };
       
   146 
       
   147 void TestG1BiasedArray_test() {
       
   148   TestMappedArray::test_biasedarray();
       
   149 }
       
   150 
       
   151 #endif
    56 #endif