oculus1

annotate libovr/Src/Kernel/OVR_RefCount.cpp @ 17:cfe4979ab3eb

ops, minor error in the last commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 07:09:48 +0300
parents e2f9e4603129
children
rev   line source
nuclear@3 1 /************************************************************************************
nuclear@3 2
nuclear@3 3 Filename : OVR_RefCount.cpp
nuclear@3 4 Content : Reference counting implementation
nuclear@3 5 Created : September 19, 2012
nuclear@3 6 Notes :
nuclear@3 7
nuclear@3 8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@3 9
nuclear@3 10 Use of this software is subject to the terms of the Oculus license
nuclear@3 11 agreement provided at the time of installation or download, or which
nuclear@3 12 otherwise accompanies this software in either electronic or hard copy form.
nuclear@3 13
nuclear@3 14 ************************************************************************************/
nuclear@3 15
nuclear@3 16 #include "OVR_RefCount.h"
nuclear@3 17 #include "OVR_Atomic.h"
nuclear@3 18 #include "OVR_Log.h"
nuclear@3 19
nuclear@3 20 namespace OVR {
nuclear@3 21
nuclear@3 22 #ifdef OVR_CC_ARM
nuclear@3 23 void* ReturnArg0(void* p)
nuclear@3 24 {
nuclear@3 25 return p;
nuclear@3 26 }
nuclear@3 27 #endif
nuclear@3 28
nuclear@3 29 // ***** Reference Count Base implementation
nuclear@3 30
nuclear@3 31 RefCountImplCore::~RefCountImplCore()
nuclear@3 32 {
nuclear@3 33 // RefCount can be either 1 or 0 here.
nuclear@3 34 // 0 if Release() was properly called.
nuclear@3 35 // 1 if the object was declared on stack or as an aggregate.
nuclear@3 36 OVR_ASSERT(RefCount <= 1);
nuclear@3 37 }
nuclear@3 38
nuclear@3 39 #ifdef OVR_BUILD_DEBUG
nuclear@3 40 void RefCountImplCore::reportInvalidDelete(void *pmem)
nuclear@3 41 {
nuclear@3 42 OVR_DEBUG_LOG(
nuclear@3 43 ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem));
nuclear@3 44 OVR_ASSERT(0);
nuclear@3 45 }
nuclear@3 46 #endif
nuclear@3 47
nuclear@3 48 RefCountNTSImplCore::~RefCountNTSImplCore()
nuclear@3 49 {
nuclear@3 50 // RefCount can be either 1 or 0 here.
nuclear@3 51 // 0 if Release() was properly called.
nuclear@3 52 // 1 if the object was declared on stack or as an aggregate.
nuclear@3 53 OVR_ASSERT(RefCount <= 1);
nuclear@3 54 }
nuclear@3 55
nuclear@3 56 #ifdef OVR_BUILD_DEBUG
nuclear@3 57 void RefCountNTSImplCore::reportInvalidDelete(void *pmem)
nuclear@3 58 {
nuclear@3 59 OVR_DEBUG_LOG(
nuclear@3 60 ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem));
nuclear@3 61 OVR_ASSERT(0);
nuclear@3 62 }
nuclear@3 63 #endif
nuclear@3 64
nuclear@3 65
nuclear@3 66 // *** Thread-Safe RefCountImpl
nuclear@3 67
nuclear@3 68 void RefCountImpl::AddRef()
nuclear@3 69 {
nuclear@3 70 AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, 1);
nuclear@3 71 }
nuclear@3 72 void RefCountImpl::Release()
nuclear@3 73 {
nuclear@3 74 if ((AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0)
nuclear@3 75 delete this;
nuclear@3 76 }
nuclear@3 77
nuclear@3 78 // *** Thread-Safe RefCountVImpl w/virtual AddRef/Release
nuclear@3 79
nuclear@3 80 void RefCountVImpl::AddRef()
nuclear@3 81 {
nuclear@3 82 AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, 1);
nuclear@3 83 }
nuclear@3 84 void RefCountVImpl::Release()
nuclear@3 85 {
nuclear@3 86 if ((AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0)
nuclear@3 87 delete this;
nuclear@3 88 }
nuclear@3 89
nuclear@3 90 // *** NON-Thread-Safe RefCountImpl
nuclear@3 91
nuclear@3 92 void RefCountNTSImpl::Release() const
nuclear@3 93 {
nuclear@3 94 RefCount--;
nuclear@3 95 if (RefCount == 0)
nuclear@3 96 delete this;
nuclear@3 97 }
nuclear@3 98
nuclear@3 99
nuclear@3 100 } // OVR