Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.1 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: CountedObjects.cpp
  3. //
  4. // Copyright (c) 1999-2000, Microsoft Corporation
  5. //
  6. // Base class that implements object reference counting
  7. //
  8. // History: 1999-08-17 vtan created
  9. // 2000-02-01 vtan moved from Neptune to Whistler
  10. // --------------------------------------------------------------------------
  11. #include "StandardHeader.h"
  12. #include "CountedObject.h"
  13. // --------------------------------------------------------------------------
  14. // CCountedObject::CCountedObject
  15. //
  16. // Arguments: <none>
  17. //
  18. // Returns: <none>
  19. //
  20. // Purpose: Initializes the reference count to 1.
  21. //
  22. // History: 1999-08-17 vtan created
  23. // --------------------------------------------------------------------------
  24. CCountedObject::CCountedObject (void) :
  25. _lReferenceCount(1),
  26. _fReleased(false)
  27. {
  28. }
  29. // --------------------------------------------------------------------------
  30. // CCountedObject::~CCountedObject
  31. //
  32. // Arguments: <none>
  33. //
  34. // Returns: <none>
  35. //
  36. // Purpose: Virtual destructor that just checks valid deletion.
  37. //
  38. // History: 1999-08-17 vtan created
  39. // --------------------------------------------------------------------------
  40. CCountedObject::~CCountedObject (void)
  41. {
  42. ASSERTMSG(_fReleased, "CCountedObject::~CCountedObject invoked without being released");
  43. }
  44. // --------------------------------------------------------------------------
  45. // CCountedObject::AddRef
  46. //
  47. // Arguments: <none>
  48. //
  49. // Returns: <none>
  50. //
  51. // Purpose: Increments the object's reference count.
  52. //
  53. // History: 1999-08-17 vtan created
  54. // --------------------------------------------------------------------------
  55. void CCountedObject::AddRef (void)
  56. {
  57. InterlockedIncrement(&_lReferenceCount);
  58. }
  59. // --------------------------------------------------------------------------
  60. // CCountedObject::Release
  61. //
  62. // Arguments: <none>
  63. //
  64. // Returns: <none>
  65. //
  66. // Purpose: Decrements the object's reference count. When the count
  67. // reaches zero the object is deleted. Do NOT use referenced
  68. // objects when using stack based objects. These must be
  69. // dynamically allocated.
  70. //
  71. // History: 1999-08-17 vtan created
  72. // --------------------------------------------------------------------------
  73. void CCountedObject::Release (void)
  74. {
  75. ASSERT( 0 != _lReferenceCount );
  76. if (InterlockedDecrement(&_lReferenceCount) == 0)
  77. {
  78. _fReleased = true;
  79. delete this;
  80. }
  81. }
  82. // --------------------------------------------------------------------------
  83. // CCountedObject::GetCount
  84. //
  85. // Arguments: <none>
  86. //
  87. // Returns: LONG
  88. //
  89. // Purpose: Returns the count of outstanding references on the object.
  90. //
  91. // History: 2000-07-17 vtan created
  92. // --------------------------------------------------------------------------
  93. LONG CCountedObject::GetCount (void) const
  94. {
  95. return(_lReferenceCount);
  96. }