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.

162 lines
3.5 KiB

  1. /*++
  2. Microsoft Windows NT RPC Name Service
  3. Copyright (C) Microsoft Corporation, 1995 - 1999
  4. Module Name:
  5. util.hxx
  6. Abstract:
  7. This file defines CRefCounted and TCResourceCounted.
  8. The former is used as the base for reference counted objects.
  9. The latter is used as a debugging tool for detecting leaks
  10. and accounting for resource usage in general.
  11. Author:
  12. Satish Thatte (SatishT) 09/01/95 Created all the code below except where
  13. otherwise indicated.
  14. --*/
  15. #ifndef _UTIL_HXX_
  16. #define _UTIL_HXX_
  17. /*++
  18. Template Class Definition:
  19. TCResourceCounted
  20. Abstract:
  21. Base template class for resource counted objects.
  22. This is primarily a debugging tool to detect
  23. resource leakage during my own stress tests.
  24. The template form allows separate resource counting
  25. for each derived class and also a neat summary.
  26. --*/
  27. template <char * name>
  28. class TCResourceCounted {
  29. static ULONG ulCurrentResourceCount; // class-wide counts
  30. static ULONG ulTotalResourceCount;
  31. public:
  32. TCResourceCounted() {
  33. ulCurrentResourceCount++;
  34. ulTotalResourceCount++;
  35. }
  36. ~TCResourceCounted() { ulCurrentResourceCount--; }
  37. static void printSummary() {
  38. fprintf(stderr,"Total %s objects = %d\nCurrent %s objects = %d\n",
  39. name,ulTotalResourceCount,
  40. name,ulCurrentResourceCount);
  41. }
  42. };
  43. // static member initialization
  44. #if !(_MSC_VER >= 1100 && defined(__BOOL_DEFINED)) && !defined(_AMD64_) && !defined(IA64)
  45. template <int index>
  46. ULONG TCResourceCounted<index>::ulCurrentResourceCount = 0;
  47. template <int index>
  48. ULONG TCResourceCounted<index>::ulTotalResourceCount = 0;
  49. #endif
  50. /*++
  51. Class Definition:
  52. CRefCounted
  53. Abstract:
  54. Base class for reference counted objects.
  55. Why make hold and release virtual? If necessary, why not just redefine them?
  56. That would be a viable approach if it is never necessary to maintain
  57. references to CRefCounted objects as just CRefCounted objects,
  58. i.e., without knowing which derived class they belonged to.
  59. This is not a good assumption. What if a CRefCounted object needs
  60. to be released on a timeout basis and is held along with other
  61. similar objects without knowledge of its complete type?
  62. The class has a virtual destructor for the same reason -- all
  63. associated resources will be released during self-destruct even
  64. if the only known class for the object is CRefCounted.
  65. We use the Win32 locked increment/decrement APIs for MT safety.
  66. --*/
  67. class CRefCounted {
  68. protected:
  69. long ulRefCount;
  70. public:
  71. virtual void hold() {
  72. InterlockedIncrement(&ulRefCount);
  73. }
  74. virtual void release() {
  75. ASSERT(ulRefCount, "Decrementing nonpositive reference count\n");
  76. if (!InterlockedDecrement(&ulRefCount)) {
  77. DBGOUT(REFCOUNT, "Deleting refcounted object ****\n\n");
  78. delete this;
  79. }
  80. }
  81. /* The function "willBeDeletedIfReleased" is not thread safe --
  82. use with caution in cooperative situations only */
  83. int willBeDeletedIfReleased()
  84. {
  85. return ulRefCount == 1;
  86. }
  87. /* start with count of 1 to avoid mandatory AddRef at creation */
  88. CRefCounted() { ulRefCount = 1; }
  89. virtual ~CRefCounted() {};
  90. };
  91. template <class TYPE>
  92. inline void
  93. swapThem(TYPE& v1, TYPE& v2) {
  94. TYPE temp;
  95. temp = v1;
  96. v1 = v2;
  97. v2 = temp;
  98. }
  99. unsigned
  100. RandomBit(
  101. unsigned long *pState
  102. );
  103. void
  104. GetDomainFlatName(CONST WCHAR *domainNameDns, WCHAR **szDomainNameFlat);
  105. #endif _UTIL_HXX_