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.

95 lines
2.2 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name :
  4. smartptr.pp
  5. Abstract:
  6. Smart pointers and reference counting
  7. Revision History:
  8. --*/
  9. #include "precomp.hxx"
  10. #define TRC_FILE "smartptr"
  11. #include "trc.h"
  12. #if DBG
  13. void RefCount::RecordReferenceStack(LONG refs)
  14. {
  15. ULONG hash;
  16. DWORD index;
  17. BEGIN_FN("RefCount::RecordReferenceStack");
  18. //
  19. // The masking does the wrapping automatically, while keeping
  20. // the operation atomic using InterlockedIncrement
  21. //
  22. index = InterlockedIncrement((PLONG)&_dwReferenceTraceIndex) & kReferenceTraceMask;
  23. _TraceRecordList[index].ClassName = _ClassName;
  24. _TraceRecordList[index].pRefCount = this;
  25. _TraceRecordList[index].refs = refs;
  26. RtlZeroMemory(_TraceRecordList[index].Stack,
  27. sizeof(_TraceRecordList[index].Stack));
  28. RtlCaptureStackBackTrace(1,
  29. kdwStackSize,
  30. _TraceRecordList[index].Stack,
  31. &hash);
  32. }
  33. #endif // DBG
  34. RefCount::~RefCount()
  35. {
  36. BEGIN_FN("RefCount::~RefCount");
  37. ASSERT(_crefs == 0);
  38. TRC_DBG((TB, "RefCount object deleted(%p, cref=%d)", this, _crefs));
  39. }
  40. void RefCount::AddRef(void)
  41. {
  42. LONG crefs = InterlockedIncrement(&_crefs);
  43. BEGIN_FN("RefCount::AddRef");
  44. ASSERT(crefs > 0);
  45. RecordReferenceStack(crefs);
  46. TRC_DBG((TB, "AddRef object type %s (%p) to %d", _ClassName, this,
  47. crefs));
  48. }
  49. void RefCount::Release(void)
  50. {
  51. LONG crefs;
  52. #if DBG
  53. PCHAR ClassName = _ClassName;
  54. #endif
  55. BEGIN_FN("RefCount::Release");
  56. ASSERT(_crefs > 0);
  57. //
  58. // The trace below is not thread safe to access class member
  59. // So we need to make a copy of the class name
  60. //
  61. RecordReferenceStack(_crefs);
  62. crefs = InterlockedDecrement(&_crefs);
  63. if (crefs == 0)
  64. {
  65. TRC_DBG((TB, "Deleting RefCount object type %s (%p)",
  66. ClassName, this));
  67. delete this;
  68. } else {
  69. TRC_DBG((TB, "Releasing object type %s (%p) to %d",
  70. ClassName, this, crefs));
  71. }
  72. }