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.

124 lines
2.6 KiB

  1. //
  2. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  3. //
  4. // ***************************************************************************
  5. //
  6. // Original Author: Rajesh Rao
  7. //
  8. // $Author: rajeshr $
  9. // $Date: 6/11/98 4:43p $
  10. // $Workfile:refcount.cpp $
  11. //
  12. // $Modtime: 6/11/98 11:21a $
  13. // $Revision: 1 $
  14. // $Nokeywords: $
  15. //
  16. //
  17. // Description: Contains the imlementation for a basic reference counted object
  18. //
  19. //***************************************************************************
  20. #include "precomp.h"
  21. CRefCountedObject::CRefCountedObject() :
  22. m_dwRefCount ( 1 ),
  23. m_lpszName ( NULL ),
  24. m_CreationTime ( 0 ),
  25. m_LastAccessTime ( 0 )
  26. {
  27. // Initialize the critical section
  28. InitializeCriticalSection(&m_ReferenceCountSection);
  29. FILETIME fileTime;
  30. GetSystemTimeAsFileTime(&fileTime);
  31. LARGE_INTEGER creationTime;
  32. memcpy((LPVOID)&creationTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  33. m_CreationTime = creationTime.QuadPart;
  34. m_LastAccessTime = creationTime.QuadPart;
  35. }
  36. CRefCountedObject::CRefCountedObject(LPCWSTR lpszName) :
  37. m_dwRefCount ( 1 ),
  38. m_lpszName ( NULL ),
  39. m_CreationTime ( 0 ),
  40. m_LastAccessTime ( 0 )
  41. {
  42. // Initialize the critical section
  43. InitializeCriticalSection(&m_ReferenceCountSection);
  44. if(lpszName)
  45. {
  46. try
  47. {
  48. m_lpszName = new WCHAR[wcslen(lpszName) + 1];
  49. wcscpy(m_lpszName, lpszName);
  50. }
  51. catch ( ... )
  52. {
  53. if ( m_lpszName )
  54. {
  55. delete [] m_lpszName;
  56. m_lpszName = NULL;
  57. }
  58. DeleteCriticalSection(&m_ReferenceCountSection);
  59. throw;
  60. }
  61. }
  62. FILETIME fileTime;
  63. GetSystemTimeAsFileTime(&fileTime);
  64. LARGE_INTEGER creationTime;
  65. memcpy((LPVOID)&creationTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  66. m_CreationTime = creationTime.QuadPart;
  67. }
  68. CRefCountedObject::~CRefCountedObject()
  69. {
  70. if (m_lpszName)
  71. {
  72. delete[] m_lpszName;
  73. }
  74. // Destroy the critical section
  75. DeleteCriticalSection(&m_ReferenceCountSection);
  76. }
  77. void CRefCountedObject::AddRef()
  78. {
  79. EnterCriticalSection(&m_ReferenceCountSection);
  80. m_dwRefCount ++;
  81. LeaveCriticalSection(&m_ReferenceCountSection);
  82. }
  83. void CRefCountedObject::Release()
  84. {
  85. EnterCriticalSection(&m_ReferenceCountSection);
  86. DWORD dwCount = --m_dwRefCount;
  87. LeaveCriticalSection(&m_ReferenceCountSection);
  88. if( dwCount == 0)
  89. delete this;
  90. }
  91. LPCWSTR CRefCountedObject::GetName()
  92. {
  93. return m_lpszName;
  94. }
  95. void CRefCountedObject::SetName(LPCWSTR lpszName)
  96. {
  97. if (m_lpszName)
  98. {
  99. delete[] m_lpszName;
  100. m_lpszName=NULL;
  101. }
  102. if(lpszName)
  103. {
  104. m_lpszName = new WCHAR[wcslen(lpszName) + 1];
  105. wcscpy(m_lpszName, lpszName);
  106. }
  107. }