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