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.

117 lines
3.2 KiB

  1. #pragma once
  2. #include "nt.h"
  3. #include "ntrtl.h"
  4. #include "nturtl.h"
  5. #include "windows.h"
  6. #include "bcl_common.h"
  7. #include "bcl_w32unicodeinlinestringbuffer.h"
  8. #define DBGPRINT_LVL_ERROR (0x00000001)
  9. #define DBGPRINT_LVL_SPEW (0x00000002)
  10. #define SXS_STORE_SERVICE_NAME (L"SxsStoreManagementService")
  11. void DebugPrint(ULONG ulLevel, PCSTR Format, ...);
  12. void DebugPrintVa(ULONG ulLevel, PCSTR Format, va_list va);
  13. typedef BCL::CWin32BaseUnicodeInlineStringBuffer<64> CStringBuffer;
  14. typedef BCL::CWin32BaseUnicodeInlineStringBufferTraits<64> CStringBufferTraits;
  15. typedef BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> CStringPair;
  16. template<typename T> inline
  17. CStringPair StringPair(T& source) {
  18. return CStringBufferTraits::GetStringPair(&source);
  19. }
  20. BOOL FormatGuid(LPCGUID g, CStringBuffer &Target);
  21. #ifndef NUMBER_OF
  22. #define NUMBER_OF(x) (sizeof(x)/sizeof(*x))
  23. #endif
  24. #if DBG
  25. #define DbgPrint(x) DebugPrint x
  26. #else
  27. #define DbgPrint(x)
  28. #endif
  29. #ifndef ALIGN_TO_SIZE
  30. #define ALIGN_TO_SIZE(P, Sz) (((ULONG_PTR)(P)) & ~((ULONG_PTR)(Sz) - 1))
  31. #endif
  32. template <SIZE_T cbInternalSize = 64, SIZE_T cbGrowthSize = 4096>
  33. class CMiniInlineHeap
  34. {
  35. BYTE m_bInternalHeap[cbInternalSize];
  36. PBYTE m_pbCurrentBlob;
  37. PBYTE m_pbBlobEnding;
  38. SIZE_T m_cbCurrentBlob;
  39. PBYTE m_pbNextAvailable;
  40. public:
  41. CMiniInlineHeap()
  42. : m_pbCurrentBlob(m_bInternalHeap),
  43. m_cbCurrentBlob(cbInternalSize),
  44. m_pbNextAvailable(m_bInternalHeap),
  45. m_pbBlobEnding(m_bInternalHeap + cbInternalSize)
  46. {
  47. }
  48. ~CMiniInlineHeap()
  49. {
  50. if (m_pbCurrentBlob && (m_pbCurrentBlob != m_bInternalHeap)) {
  51. HeapFree(GetProcessHeap(), 0, m_pbCurrentBlob);
  52. m_pbCurrentBlob = m_bInternalHeap;
  53. m_cbCurrentBlob = cbInternalSize;
  54. }
  55. }
  56. template <typename T>
  57. BOOL Allocate(SIZE_T Count, T*& rptAllocated) {
  58. return this->AllocateBytes(sizeof(T) * Count, sizeof(T), rptAllocated);
  59. }
  60. BOOL AllocateBytes(SIZE_T cbRequired, SIZE_T cbAlignment, PVOID &ppvAllocated) {
  61. //
  62. // Align the next-available pointer up to store cbAlignment-sized stuff
  63. //
  64. PBYTE pbAlignedPointer = ALIGN_TO_SIZE(m_pbNextAvailable, cbAlignment);
  65. ppvAllocated = NULL;
  66. //
  67. // If there's no space left at the end of the heap, expand ourselves and
  68. // try again.
  69. //
  70. if ((pbAlignedPointer + cbRequired) >= m_pbBlobEnding) {
  71. if (!ExpandHeap(m_cbCurrentBlob + cbRequired + cbAlignment)) {
  72. return FALSE;
  73. }
  74. else {
  75. pbAlignedPointer = ALIGN_TO_SIZE(m_pbNextAvailable, cbAlignment);
  76. }
  77. }
  78. //
  79. // Advance our pointers, set the outbound thing, return true.
  80. //
  81. ppvAllocated = pbAlignedPointer;
  82. m_pbNextAvailable = (pbAlignedPointer + cbRequired);
  83. ASSERT(m_pbNextAvailable <= m_pbBlobEnding);
  84. return TRUE;
  85. }
  86. PBYTE GetCurrentBase() { return m_pbCurrentBlob; }
  87. void Reset()
  88. {
  89. m_pbNextAvailable = m_pbCurrentBlob;
  90. }
  91. };