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 "runtime/mutex.hpp"
  29 #include "runtime/os.hpp"
  30 #include "utilities/debug.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 // +1 for NULL singular entry.
  35 OopStorage* OopStorageSet::storages[all_count + 1] = {};
  36 
  37 static Mutex* make_oopstorage_mutex(const char* storage_name,
  38                                     const char* kind,
  39                                     int rank) {
  40   char name[256];
  41   os::snprintf(name, sizeof(name), "%s %s lock", storage_name, kind);
  42   return new PaddedMutex(rank, name, true, Mutex::_safepoint_check_never);
  43 }
  44 
  45 static OopStorage* make_oopstorage(const char* name) {
  46   Mutex* alloc = make_oopstorage_mutex(name, "alloc", Mutex::oopstorage);
  47   Mutex* active = make_oopstorage_mutex(name, "active", Mutex::oopstorage - 1);
  48   return new OopStorage(name, alloc, active);
  49 }
  50 
  51 void OopStorageSet::initialize() {
  52   storages[jni_global_index]        = make_oopstorage("JNI global");
  53   storages[vm_global_index]         = make_oopstorage("VM global");
  54   storages[jni_weak_index]          = make_oopstorage("JNI weak");
  55   storages[vm_weak_index]           = make_oopstorage("VM weak");
  56   storages[string_table_weak_index] = make_oopstorage("StringTable weak");
  57   storages[resolved_method_table_weak_index] =
  58     make_oopstorage("ResolvedMethodTable weak");
  59 
  60   // Ensure we have all of them.
  61   STATIC_ASSERT(all_count == 6);
  62   assert(storages[singular_index] == NULL, "postcondition");
  63 #ifdef ASSERT
  64   for (uint i = all_start; i < all_end; ++i) {
  65     assert(storages[i] != NULL, "postcondition");
  66   }
  67 #endif // ASSERT
  68 }
  69 
  70 void oopstorage_init() {
  71   OopStorageSet::initialize();
  72 }
  73 
  74 #ifdef ASSERT
  75 
  76 void OopStorageSet::verify_initialized(uint index) {
  77   assert(storages[index] != NULL, "oopstorage_init not yet called");
  78 }
  79 
  80 void OopStorageSet::Iterator::verify_nonsingular() const {
  81   assert(_category != singular, "precondition");
  82 }
  83 
  84 void OopStorageSet::Iterator::verify_category_match(const Iterator& other) const {
  85   verify_nonsingular();
  86   assert(_category == other._category, "precondition");
  87 }
  88 
  89 void OopStorageSet::Iterator::verify_dereferenceable() const {
  90   verify_nonsingular();
  91   assert(!is_end(), "precondition");
  92 }
  93 
  94 #endif // ASSERT