Path: blob/master/src/hotspot/share/jfr/utilities/jfrRefCountPointer.hpp
41149 views
/*1* Copyright (c) 2017, 2021, 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_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP25#define SHARE_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP2627#include "jfr/utilities/jfrAllocation.hpp"28#include "runtime/atomic.hpp"2930template <typename T>31class RefCountHandle {32private:33const T* _ptr;3435RefCountHandle(const T* ptr) : _ptr(ptr) {36assert(_ptr != NULL, "invariant");37_ptr->add_ref();38}3940public:41RefCountHandle() : _ptr(NULL) {}4243RefCountHandle(const RefCountHandle<T>& rhs) : _ptr(rhs._ptr) {44if (_ptr != NULL) {45_ptr->add_ref();46}47}4849~RefCountHandle() {50if (_ptr != NULL) {51_ptr->remove_ref();52_ptr = NULL;53}54}5556// The copy-and-swap idiom upholds reference counting semantics57void operator=(RefCountHandle<T> rhs) {58const T* temp = rhs._ptr;59rhs._ptr = _ptr;60_ptr = temp;61}6263bool operator==(const RefCountHandle<T>& rhs) const {64return _ptr == rhs._ptr;65}6667bool operator!=(const RefCountHandle<T>& rhs) const {68return !operator==(rhs);69}7071bool valid() const {72return _ptr != NULL;73}7475const T& operator->() const {76return *_ptr;77}7879T& operator->() {80return *const_cast<T*>(_ptr);81}8283static RefCountHandle<T> make(const T* ptr) {84return ptr;85}86};8788class SingleThreadedRefCounter {89private:90mutable intptr_t _refs;91public:92SingleThreadedRefCounter() : _refs(0) {}9394void inc() const {95++_refs;96}9798bool dec() const {99return --_refs == 0;100}101102intptr_t current() const {103return _refs;104}105};106107class MultiThreadedRefCounter {108private:109mutable volatile intptr_t _refs;110public:111MultiThreadedRefCounter() : _refs(0) {}112113void inc() const {114Atomic::add(&_refs, 1);115}116117bool dec() const {118return 0 == Atomic::add(&_refs, (-1));119}120121intptr_t current() const {122return _refs;123}124};125126template <typename T, typename RefCountImpl = MultiThreadedRefCounter>127class RefCountPointer : public JfrCHeapObj {128template <typename>129friend class RefCountHandle;130typedef RefCountHandle<RefCountPointer<T, RefCountImpl> > RefHandle;131private:132const T* _ptr;133mutable RefCountImpl _refs;134135// disallow multiple copies136RefCountPointer(const RefCountPointer<T, RefCountImpl>& rhs);137void operator=(const RefCountPointer<T, RefCountImpl>& rhs);138139~RefCountPointer() {140assert(_refs.current() == 0, "invariant");141delete const_cast<T*>(_ptr);142}143144void add_ref() const {145_refs.inc();146}147148void remove_ref() const {149if (_refs.dec()) {150delete this;151}152}153154RefCountPointer(const T* ptr) : _ptr(ptr), _refs() {155assert(_ptr != NULL, "invariant");156}157158public:159const T* operator->() const {160return _ptr;161}162163T* operator->() {164return const_cast<T*>(_ptr);165}166167static RefHandle make(const T* ptr) {168return RefHandle::make(new RefCountPointer<T, RefCountImpl>(ptr));169}170};171172#endif // SHARE_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP173174175