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.

143 lines
2.9 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. typedef LONGLONG TFileOffset;
  10. #define MAX_HASH_LEN 32
  11. void* TempAlloc(DWORD dwLen);
  12. void TempFree(void* p, DWORD dwLen = 0);
  13. void* TempAlloc(CTempMemoryManager& Manager, DWORD dwLen);
  14. void TempFree(CTempMemoryManager& Manager, void* p, DWORD dwLen = 0);
  15. HRESULT A51TranslateErrorCode(long lRes);
  16. #define TEMPFREE_ME
  17. class CTempFreeMe
  18. {
  19. protected:
  20. void* m_p;
  21. DWORD m_dwLen;
  22. public:
  23. CTempFreeMe(void* p, DWORD dwLen = 0) : m_p(p), m_dwLen(dwLen){}
  24. ~CTempFreeMe() {TempFree(m_p, m_dwLen);}
  25. };
  26. inline void wbem_wcsupr(WCHAR* pwcTo, const WCHAR* pwcFrom)
  27. {
  28. while(*pwcFrom)
  29. {
  30. if(*pwcFrom >= 'a' && *pwcFrom <= 'z')
  31. *pwcTo = *pwcFrom + ('A'-'a');
  32. else if(*pwcFrom < 128)
  33. *pwcTo = *pwcFrom;
  34. else
  35. *pwcTo = wbem_towupper(*pwcFrom);
  36. pwcTo++;
  37. pwcFrom++;
  38. }
  39. *pwcTo = 0;
  40. }
  41. class CFileName
  42. {
  43. private:
  44. wchar_t *m_wszFilename;
  45. public:
  46. DWORD Length() { return MAX_PATH + 1; }
  47. CFileName() { m_wszFilename = (wchar_t*)TempAlloc(sizeof(wchar_t) * Length()); }
  48. ~CFileName() { TempFree(m_wszFilename, sizeof(wchar_t) * Length()); }
  49. operator wchar_t *() { return m_wszFilename; }
  50. };
  51. long EnsureDirectory(LPCWSTR wszPath, LPSECURITY_ATTRIBUTES pSA = NULL);
  52. bool A51Hash(LPCWSTR wszName, LPWSTR wszHash);
  53. extern __int64 g_nCurrentTime;
  54. template<class T>
  55. class CTempAllocator
  56. {
  57. public:
  58. typedef size_t size_type;
  59. typedef ptrdiff_t difference_type;
  60. typedef T *pointer;
  61. typedef const T *const_pointer;
  62. typedef T& reference;
  63. typedef const T& const_reference;
  64. typedef T value_type;
  65. char* _Charalloc(size_t n)
  66. {
  67. return (char*)TempAlloc(n);
  68. }
  69. void deallocate(void* p, size_t n)
  70. {
  71. TempFree(p, 0);
  72. }
  73. void construct(pointer p, const T& val)
  74. {
  75. new ((void*)p) T(val);
  76. }
  77. void destroy(pointer p)
  78. {
  79. p->T::~T();
  80. }
  81. };
  82. template<class T>
  83. class CPrivateTempAllocator
  84. {
  85. public:
  86. typedef size_t size_type;
  87. typedef ptrdiff_t difference_type;
  88. typedef T *pointer;
  89. typedef const T *const_pointer;
  90. typedef T& reference;
  91. typedef const T& const_reference;
  92. typedef T value_type;
  93. CPrivateTempAllocator(CTempMemoryManager* pManager)
  94. : m_pManager(pManager)
  95. {}
  96. char* _Charalloc(size_t n)
  97. {
  98. return (char*)m_pManager->Allocate(n);
  99. }
  100. void deallocate(void* p, size_t n)
  101. {
  102. m_pManager->Free(p, 0);
  103. }
  104. void construct(pointer p, const T& val)
  105. {
  106. new ((void*)p) T(val);
  107. }
  108. void destroy(pointer p)
  109. {
  110. p->T::~T();
  111. }
  112. protected:
  113. CTempMemoryManager* m_pManager;
  114. };
  115. #endif