summaryrefslogtreecommitdiffhomepage
path: root/ovr_sdk_win_23.0.0/LibOVRKernel/Src/Kernel/OVR_RefCount.cpp
blob: e31906382011f31a6c883f9fbb83f0a2919c1c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/************************************************************************************

Filename    :   OVR_RefCount.cpp
Content     :   Reference counting implementation
Created     :   September 19, 2012
Notes       :

Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.

Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

https://developer.oculus.com/licenses/oculusmastersdk-1.0

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#include "OVR_RefCount.h"
#include "OVR_Atomic.h"

namespace OVR {

// ***** Reference Count Base implementation

RefCountImplCore::~RefCountImplCore() {
  // RefCount can be either 1 or 0 here.
  //  0 if Release() was properly called.
  //  1 if the object was declared on stack or as an aggregate.
  OVR_ASSERT(RefCount <= 1);
}

#ifdef OVR_BUILD_DEBUG
void RefCountImplCore::reportInvalidDelete(void* /*pmem*/) {
  // Invalid delete call on ref-counted object. Please use Release()
  OVR_FAIL();
}
#endif

RefCountNTSImplCore::~RefCountNTSImplCore() {
  // RefCount can be either 1 or 0 here.
  //  0 if Release() was properly called.
  //  1 if the object was declared on stack or as an aggregate.
  OVR_ASSERT(RefCount <= 1);
}

#ifdef OVR_BUILD_DEBUG
void RefCountNTSImplCore::reportInvalidDelete(void* /*pmem*/) {
  // Invalid delete call on ref-counted object. Please use Release()
  OVR_FAIL();
}
#endif

// *** Thread-Safe RefCountImpl

void RefCountImpl::AddRef() {
  RefCount.fetch_add(1, std::memory_order_relaxed);
}
void RefCountImpl::Release() {
  if (RefCount.fetch_add(-1, std::memory_order_relaxed) - 1 == 0)
    delete this;
}

// *** Thread-Safe RefCountVImpl w/virtual AddRef/Release

void RefCountVImpl::AddRef() {
  RefCount.fetch_add(1, std::memory_order_relaxed);
}
void RefCountVImpl::Release() {
  if (RefCount.fetch_add(-1, std::memory_order_relaxed) - 1 == 0)
    delete this;
}

// *** NON-Thread-Safe RefCountImpl

void RefCountNTSImpl::Release() const {
  RefCount--;
  if (RefCount == 0)
    delete this;
}

} // namespace OVR