1 /*
 2  * Copyright (c) 2018, 2020, 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 "oops/access.inline.hpp"
28 #include "oops/oop.hpp"
29 #include "oops/weakHandle.inline.hpp"
30 #include "utilities/debug.hpp"
31 #include "utilities/ostream.hpp"
32 
33 WeakHandle::WeakHandle(OopStorage* storage, Handle obj) :
34     WeakHandle(storage, obj()) {}
35 
36 WeakHandle::WeakHandle(OopStorage* storage, oop obj) :
37     _obj(storage->allocate()) {
38   assert(obj != NULL, "no need to create weak null oop");
39 
40   if (_obj == NULL) {
41     vm_exit_out_of_memory(sizeof(oop*), OOM_MALLOC_ERROR,
42                           "Unable to create new weak oop handle in OopStorage %s",
43                           storage->name());
44   }
45 
46   NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(_obj, obj);
47 }
48 
49 void WeakHandle::release(OopStorage* storage) const {
50   // Only release if the pointer to the object has been created.
51   if (_obj != NULL) {
52     // Clear the WeakHandle.  For race in creating ClassLoaderData, we can release this
53     // WeakHandle before it is cleared by GC.
54     NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(_obj, (oop)NULL);
55     storage->release(_obj);
56   }
57 }
58 
59 WeakHandle WeakHandle::from_raw(oop* raw) {
60   assert(raw != NULL, "can't create from raw with NULL value");
61   return WeakHandle(raw);
62 }
63 
64 void WeakHandle::print() const { print_on(tty); }
65 
66 void WeakHandle::print_on(outputStream* st) const {
67   st->print("WeakHandle: " PTR_FORMAT, p2i(peek()));
68 }
--- EOF ---