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.

192 lines
4.0 KiB

  1. /*++
  2. Copyright (C) 2000-2001 Microsoft Corporation
  3. --*/
  4. #ifndef __A51_TOOLS__H_
  5. #define __A51_TOOLS__H_
  6. #include <sync.h>
  7. #include <newnew.h>
  8. #include <xmemory>
  9. #ifdef DBG
  10. #define _A51_INTERNAL_ASSERT
  11. #endif
  12. typedef LONGLONG TFileOffset;
  13. #define MAX_HASH_LEN 32
  14. void* TempAlloc(DWORD dwLen);
  15. void TempFree(void* p, DWORD dwLen = 0);
  16. void* TempAlloc(CTempMemoryManager& Manager, DWORD dwLen);
  17. void TempFree(CTempMemoryManager& Manager, void* p, DWORD dwLen);
  18. HRESULT A51TranslateErrorCode(long lRes);
  19. #define TEMPFREE_ME
  20. class CTempFreeMe
  21. {
  22. protected:
  23. void* m_p;
  24. DWORD m_dwLen;
  25. public:
  26. CTempFreeMe(void* p, DWORD dwLen = 0) : m_p(p), m_dwLen(dwLen){}
  27. ~CTempFreeMe() {TempFree(m_p, m_dwLen);}
  28. };
  29. HANDLE A51GetNewEvent();
  30. void A51ReturnEvent(HANDLE hEvent);
  31. class CReturnMe
  32. {
  33. protected:
  34. HANDLE m_h;
  35. public:
  36. CReturnMe(HANDLE h) : m_h(h){}
  37. ~CReturnMe() {A51ReturnEvent(m_h);}
  38. };
  39. inline void wbem_wcsupr(WCHAR* pwcTo, const WCHAR* pwcFrom)
  40. {
  41. while(*pwcFrom)
  42. {
  43. if(*pwcFrom >= 'a' && *pwcFrom <= 'z')
  44. *pwcTo = *pwcFrom + ('A'-'a');
  45. else
  46. *pwcTo = towupper(*pwcFrom);
  47. pwcTo++;
  48. pwcFrom++;
  49. }
  50. *pwcTo = 0;
  51. }
  52. class CFileName
  53. {
  54. private:
  55. wchar_t *m_wszFilename;
  56. public:
  57. DWORD Length() { return MAX_PATH + 1; }
  58. CFileName() { m_wszFilename = (wchar_t*)TempAlloc(sizeof(wchar_t) * Length()); }
  59. ~CFileName() { TempFree(m_wszFilename, sizeof(wchar_t) * Length()); }
  60. operator wchar_t *() { return m_wszFilename; }
  61. };
  62. long EnsureDirectory(LPCWSTR wszPath, LPSECURITY_ATTRIBUTES pSA = NULL);
  63. long EnsureDirectoryForFile(LPCWSTR wszPath, LPSECURITY_ATTRIBUTES pSA = NULL);
  64. bool A51Hash(LPCWSTR wszName, LPWSTR wszHash);
  65. long A51DeleteFile(LPCWSTR wszFullPath);
  66. long A51WriteFile(LPCWSTR wszFullPath, DWORD dwLen, BYTE* pBuffer);
  67. long A51RemoveDirectory(LPCWSTR wszFullPath);
  68. long A51WriteToFileAsync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  69. DWORD dwBufferLen, OVERLAPPED* pov);
  70. long A51WriteToFileSync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  71. DWORD dwBufferLen);
  72. long A51ReadFromFileAsync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  73. DWORD dwBufferLen, OVERLAPPED* pov);
  74. long A51ReadFromFileSync(HANDLE hFile, long lStartingOffset, BYTE* pBuffer,
  75. DWORD dwBufferLen);
  76. extern __int64 g_nCurrentTime;
  77. #undef _ASSERT
  78. #ifdef _A51_INTERNAL_ASSERT
  79. #define _ASSERT(X, MSG) {if(!(X)) {A51TraceFlush(); DebugBreak();}}
  80. #else
  81. #define _ASSERT(X, MSG)
  82. #endif
  83. extern FILE* g_fLog;
  84. #ifdef _A51_INTERNAL_DEBUG
  85. #define A51TRACE(X) A51Trace X
  86. #else
  87. #define A51TRACE(X)
  88. #endif
  89. void A51Trace(LPCSTR szFormat, ...);
  90. void A51TraceFlush();
  91. template<class T>
  92. class CTempAllocator
  93. {
  94. public:
  95. typedef size_t size_type;
  96. typedef ptrdiff_t difference_type;
  97. typedef T *pointer;
  98. typedef const T *const_pointer;
  99. typedef T& reference;
  100. typedef const T& const_reference;
  101. typedef T value_type;
  102. char* _Charalloc(size_t n)
  103. {
  104. return (char*)TempAlloc(n);
  105. }
  106. void deallocate(void* p, size_t n)
  107. {
  108. TempFree(p, 0);
  109. }
  110. void construct(pointer p, const T& val)
  111. {
  112. new ((void*)p) T(val);
  113. }
  114. void destroy(pointer p)
  115. {
  116. p->T::~T();
  117. }
  118. };
  119. template<class T>
  120. class CPrivateTempAllocator
  121. {
  122. public:
  123. typedef size_t size_type;
  124. typedef ptrdiff_t difference_type;
  125. typedef T *pointer;
  126. typedef const T *const_pointer;
  127. typedef T& reference;
  128. typedef const T& const_reference;
  129. typedef T value_type;
  130. CPrivateTempAllocator(CTempMemoryManager* pManager)
  131. : m_pManager(pManager)
  132. {}
  133. char* _Charalloc(size_t n)
  134. {
  135. return (char*)m_pManager->Allocate(n);
  136. }
  137. void deallocate(void* p, size_t n)
  138. {
  139. m_pManager->Free(p, 0);
  140. }
  141. void construct(pointer p, const T& val)
  142. {
  143. new ((void*)p) T(val);
  144. }
  145. void destroy(pointer p)
  146. {
  147. p->T::~T();
  148. }
  149. protected:
  150. CTempMemoryManager* m_pManager;
  151. };
  152. #endif