oculus1

annotate libovr/Src/Kernel/OVR_RefCount.cpp @ 1:e2f9e4603129

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