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.

121 lines
2.4 KiB

  1. /*
  2. * refcount.cpp - RefCount class implementation.
  3. *
  4. * Taken from URL code - essentially identical to DavidDi's original code
  5. *
  6. * Created: ChrisPi 9-11-95
  7. *
  8. */
  9. /* Headers
  10. **********/
  11. #include "precomp.h"
  12. #include "clrefcnt.hpp"
  13. /****************************** Public Functions *****************************/
  14. #ifdef DEBUG
  15. BOOL IsValidPCRefCount(PCRefCount pcrefcnt)
  16. {
  17. // m_ulcRef may be any value.
  18. return(IS_VALID_READ_PTR(pcrefcnt, CRefCount) &&
  19. (! pcrefcnt->m_ObjectDestroyed ||
  20. IS_VALID_CODE_PTR(pcrefcnt->m_ObjectDestroyed, OBJECTDESTROYEDPROC)));
  21. }
  22. #endif
  23. /********************************** Methods **********************************/
  24. RefCount::RefCount(OBJECTDESTROYEDPROC ObjectDestroyed)
  25. {
  26. DebugEntry(RefCount::RefCount);
  27. /* Don't validate this until after initialization. */
  28. ASSERT(! ObjectDestroyed ||
  29. IS_VALID_CODE_PTR(ObjectDestroyed, OBJECTDESTROYEDPROC));
  30. m_ulcRef = 1;
  31. m_ObjectDestroyed = ObjectDestroyed;
  32. DBGAPI_REF("Ref: %08X c=%d (created)", this, m_ulcRef);
  33. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  34. DebugExitVOID(RefCount::RefCount);
  35. return;
  36. }
  37. RefCount::~RefCount(void)
  38. {
  39. DebugEntry(RefCount::~RefCount);
  40. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  41. // m_ulcRef may be any value.
  42. DBGAPI_REF("Ref: %08X c=%d (destroyed)", this, m_ulcRef);
  43. if (m_ObjectDestroyed)
  44. {
  45. m_ObjectDestroyed();
  46. m_ObjectDestroyed = NULL;
  47. }
  48. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  49. DebugExitVOID(RefCount::~RefCount);
  50. return;
  51. }
  52. ULONG STDMETHODCALLTYPE RefCount::AddRef(void)
  53. {
  54. DebugEntry(RefCount::AddRef);
  55. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  56. ASSERT(m_ulcRef < ULONG_MAX);
  57. m_ulcRef++;
  58. DBGAPI_REF("Ref: %08X c=%d (AddRef)", this, m_ulcRef);
  59. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  60. DebugExitULONG(RefCount::AddRef, m_ulcRef);
  61. return(m_ulcRef);
  62. }
  63. ULONG STDMETHODCALLTYPE RefCount::Release(void)
  64. {
  65. ULONG ulcRef;
  66. DebugEntry(RefCount::Release);
  67. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  68. if (EVAL(m_ulcRef > 0))
  69. m_ulcRef--;
  70. ulcRef = m_ulcRef;
  71. DBGAPI_REF("Ref: %08X c=%d (Release)", this, m_ulcRef);
  72. if (! ulcRef)
  73. delete this;
  74. DebugExitULONG(RefCount::Release, ulcRef);
  75. return(ulcRef);
  76. }