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