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#include "precompiled.hpp"
25#include "gc/g1/g1BiasedArray.hpp"
26#include "unittest.hpp"
27
28class TestMappedArray : public G1BiasedMappedArray<int> {
29public:
30  virtual int default_value() const {
31    return 0xBAADBABE;
32  }
33  int* my_address_mapped_to(HeapWord* address) {
34    return address_mapped_to(address);
35  }
36};
37
38TEST_VM(G1BiasedArray, simple) {
39  const size_t REGION_SIZE_IN_WORDS = 512;
40  const size_t NUM_REGIONS = 20;
41  // Any value that is non-zero
42  HeapWord* fake_heap =
43          (HeapWord*) LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000);
44
45  TestMappedArray array;
46  array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS,
47          REGION_SIZE_IN_WORDS * HeapWordSize);
48  const int DEFAULT_VALUE = array.default_value();
49
50  // Check address calculation (bounds)
51  ASSERT_EQ(fake_heap, array.bottom_address_mapped())
52          << "bottom mapped address should be "
53          << p2i(array.bottom_address_mapped())
54          << ", but is "
55          << p2i(fake_heap);
56  ASSERT_EQ(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS,
57          array.end_address_mapped());
58
59  int* bottom = array.my_address_mapped_to(fake_heap);
60  ASSERT_EQ((void*) bottom, (void*) array.base());
61  int* end = array.my_address_mapped_to(fake_heap +
62          REGION_SIZE_IN_WORDS * NUM_REGIONS);
63  ASSERT_EQ((void*) end, (void*) (array.base() + array.length()));
64  // The entire array should contain default value elements
65  for (int* current = bottom; current < end; current++) {
66    ASSERT_EQ(DEFAULT_VALUE, *current);
67  }
68
69  // Test setting values in the table
70  HeapWord* region_start_address =
71          fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2);
72  HeapWord* region_end_address =
73          fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) +
74          REGION_SIZE_IN_WORDS - 1);
75
76  // Set/get by address tests: invert some value; first retrieve one
77  int actual_value = array.get_by_index(NUM_REGIONS / 2);
78  array.set_by_index(NUM_REGIONS / 2, ~actual_value);
79  // Get the same value by address, should correspond to the start of the "region"
80  int value = array.get_by_address(region_start_address);
81  ASSERT_EQ(value, ~actual_value);
82  // Get the same value by address, at one HeapWord before the start
83  value = array.get_by_address(region_start_address - 1);
84  ASSERT_EQ(DEFAULT_VALUE, value);
85  // Get the same value by address, at the end of the "region"
86  value = array.get_by_address(region_end_address);
87  ASSERT_EQ(value, ~actual_value);
88  // Make sure the next value maps to another index
89  value = array.get_by_address(region_end_address + 1);
90  ASSERT_EQ(DEFAULT_VALUE, value);
91
92  // Reset the value in the array
93  array.set_by_address(region_start_address +
94          (region_end_address - region_start_address) / 2,
95          actual_value);
96
97  // The entire array should have the default value again
98  for (int* current = bottom; current < end; current++) {
99    ASSERT_EQ(DEFAULT_VALUE, *current);
100  }
101
102  // Set/get by index tests: invert some value
103  size_t index = NUM_REGIONS / 2;
104  actual_value = array.get_by_index(index);
105  array.set_by_index(index, ~actual_value);
106
107  value = array.get_by_index(index);
108  ASSERT_EQ(~actual_value, value);
109
110  value = array.get_by_index(index - 1);
111  ASSERT_EQ(DEFAULT_VALUE, value);
112
113  value = array.get_by_index(index + 1);
114  ASSERT_EQ(DEFAULT_VALUE, value);
115
116  array.set_by_index(0, 0);
117  value = array.get_by_index(0);
118  ASSERT_EQ(0, value);
119
120  array.set_by_index(array.length() - 1, 0);
121  value = array.get_by_index(array.length() - 1);
122  ASSERT_EQ(0, value);
123
124  array.set_by_index(index, 0);
125
126  // The array should have three zeros, and default values otherwise
127  size_t num_zeros = 0;
128  for (int* current = bottom; current < end; current++) {
129    ASSERT_TRUE(*current == DEFAULT_VALUE || *current == 0);
130    if (*current == 0) {
131      num_zeros++;
132    }
133  }
134  ASSERT_EQ((size_t) 3, num_zeros);
135}
136
137