Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/utilities/jfrRefCountPointer.hpp
38920 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP25#define SHARE_VM_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP2627#include "jfr/utilities/jfrAllocation.hpp"28#include "runtime/atomic.hpp"2930template <typename T>31class RefCountHandle {32template <typename, typename>33friend class RefCountPointer;34private:35const T* _ptr;3637RefCountHandle(const T* ptr) : _ptr(ptr) {38assert(_ptr != NULL, "invariant");39_ptr->add_ref();40}4142public:43RefCountHandle() : _ptr(NULL) {}4445RefCountHandle(const RefCountHandle<T>& rhs) : _ptr(rhs._ptr) {46if (_ptr != NULL) {47_ptr->add_ref();48}49}5051~RefCountHandle() {52if (_ptr != NULL) {53const T* temp = _ptr;54_ptr = NULL;55temp->remove_ref();56}57}5859// The copy-and-swap idiom upholds reference counting semantics60void operator=(RefCountHandle<T> rhs) {61const T* temp = rhs._ptr;62rhs._ptr = _ptr;63_ptr = temp;64}6566bool operator==(const RefCountHandle<T>& rhs) const {67return _ptr == rhs._ptr;68}6970bool operator!=(const RefCountHandle<T>& rhs) const {71return !operator==(rhs);72}7374bool valid() const {75return _ptr != NULL;76}7778const T & operator->() const {79return *_ptr;80}8182T& operator->() {83return *const_cast<T*>(_ptr);84}85};8687class MultiThreadedRefCounter {88private:89mutable volatile int _refs;90public:91MultiThreadedRefCounter() : _refs(0) {}9293void inc() const {94Atomic::add(1, &_refs);95}9697bool dec() const {98return 0 == Atomic::add((-1), &_refs);99}100101int current() const {102return _refs;103}104};105106template <typename T, typename RefCountImpl = MultiThreadedRefCounter>107class RefCountPointer : public JfrCHeapObj {108template <typename>109friend class RefCountHandle;110typedef RefCountHandle<RefCountPointer<T, RefCountImpl> > RefHandle;111private:112const T* _ptr;113mutable RefCountImpl _refs;114115// disallow multiple copies116RefCountPointer(const RefCountPointer<T, RefCountImpl>& rhs);117void operator=(const RefCountPointer<T, RefCountImpl>& rhs);118119~RefCountPointer() {120assert(_refs.current() == 0, "invariant");121delete const_cast<T*>(_ptr);122}123124void add_ref() const {125_refs.inc();126}127128void remove_ref() const {129if (_refs.dec()) {130delete this;131}132}133134RefCountPointer(const T* ptr) : _ptr(ptr), _refs() {135assert(_ptr != NULL, "invariant");136}137138public:139const T* operator->() const {140return _ptr;141}142143T* operator->() {144return const_cast<T*>(_ptr);145}146147static RefHandle make(const T* ptr) {148assert(ptr != NULL, "invariant");149return RefHandle(new RefCountPointer<T, RefCountImpl>(ptr));150}151};152153#endif // SHARE_VM_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP154155156