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