nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: Filename : OVR_RefCount.cpp nuclear@0: Content : Reference counting implementation nuclear@0: Created : September 19, 2012 nuclear@0: Notes : nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #include "OVR_RefCount.h" nuclear@0: #include "OVR_Atomic.h" nuclear@0: #include "OVR_Log.h" nuclear@0: nuclear@0: namespace OVR { nuclear@0: nuclear@0: #ifdef OVR_CC_ARM nuclear@0: void* ReturnArg0(void* p) nuclear@0: { nuclear@0: return p; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: // ***** Reference Count Base implementation nuclear@0: nuclear@0: RefCountImplCore::~RefCountImplCore() nuclear@0: { nuclear@0: // RefCount can be either 1 or 0 here. nuclear@0: // 0 if Release() was properly called. nuclear@0: // 1 if the object was declared on stack or as an aggregate. nuclear@0: OVR_ASSERT(RefCount <= 1); nuclear@0: } nuclear@0: nuclear@0: #ifdef OVR_BUILD_DEBUG nuclear@0: void RefCountImplCore::reportInvalidDelete(void *pmem) nuclear@0: { nuclear@0: OVR_DEBUG_LOG( nuclear@0: ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem)); nuclear@0: OVR_ASSERT(0); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: RefCountNTSImplCore::~RefCountNTSImplCore() nuclear@0: { nuclear@0: // RefCount can be either 1 or 0 here. nuclear@0: // 0 if Release() was properly called. nuclear@0: // 1 if the object was declared on stack or as an aggregate. nuclear@0: OVR_ASSERT(RefCount <= 1); nuclear@0: } nuclear@0: nuclear@0: #ifdef OVR_BUILD_DEBUG nuclear@0: void RefCountNTSImplCore::reportInvalidDelete(void *pmem) nuclear@0: { nuclear@0: OVR_DEBUG_LOG( nuclear@0: ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem)); nuclear@0: OVR_ASSERT(0); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: // *** Thread-Safe RefCountImpl nuclear@0: nuclear@0: void RefCountImpl::AddRef() nuclear@0: { nuclear@0: AtomicOps::ExchangeAdd_NoSync(&RefCount, 1); nuclear@0: } nuclear@0: void RefCountImpl::Release() nuclear@0: { nuclear@0: if ((AtomicOps::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0) nuclear@0: delete this; nuclear@0: } nuclear@0: nuclear@0: // *** Thread-Safe RefCountVImpl w/virtual AddRef/Release nuclear@0: nuclear@0: void RefCountVImpl::AddRef() nuclear@0: { nuclear@0: AtomicOps::ExchangeAdd_NoSync(&RefCount, 1); nuclear@0: } nuclear@0: void RefCountVImpl::Release() nuclear@0: { nuclear@0: if ((AtomicOps::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0) nuclear@0: delete this; nuclear@0: } nuclear@0: nuclear@0: // *** NON-Thread-Safe RefCountImpl nuclear@0: nuclear@0: void RefCountNTSImpl::Release() const nuclear@0: { nuclear@0: RefCount--; nuclear@0: if (RefCount == 0) nuclear@0: delete this; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: } // OVR