Source code of Windows XP (NT5)
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.

139 lines
2.6 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. refcnt.h
  5. Abstract:
  6. Reference counting for an object.
  7. Author:
  8. Scott Holden (sholden) 12/29/1998 Borrowed from IrDA.
  9. Revision History:
  10. --*/
  11. #ifndef _REFCNT_H_
  12. #define _REFCNT_H_
  13. #ifdef NDIS40 // Only used for NDIS40 code now.
  14. #define TAG_CNT 8
  15. #define REF_SIG 0x7841eeee
  16. #if DBG
  17. typedef struct _REF_TAG
  18. {
  19. ULONG Tag;
  20. LONG Count;
  21. } REF_TAG;
  22. #endif
  23. typedef struct _REF_CNT
  24. {
  25. LONG Count;
  26. PVOID Instance;
  27. VOID (*DeleteHandler)(PVOID pvContext);
  28. #if DBG
  29. int Sig;
  30. REF_TAG Tags[TAG_CNT];
  31. KSPIN_LOCK Lock;
  32. ULONG TypeTag;
  33. #endif // DBG
  34. }
  35. REF_CNT, *PREF_CNT;
  36. //
  37. // ReferenceInit - Initialize the reference control block.
  38. //
  39. _inline VOID
  40. ReferenceInit(
  41. IN PREF_CNT pRefCnt,
  42. PVOID InstanceHandle,
  43. VOID (*DeleteHandler)(PVOID pvContext)
  44. )
  45. {
  46. pRefCnt->Count = 0;
  47. pRefCnt->Instance = InstanceHandle;
  48. pRefCnt->DeleteHandler = DeleteHandler;
  49. }
  50. //
  51. // ReferenceAdd - Add a reference.
  52. //
  53. _inline VOID
  54. ReferenceAdd(
  55. IN PREF_CNT pRefCnt
  56. )
  57. {
  58. InterlockedIncrement(&pRefCnt->Count);
  59. }
  60. //
  61. // ReferenceRemove - Del a reference. If the reference is zero, and a
  62. // delete handler has been specified, then call the
  63. // handler.
  64. //
  65. _inline VOID
  66. ReferenceRemove(
  67. IN PREF_CNT pRefCnt
  68. )
  69. {
  70. if (InterlockedDecrement(&pRefCnt->Count) <= 0 &&
  71. pRefCnt->DeleteHandler)
  72. {
  73. (pRefCnt->DeleteHandler)(pRefCnt->Instance);
  74. }
  75. }
  76. #if DBG
  77. //
  78. // For checked builds, we will do some verification with tags, etc to ensure
  79. // that the ref counting is done correctly.
  80. //
  81. VOID
  82. ReferenceInitDbg(
  83. IN PREF_CNT pRefCnt,
  84. PVOID InstanceHandle,
  85. VOID (*DeleteHandler)(PVOID pvContext),
  86. ULONG TypeTag
  87. );
  88. VOID
  89. ReferenceAddDbg(
  90. PREF_CNT pRefCnt,
  91. ULONG Tag,
  92. int cLine
  93. );
  94. VOID
  95. ReferenceRemoveDbg(
  96. PREF_CNT pRefCnt,
  97. ULONG Tag,
  98. int cLine
  99. );
  100. #define REFINIT(Rc, Inst, DelHandler, Tag) ReferenceInitDbg(Rc, Inst, DelHandler, Tag)
  101. #define REFADD(Rc, Tag) ReferenceAddDbg(Rc, Tag, __LINE__)
  102. #define REFDEL(Rc, Tag) ReferenceRemoveDbg(Rc, Tag, __LINE__)
  103. #else // DBG
  104. #define REFINIT(Rc, Inst, DelHandler, Tag) ReferenceInit(Rc, Inst, DelHandler)
  105. #define REFADD(Rc, Tag) ReferenceAdd(Rc);
  106. #define REFDEL(Rc, Tag) ReferenceRemove(Rc);
  107. #endif // !DBG
  108. #endif // NDIS40
  109. #endif // _REFCNT_H_