test/hotspot/gtest/gc/shared/test_oopStorageSet.cpp
changeset 57828 35db8fba55f9
equal deleted inserted replaced
57827:425412369353 57828:35db8fba55f9
       
     1 /*
       
     2  * Copyright (c) 2019, 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/shared/oopStorage.hpp"
       
    27 #include "gc/shared/oopStorageSet.hpp"
       
    28 #include "memory/allocation.inline.hpp"
       
    29 #include "utilities/debug.hpp"
       
    30 #include "utilities/globalDefinitions.hpp"
       
    31 #include "utilities/macros.hpp"
       
    32 #include "unittest.hpp"
       
    33 
       
    34 // GTEST assertions may introduce ODR-uses.  Dodge them.
       
    35 template<typename T> static T no_odr(T x) { return x; }
       
    36 
       
    37 static void fill_strong(OopStorage** storages, size_t size) {
       
    38   ASSERT_EQ(size, no_odr(OopStorageSet::strong_count));
       
    39   STATIC_ASSERT(2 == OopStorageSet::strong_count);
       
    40   storages[0] = OopStorageSet::jni_global();
       
    41   storages[1] = OopStorageSet::vm_global();
       
    42 }
       
    43 
       
    44 static void fill_weak(OopStorage** storages, size_t size) {
       
    45   ASSERT_EQ(size, no_odr(OopStorageSet::weak_count));
       
    46   STATIC_ASSERT(4 == OopStorageSet::weak_count);
       
    47   storages[0] = OopStorageSet::jni_weak();
       
    48   storages[1] = OopStorageSet::vm_weak();
       
    49   storages[2] = OopStorageSet::string_table_weak();
       
    50   storages[3] = OopStorageSet::resolved_method_table_weak();
       
    51 }
       
    52 
       
    53 static void fill_all(OopStorage** storages, size_t size) {
       
    54   ASSERT_EQ(size, no_odr(OopStorageSet::all_count));
       
    55   const uint strong_count = OopStorageSet::strong_count;
       
    56   fill_strong(storages, strong_count);
       
    57   fill_weak(storages + strong_count, size - strong_count);
       
    58 }
       
    59 
       
    60 // Returns index of s in storages, or size if not found.
       
    61 static size_t find_storage(OopStorage* s, OopStorage** storages, size_t size) {
       
    62   for (uint i = 0; i < size; ++i) {
       
    63     if (s == storages[i]) {
       
    64       return i;
       
    65     }
       
    66   }
       
    67   return size;
       
    68 }
       
    69 
       
    70 static void check_iterator(OopStorageSet::Iterator it,
       
    71                            OopStorage** storages,
       
    72                            size_t size) {
       
    73   OopStorageSet::Iterator start = it;
       
    74   ASSERT_EQ(start, it);
       
    75   for ( ; !it.is_end(); ++it) {
       
    76     size_t index = find_storage(*it, storages, size);
       
    77     ASSERT_LT(index, size);
       
    78     storages[index] = NULL;
       
    79   }
       
    80   ASSERT_NE(start, it);
       
    81   const OopStorage* null_storage = NULL;
       
    82   for (uint i = 0; i < size; ++i) {
       
    83     ASSERT_EQ(null_storage, storages[i]);
       
    84   }
       
    85 }
       
    86 
       
    87 static void test_iterator(uint count,
       
    88                           OopStorageSet::Iterator iterator,
       
    89                           void (*fill)(OopStorage**, size_t)) {
       
    90   OopStorage** storages = NEW_C_HEAP_ARRAY(OopStorage*, count, mtGC);
       
    91   fill(storages, count);
       
    92   check_iterator(iterator, storages, count);
       
    93   FREE_C_HEAP_ARRAY(OopStorage*, storages);
       
    94 }
       
    95 
       
    96 #define TEST_ITERATOR(kind)                                             \
       
    97   TEST_VM(OopStorageSetTest, PASTE_TOKENS(kind, _iterator)) {           \
       
    98     test_iterator(OopStorageSet::PASTE_TOKENS(kind, _count),            \
       
    99                   OopStorageSet::PASTE_TOKENS(kind, _iterator)(),       \
       
   100                   &PASTE_TOKENS(fill_, kind));                          \
       
   101   }
       
   102 
       
   103 TEST_ITERATOR(strong);
       
   104 TEST_ITERATOR(weak)
       
   105 TEST_ITERATOR(all)