ovr_sdk

view 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 source
1 /************************************************************************************
3 Filename : OVR_RefCount.cpp
4 Content : Reference counting implementation
5 Created : September 19, 2012
6 Notes :
8 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
10 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
15 You may obtain a copy of the License at
17 http://www.oculusvr.com/licenses/LICENSE-3.2
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
25 ************************************************************************************/
27 #include "OVR_RefCount.h"
28 #include "OVR_Atomic.h"
29 #include "OVR_Log.h"
31 namespace OVR {
33 #ifdef OVR_CC_ARM
34 void* ReturnArg0(void* p)
35 {
36 return p;
37 }
38 #endif
40 // ***** Reference Count Base implementation
42 RefCountImplCore::~RefCountImplCore()
43 {
44 // RefCount can be either 1 or 0 here.
45 // 0 if Release() was properly called.
46 // 1 if the object was declared on stack or as an aggregate.
47 OVR_ASSERT(RefCount <= 1);
48 }
50 #ifdef OVR_BUILD_DEBUG
51 void RefCountImplCore::reportInvalidDelete(void *pmem)
52 {
53 OVR_DEBUG_LOG(
54 ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem));
55 OVR_ASSERT(0);
56 }
57 #endif
59 RefCountNTSImplCore::~RefCountNTSImplCore()
60 {
61 // RefCount can be either 1 or 0 here.
62 // 0 if Release() was properly called.
63 // 1 if the object was declared on stack or as an aggregate.
64 OVR_ASSERT(RefCount <= 1);
65 }
67 #ifdef OVR_BUILD_DEBUG
68 void RefCountNTSImplCore::reportInvalidDelete(void *pmem)
69 {
70 OVR_DEBUG_LOG(
71 ("Invalid delete call on ref-counted object at %p. Please use Release()", pmem));
72 OVR_ASSERT(0);
73 }
74 #endif
77 // *** Thread-Safe RefCountImpl
79 void RefCountImpl::AddRef()
80 {
81 AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, 1);
82 }
83 void RefCountImpl::Release()
84 {
85 if ((AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0)
86 delete this;
87 }
89 // *** Thread-Safe RefCountVImpl w/virtual AddRef/Release
91 void RefCountVImpl::AddRef()
92 {
93 AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, 1);
94 }
95 void RefCountVImpl::Release()
96 {
97 if ((AtomicOps<int>::ExchangeAdd_NoSync(&RefCount, -1) - 1) == 0)
98 delete this;
99 }
101 // *** NON-Thread-Safe RefCountImpl
103 void RefCountNTSImpl::Release() const
104 {
105 RefCount--;
106 if (RefCount == 0)
107 delete this;
108 }
111 } // OVR