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.

229 lines
3.9 KiB

  1. #include "precomp.h"
  2. CRefCount::CRefCount(DWORD dwStampID)
  3. :
  4. #ifndef SHIP_BUILD
  5. m_dwStampID(dwStampID),
  6. #endif
  7. m_cRefs(1),
  8. m_cLocks(0)
  9. {
  10. }
  11. // though it is pure virtual, we still need to have a destructor.
  12. CRefCount::~CRefCount(void)
  13. {
  14. }
  15. LONG CRefCount::AddRef(void)
  16. {
  17. ASSERT(0 < m_cRefs);
  18. ::InterlockedIncrement(&m_cRefs);
  19. return m_cRefs;
  20. }
  21. LONG CRefCount::Release(void)
  22. {
  23. ASSERT(NULL != this);
  24. ASSERT(0 < m_cRefs);
  25. if (0 == ::InterlockedDecrement(&m_cRefs))
  26. {
  27. ASSERT(0 == m_cLocks);
  28. delete this;
  29. return 0;
  30. }
  31. return m_cRefs;
  32. }
  33. void CRefCount::ReleaseNow(void)
  34. {
  35. ASSERT(NULL != this);
  36. ASSERT(0 < m_cRefs);
  37. m_cRefs = 0;
  38. delete this;
  39. }
  40. LONG CRefCount::Lock(void)
  41. {
  42. AddRef();
  43. ASSERT(0 <= m_cLocks);
  44. ::InterlockedIncrement(&m_cLocks);
  45. return m_cLocks;
  46. }
  47. LONG CRefCount::Unlock(BOOL fRelease)
  48. {
  49. ASSERT(0 < m_cLocks);
  50. ::InterlockedDecrement(&m_cLocks);
  51. LONG c = m_cLocks; // in case Release() frees the object
  52. if (fRelease)
  53. {
  54. Release();
  55. }
  56. return c;
  57. }
  58. UINT My_strlenA(LPCSTR pszSrc)
  59. {
  60. UINT cch = 0;
  61. if (NULL != pszSrc)
  62. {
  63. cch = lstrlenA(pszSrc);
  64. }
  65. return cch;
  66. }
  67. #if defined(_DEBUG)
  68. LPSTR _My_strdupA(LPCSTR pszSrc, LPSTR pszFileName, UINT nLineNumber)
  69. #else
  70. LPSTR My_strdupA(LPCSTR pszSrc)
  71. #endif
  72. {
  73. if (NULL == pszSrc)
  74. {
  75. return NULL;
  76. }
  77. UINT cch = lstrlenA(pszSrc) + 1;
  78. #if defined(_DEBUG)
  79. LPSTR pszDst = (LPSTR) DbgMemAlloc(cch, NULL, pszFileName, nLineNumber);
  80. #else
  81. LPSTR pszDst = new char[cch];
  82. #endif
  83. if (NULL != pszDst)
  84. {
  85. CopyMemory(pszDst, pszSrc, cch);
  86. }
  87. return pszDst;
  88. }
  89. UINT My_strlenW(LPCWSTR pszSrc)
  90. {
  91. UINT cch = 0;
  92. if (NULL != pszSrc)
  93. {
  94. cch = lstrlenW(pszSrc);
  95. }
  96. return cch;
  97. }
  98. #if defined(_DEBUG)
  99. LPWSTR _My_strdupW(LPCWSTR pszSrc, LPSTR pszFileName, UINT nLineNumber)
  100. #else
  101. LPWSTR My_strdupW(LPCWSTR pszSrc)
  102. #endif
  103. {
  104. if (NULL == pszSrc)
  105. {
  106. return NULL;
  107. }
  108. UINT cch = lstrlenW(pszSrc) + 1;
  109. #if defined(_DEBUG)
  110. LPWSTR pszDst = (LPWSTR) DbgMemAlloc(cch * sizeof(WCHAR), NULL, pszFileName, nLineNumber);
  111. #else
  112. LPWSTR pszDst = new WCHAR[cch];
  113. #endif
  114. if (NULL != pszDst)
  115. {
  116. CopyMemory(pszDst, pszSrc, cch * sizeof(WCHAR));
  117. }
  118. return pszDst;
  119. }
  120. //
  121. // LONCHANC: This is to provide backward compatibility to UnicodeString
  122. // in protocol structures. hopefully, we can remove this hack later.
  123. //
  124. #if defined(_DEBUG)
  125. LPWSTR _My_strdupW2(UINT cchSrc, LPCWSTR pszSrc, LPSTR pszFileName, UINT nLineNumber)
  126. #else
  127. LPWSTR My_strdupW2(UINT cchSrc, LPCWSTR pszSrc)
  128. #endif
  129. {
  130. #if defined(_DEBUG)
  131. LPWSTR pwsz = (LPWSTR) DbgMemAlloc((cchSrc+1) * sizeof(WCHAR), NULL, pszFileName, nLineNumber);
  132. #else
  133. LPWSTR pwsz = new WCHAR[cchSrc+1];
  134. #endif
  135. if (NULL != pwsz)
  136. {
  137. CopyMemory(pwsz, pszSrc, cchSrc * sizeof(WCHAR));
  138. }
  139. pwsz[cchSrc] = 0;
  140. return pwsz;
  141. }
  142. int My_strcmpW(LPCWSTR pwsz1, LPCWSTR pwsz2)
  143. {
  144. if (NULL == pwsz1 || NULL == pwsz2)
  145. {
  146. return -1;
  147. }
  148. WCHAR ch;
  149. while (0 == (ch = *pwsz1 - *pwsz2) &&
  150. NULL != *pwsz1++ &&
  151. NULL != *pwsz2++)
  152. ;
  153. return (int) ch;
  154. }
  155. #if defined(_DEBUG)
  156. LPOSTR _My_strdupO2(LPBYTE lpbSrc, UINT cOctets, LPSTR pszFileName, UINT nLineNumber)
  157. #else
  158. LPOSTR My_strdupO2(LPBYTE lpbSrc, UINT cOctets)
  159. #endif
  160. {
  161. #if defined(_DEBUG)
  162. LPOSTR poszDst = (LPOSTR) DbgMemAlloc(sizeof(OSTR) + cOctets + 1, NULL, pszFileName, nLineNumber);
  163. #else
  164. LPOSTR poszDst = (LPOSTR) new char[sizeof(OSTR) + cOctets + 1];
  165. #endif
  166. if (NULL != poszDst)
  167. {
  168. poszDst->length = cOctets;
  169. poszDst->value = (LPBYTE) (poszDst + 1);
  170. ::CopyMemory(poszDst->value, lpbSrc, cOctets);
  171. }
  172. return poszDst;
  173. }
  174. INT My_strcmpO(LPOSTR posz1, LPOSTR posz2)
  175. {
  176. if (NULL == posz1 || NULL == posz2)
  177. {
  178. return -1;
  179. }
  180. if (posz1->length != posz2->length)
  181. {
  182. return -1;
  183. }
  184. UINT cnt = posz1->length;
  185. LPBYTE lpb1 = posz1->value, lpb2 = posz2->value;
  186. BYTE b = 0;
  187. while (cnt--)
  188. {
  189. if (0 != (b = *lpb1++ - *lpb2++))
  190. {
  191. break;
  192. }
  193. }
  194. return (INT) b;
  195. }