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.

76 lines
1.5 KiB

  1. // File: clrefcnt.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. }
  18. RefCount::~RefCount(void)
  19. {
  20. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  21. // m_ulcRef may be any value.
  22. DbgMsgRefCount("Ref: %08X c=%d (destroyed)", this, m_ulcRef);
  23. if (m_ObjectDestroyed)
  24. {
  25. m_ObjectDestroyed();
  26. m_ObjectDestroyed = NULL;
  27. }
  28. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  29. }
  30. ULONG STDMETHODCALLTYPE RefCount::AddRef(void)
  31. {
  32. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  33. ASSERT(m_ulcRef < ULONG_MAX);
  34. m_ulcRef++;
  35. DbgMsgRefCount("Ref: %08X c=%d (AddRef)", this, m_ulcRef);
  36. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  37. return m_ulcRef;
  38. }
  39. ULONG STDMETHODCALLTYPE RefCount::Release(void)
  40. {
  41. ASSERT(IS_VALID_STRUCT_PTR(this, CRefCount));
  42. if (m_ulcRef > 0)
  43. {
  44. m_ulcRef--;
  45. }
  46. ULONG ulcRef = m_ulcRef;
  47. DbgMsgRefCount("Ref: %08X c=%d (Release)", this, m_ulcRef);
  48. if (! ulcRef)
  49. {
  50. delete this;
  51. }
  52. return ulcRef;
  53. }