1 /*
   2  * Copyright (c) 2018, 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 "memory/universe.hpp"
  28 #include "oops/access.inline.hpp"
  29 #include "oops/oop.hpp"
  30 #include "oops/weakHandle.hpp"
  31 #include "utilities/debug.hpp"
  32 #include "utilities/ostream.hpp"
  33 
  34 WeakHandle WeakHandle::create(Handle obj) {
  35   assert(obj() != NULL, "no need to create weak null oop");
  36   oop* oop_addr = Universe::vm_weak_oop_storage()->allocate();
  37   if (oop_addr == NULL) {
  38     vm_exit_out_of_memory(sizeof(oop*), OOM_MALLOC_ERROR, "Unable to create new weak oop handle in OopStorage");
  39   }
  40   // Create WeakHandle with address returned and store oop into it.
  41   RootAccess<ON_PHANTOM_OOP_REF>::oop_store(oop_addr, obj());
  42   return WeakHandle(oop_addr);
  43 }
  44 
  45 oop WeakHandle::resolve() const {
  46   if (_obj == NULL) {
  47     return NULL; // for initialization
  48   }
  49   return RootAccess<ON_PHANTOM_OOP_REF>::oop_load(_obj);
  50 }
  51 
  52 oop WeakHandle::peek() const {
  53   if (_obj == NULL) {
  54     return NULL;  // needed?
  55   }
  56   return RootAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(_obj);
  57 }
  58 
  59 void WeakHandle::release() const {
  60   Universe::vm_weak_oop_storage()->release(_obj);
  61 }
  62 
  63 void WeakHandle::print() const { print_on(tty); }
  64 
  65 void WeakHandle::print_on(outputStream* st) const {
  66   st->print("WeakHandle: " PTR_FORMAT, p2i(peek()));
  67 }