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.

216 lines
7.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. // File: otrack.hxx
  7. //
  8. // Contents: This file defines facilities for a standard implementation
  9. // of reference-counted objects, incorporating mechanisms for
  10. // tracking the usage history of the objects.
  11. //
  12. // Classes: CRefcountedObject
  13. //
  14. // History: 6-Apr-92 MikeSe Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #ifndef __OTRACK_HXX__
  18. #define __OTRACK_HXX__
  19. #include <windows.h>
  20. //+-------------------------------------------------------------------------
  21. //
  22. // Class: CRefcountedObject (rco)
  23. //
  24. // Purpose: Provides basis for tracking (OLE) objects (aka interface handles)
  25. //
  26. // History: 6-Apr-92 MikeSe Created
  27. //
  28. // Notes: Access to this class is only indirect, through the macros
  29. // defined later.
  30. //
  31. //--------------------------------------------------------------------------
  32. class CRefcountedObject
  33. {
  34. protected:
  35. CRefcountedObject ()
  36. :_cReferences(1)
  37. {
  38. # if DBG == 1
  39. _CRefcountedObject();
  40. # endif
  41. }
  42. ULONG TrackAddRef ( void );
  43. ULONG TrackRelease ( void );
  44. void TrackClassName ( char * pszName );
  45. # if DBG == 1
  46. ~CRefcountedObject()
  47. {
  48. DestroyRefcounted();
  49. }
  50. # endif
  51. private:
  52. void _CRefcountedObject ( void );
  53. void DestroyRefcounted ( void );
  54. # if DBG == 1
  55. BOOL IsClassTracking( char * pszName );
  56. static BOOL _TrackAll;
  57. public:
  58. // dumps information on all outstanding objects
  59. static void DumpTrackingInfo ( int fDeleteNode = 0 );
  60. static void TrackClass(BOOL, char * pszName );
  61. # endif // DBG == 1
  62. protected:
  63. unsigned long _cReferences;
  64. };
  65. //+-------------------------------------------------------------------------
  66. //
  67. // The following macros encapsulate use of the above
  68. //
  69. // INHERIT_TRACKING:
  70. //
  71. // For any class which implements a Cairo interface, add this macro
  72. // in the class declaration, eg:
  73. //
  74. // class CMyFoo: INHERIT_TRACKING, IFoo
  75. //
  76. // Do not repeat this in any subclass. If both INHERIT_UNWIND and
  77. // INHERIT_TRACKING are used, the former should appear first.
  78. # define INHERIT_TRACKING protected CRefcountedObject
  79. // The following declarations are for non-retail builds
  80. # if DBG == 1
  81. //
  82. // DECLARE_STD_REFCOUNTING:
  83. //
  84. // To make use of standard refcounting code, place the above
  85. // in the class declaration, in the public method section. This
  86. // macro defines the AddRef and Release methods for the class.
  87. //
  88. # define DECLARE_STD_REFCOUNTING \
  89. STDMETHOD_(ULONG, AddRef) () \
  90. { \
  91. return TrackAddRef(); \
  92. }; \
  93. STDMETHOD_(ULONG, Release) () \
  94. { \
  95. ULONG ul = TrackRelease(); \
  96. if ( ul==0 ) delete this; \
  97. return ul; \
  98. };
  99. //
  100. // ENLIST_TRACKING(class name)
  101. //
  102. // Place an invocation of this in each constructor, at any appropriate
  103. // point (generally immediately before returning from the constructor).
  104. //
  105. // ENLIST_TRACKING(CMyFoo)
  106. //
  107. # define ENLIST_TRACKING(cls) TrackClassName( #cls )
  108. //
  109. // NB: In a subclass of a class which has INHERIT_TRACKING, do not
  110. // use INHERIT_TRACKING again. However, do use ENLIST_TRACKING in
  111. // in the constructor of the derived class, and use TRACK_ADDREF
  112. // and TRACK_RELEASE if the subclass overrides the AddRef and Release
  113. // methods.
  114. //
  115. // TRACK_CLASS(fTrack, pszClassName)
  116. //
  117. // Use this to add or remove a class in the list of tracked classes.
  118. // You can turn tracking of all classes on or off by using a NULL
  119. // pszClassName. If fTrack is TRUE, the class will be tracked, if FALSE,
  120. // the class will no longer be tracked. The default configuration is
  121. // that all classes are tracked.
  122. //
  123. // NOTE: this affects only objects created after this macro is executed.
  124. //
  125. # define TRACK_CLASS(fTrack, cls) \
  126. CRefcountedObject::TrackClass( fTrack, cls )
  127. //
  128. // DUMP_TRACKING_INFO()
  129. //
  130. // Place this anywhere it would be useful to dump out the object
  131. // tracking database. By default, this is always issued at program
  132. // termination.
  133. //
  134. # define DUMP_TRACKING_INFO() DUMP_TRACKING_INFO_KEEP()
  135. # define DUMP_TRACKING_INFO_KEEP() CRefcountedObject::DumpTrackingInfo(0)
  136. # define DUMP_TRACKING_INFO_DELETE() CRefcountedObject::DumpTrackingInfo(1)
  137. // Output from this is controlled by setting the following mask values
  138. // in OtInfoLevel
  139. # define DEB_OT_OBJECTS 0x00000001L
  140. // display object addresses, reference count and name (Default)
  141. # define DEB_OT_CALLERS 0x80000000L
  142. // display call history
  143. // In addition, set the following values to cause debug output during
  144. // operation:
  145. # define DEB_OT_ERRORS 0x00000002L
  146. // report errors during tracking operations (Default)
  147. # define DEB_OT_ACTIONS 0x40000000L
  148. // report each create, addref and release
  149. # define DEB_OT_DELETE 0x20000000L
  150. // display call history at object delete
  151. # else // DBG == 0
  152. # define DECLARE_STD_REFCOUNTING \
  153. STDMETHOD_(ULONG, AddRef) () \
  154. { \
  155. return InterlockedIncrement((long*)&_cReferences); \
  156. }; \
  157. STDMETHOD_(ULONG, Release) () \
  158. { \
  159. ULONG ul = (ULONG)InterlockedDecrement((long*)&_cReferences); \
  160. if ( ul == 0 ) \
  161. { \
  162. delete this; \
  163. return 0; \
  164. } \
  165. else \
  166. return ul; \
  167. };
  168. # define ENLIST_TRACKING(cls)
  169. # define DUMP_TRACKING_INFO()
  170. # define TRACK_CLASS(fTrack, cls)
  171. # endif // DBG == 0
  172. #endif // of ifndef __OTRACK_HXX__