RavEngine
Loading...
Searching...
No Matches
CmRefCountable.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef CM_REFCOUNTABLE_H
30#define CM_REFCOUNTABLE_H
31
32#include "foundation/PxAssert.h"
33#include "foundation/PxAtomic.h"
34#include "foundation/PxAllocator.h"
35#include "common/PxBase.h"
36
37namespace physx
38{
39namespace Cm
40{
41 // PT: this is used to re-implement RefCountable using the ref-counter in PxBase, i.e. to dissociate
42 // the RefCountable data from the RefCountable code. The goal is to be able to store the ref counter
43 // in the padding bytes of PxBase, and also to avoid two v-table pointers in the class.
45 {
46 public:
47
49
50 void preExportDataReset()
51 {
52 mBuiltInRefCount = 1;
53 }
54
55 void incRefCount()
56 {
57 volatile PxI32* val = reinterpret_cast<volatile PxI32*>(&mBuiltInRefCount);
58 PxAtomicIncrement(val);
59 // value better be greater than 1, or we've created a ref to an undefined object
60 PX_ASSERT(mBuiltInRefCount>1);
61 }
62
63 void decRefCount()
64 {
65 PX_ASSERT(mBuiltInRefCount>0);
66 volatile PxI32* val = reinterpret_cast<volatile PxI32*>(&mBuiltInRefCount);
67 if(physx::PxAtomicDecrement(val) == 0)
68 onRefCountZero();
69 }
70
71 PX_FORCE_INLINE PxU32 getRefCount() const
72 {
73 return mBuiltInRefCount;
74 }
75 };
76
77 PX_FORCE_INLINE void RefCountable_preExportDataReset(PxRefCounted& base) { static_cast<RefCountableExt&>(base).preExportDataReset(); }
78 PX_FORCE_INLINE void RefCountable_incRefCount(PxRefCounted& base) { static_cast<RefCountableExt&>(base).incRefCount(); }
79 PX_FORCE_INLINE void RefCountable_decRefCount(PxRefCounted& base) { static_cast<RefCountableExt&>(base).decRefCount(); }
80 PX_FORCE_INLINE PxU32 RefCountable_getRefCount(const PxRefCounted& base) { return static_cast<const RefCountableExt&>(base).getRefCount(); }
81
82 // simple thread-safe reference count
83 // when the ref count is zero, the object is in an undefined state (pending delete)
84
86 {
87 //= ATTENTION! =====================================================================================
88 // Changing the data layout of this class breaks the binary serialization format. See comments for
89 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
90 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
91 // accordingly.
92 //==================================================================================================
93 public:
94// PX_SERIALIZATION
95 RefCountable(const PxEMPTY) { PX_ASSERT(mRefCount == 1); }
96 void preExportDataReset() { mRefCount = 1; }
97 static void getBinaryMetaData(PxOutputStream& stream);
98//~PX_SERIALIZATION
99 explicit RefCountable(PxU32 initialCount = 1)
100 : mRefCount(PxI32(initialCount))
101 {
102 PX_ASSERT(mRefCount!=0);
103 }
104
105 virtual ~RefCountable() {}
106
111 virtual void onRefCountZero()
112 {
113 PX_DELETE_THIS;
114 }
115
116 void incRefCount()
117 {
118 physx::PxAtomicIncrement(&mRefCount);
119 // value better be greater than 1, or we've created a ref to an undefined object
120 PX_ASSERT(mRefCount>1);
121 }
122
123 void decRefCount()
124 {
125 PX_ASSERT(mRefCount>0);
126 if(physx::PxAtomicDecrement(&mRefCount) == 0)
128 }
129
130 PX_FORCE_INLINE PxU32 getRefCount() const
131 {
132 return PxU32(mRefCount);
133 }
134 private:
135 volatile PxI32 mRefCount;
136 };
137
138
139} // namespace Cm
140
141}
142
143#endif
Definition CmRefCountable.h:45
Definition CmRefCountable.h:86
virtual void onRefCountZero()
Definition CmRefCountable.h:111
Output stream class for I/O.
Definition PxIO.h:114
Base class for ref-counted objects.
Definition PxBase.h:200
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PxEMPTY
Definition Px.h:87