ovr_sdk

diff LibOVR/Src/Kernel/OVR_RefCount.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/Kernel/OVR_RefCount.cpp	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,111 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   OVR_RefCount.cpp
     1.7 +Content     :   Reference counting implementation
     1.8 +Created     :   September 19, 2012
     1.9 +Notes       : 
    1.10 +
    1.11 +Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.
    1.12 +
    1.13 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.14 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.15 +which is provided at the time of installation or download, or which 
    1.16 +otherwise accompanies this software in either electronic or hard copy form.
    1.17 +
    1.18 +You may obtain a copy of the License at
    1.19 +
    1.20 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.21 +
    1.22 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.23 +distributed under the License is distributed on an "AS IS" BASIS,
    1.24 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.25 +See the License for the specific language governing permissions and
    1.26 +limitations under the License.
    1.27 +
    1.28 +************************************************************************************/
    1.29 +
    1.30 +#include "OVR_RefCount.h"
    1.31 +#include "OVR_Atomic.h"
    1.32 +#include "OVR_Log.h"
    1.33 +
    1.34 +namespace OVR {
    1.35 +
    1.36 +#ifdef OVR_CC_ARM
    1.37 +void* ReturnArg0(void* p)
    1.38 +{
    1.39 +    return p;
    1.40 +}
    1.41 +#endif
    1.42 +
    1.43 +// ***** Reference Count Base implementation
    1.44 +
    1.45 +RefCountImplCore::~RefCountImplCore()
    1.46 +{
    1.47 +    // RefCount can be either 1 or 0 here.
    1.48 +    //  0 if Release() was properly called.
    1.49 +    //  1 if the object was declared on stack or as an aggregate.
    1.50 +    OVR_ASSERT(RefCount <= 1);
    1.51 +}
    1.52 +
    1.53 +#ifdef OVR_BUILD_DEBUG
    1.54 +void RefCountImplCore::reportInvalidDelete(void *pmem)
    1.55 +{
    1.56 +    OVR_DEBUG_LOG(
    1.57 +        ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem));
    1.58 +    OVR_ASSERT(0);
    1.59 +}
    1.60 +#endif
    1.61 +
    1.62 +RefCountNTSImplCore::~RefCountNTSImplCore()
    1.63 +{
    1.64 +    // RefCount can be either 1 or 0 here.
    1.65 +    //  0 if Release() was properly called.
    1.66 +    //  1 if the object was declared on stack or as an aggregate.
    1.67 +    OVR_ASSERT(RefCount <= 1);
    1.68 +}
    1.69 +
    1.70 +#ifdef OVR_BUILD_DEBUG
    1.71 +void RefCountNTSImplCore::reportInvalidDelete(void *pmem)
    1.72 +{
    1.73 +    OVR_DEBUG_LOG(
    1.74 +        ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem));
    1.75 +    OVR_ASSERT(0);
    1.76 +}
    1.77 +#endif
    1.78 +
    1.79 +
    1.80 +// *** Thread-Safe RefCountImpl
    1.81 +
    1.82 +void    RefCountImpl::AddRef()
    1.83 +{
    1.84 +    AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, 1);
    1.85 +}
    1.86 +void    RefCountImpl::Release()
    1.87 +{
    1.88 +    if ((AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0)
    1.89 +        delete this;
    1.90 +}
    1.91 +
    1.92 +// *** Thread-Safe RefCountVImpl w/virtual AddRef/Release
    1.93 +
    1.94 +void    RefCountVImpl::AddRef()
    1.95 +{
    1.96 +    AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, 1);
    1.97 +}
    1.98 +void    RefCountVImpl::Release()
    1.99 +{
   1.100 +    if ((AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0)
   1.101 +        delete this;
   1.102 +}
   1.103 +
   1.104 +// *** NON-Thread-Safe RefCountImpl
   1.105 +
   1.106 +void    RefCountNTSImpl::Release() const
   1.107 +{
   1.108 +    RefCount--;
   1.109 +    if (RefCount == 0)
   1.110 +        delete this;
   1.111 +}
   1.112 +
   1.113 +
   1.114 +} // OVR