OVR_RefCount.cpp 2.7 KB

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