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.

245 lines
9.4 KiB

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