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.

92 lines
1.8 KiB

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