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.

111 lines
2.3 KiB

  1. // File: refcount.cpp
  2. //
  3. // RefCount class
  4. #include "precomp.h"
  5. #include "ConfUtil.h"
  6. /* R E F C O U N T */
  7. /*-------------------------------------------------------------------------
  8. %%Function: Init
  9. -------------------------------------------------------------------------*/
  10. void RefCount::Init(OBJECTDESTROYEDPROC ObjectDestroyed)
  11. {
  12. ASSERT((!ObjectDestroyed) ||
  13. IS_VALID_CODE_PTR(ObjectDestroyed, OBJECTDESTROYEDPROC));
  14. m_ulcRef = 1;
  15. m_ObjectDestroyed = ObjectDestroyed;
  16. DbgMsgRefCount("Ref: %08X c=%d (created)", this, m_ulcRef);
  17. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  18. #ifdef DEBUG
  19. m_fTrack = FALSE;
  20. #endif
  21. }
  22. /*-------------------------------------------------------------------------
  23. %%Function: RefCount
  24. -------------------------------------------------------------------------*/
  25. RefCount::RefCount()
  26. {
  27. Init(NULL);
  28. }
  29. /*-------------------------------------------------------------------------
  30. %%Function: RefCount
  31. -------------------------------------------------------------------------*/
  32. RefCount::RefCount(OBJECTDESTROYEDPROC ObjectDestroyed)
  33. {
  34. Init(ObjectDestroyed);
  35. }
  36. RefCount::~RefCount(void)
  37. {
  38. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  39. // m_ulcRef may be any value.
  40. DbgMsgRefCount("Ref: %08X c=%d (destroyed)", this, m_ulcRef);
  41. if (m_ObjectDestroyed)
  42. {
  43. m_ObjectDestroyed();
  44. m_ObjectDestroyed = NULL;
  45. }
  46. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  47. }
  48. ULONG STDMETHODCALLTYPE RefCount::AddRef(void)
  49. {
  50. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  51. ASSERT(m_ulcRef < ULONG_MAX);
  52. m_ulcRef++;
  53. DbgMsgRefCount("Ref: %08X c=%d (AddRef)", this, m_ulcRef);
  54. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  55. #ifdef DEBUG
  56. if (m_fTrack)
  57. {
  58. DbgMsg(iZONE_OBJECTS, "Obj: %08X c=%d (AddRef) *** Tracking", this, m_ulcRef);
  59. }
  60. #endif
  61. return m_ulcRef;
  62. }
  63. ULONG STDMETHODCALLTYPE RefCount::Release(void)
  64. {
  65. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  66. if (m_ulcRef > 0)
  67. {
  68. m_ulcRef--;
  69. }
  70. #ifdef DEBUG
  71. if (m_fTrack)
  72. {
  73. DbgMsg(iZONE_OBJECTS, "Obj: %08X c=%d (Release) *** Tracking", this, m_ulcRef);
  74. }
  75. #endif
  76. ULONG ulcRef = m_ulcRef;
  77. DbgMsgRefCount("Ref: %08X c=%d (Release)", this, m_ulcRef);
  78. if (! ulcRef)
  79. {
  80. delete this;
  81. }
  82. return ulcRef;
  83. }