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.

134 lines
3.2 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. #ifndef __WBEM_STACK_COM__H_
  8. #define __WBEM_STACK_COM__H_
  9. template<class T>
  10. struct Ptr
  11. {
  12. T* m_p;
  13. Ptr(T* p) : m_p(p){}
  14. Ptr() : m_p(NULL){}
  15. Ptr(const Ptr<T>& Other) :m_p(Other.m_p){}
  16. operator T*() {return m_p;}
  17. operator const T*() const {return m_p;}
  18. T* operator->() {return m_p;}
  19. const T* operator->() const {return m_p;}
  20. bool operator<(const Ptr<T>& Other) const {return m_p < Other.m_p;}
  21. };
  22. typedef DWORD CAllocationId;
  23. struct POLARITY CStackRecord
  24. {
  25. DWORD m_dwNumItems;
  26. void** m_apReturns;
  27. BOOL m_bDelete;
  28. static void* mstatic_apReturns[1000];
  29. public:
  30. CStackRecord() : m_dwNumItems(0), m_apReturns(NULL), m_bDelete(FALSE){}
  31. CStackRecord(CStackRecord& Parent, DWORD dwNumItems) :
  32. m_dwNumItems(dwNumItems), m_apReturns(Parent.m_apReturns),
  33. m_bDelete(FALSE)
  34. {}
  35. DWORD GetNumItems() const {return m_dwNumItems;}
  36. void* GetItem(int nIndex) const {return m_apReturns[nIndex];}
  37. void** GetItems() {return m_apReturns;}
  38. void** const GetItems() const {return m_apReturns;}
  39. ~CStackRecord();
  40. void Create(int nIgnore, BOOL bStatic);
  41. void MakeCopy(CStackRecord& Other);
  42. int Compare(const CStackRecord& Other) const;
  43. BOOL operator==(const CStackRecord& Other) const
  44. {return Compare(Other) == 0;}
  45. static DWORD GetStackLen();
  46. static void ReadStack(int nIgnore, void** apBuffer);
  47. void Dump(FILE* f) const;
  48. BOOL Read(FILE* f, BOOL bStatid);
  49. DWORD GetInternal() {return sizeof(void*) * m_dwNumItems;}
  50. public:
  51. class CLess
  52. {
  53. public:
  54. bool operator()(const CStackRecord* p1, const CStackRecord* p2) const
  55. {
  56. return p1->Compare(*p2) < 0;
  57. }
  58. };
  59. };
  60. struct POLARITY CAllocRecord
  61. {
  62. CStackRecord m_Stack;
  63. DWORD m_dwTotalAlloc;
  64. DWORD m_dwNumTimes;
  65. CFlexArray m_apBuffers;
  66. CAllocRecord() : m_dwTotalAlloc(0), m_dwNumTimes(0){}
  67. CAllocRecord(CStackRecord& Stack) : m_dwTotalAlloc(0), m_dwNumTimes(0)
  68. {
  69. m_Stack.MakeCopy(Stack);
  70. }
  71. void AddAlloc(void* p, DWORD dwAlloc)
  72. {m_dwTotalAlloc += dwAlloc; m_dwNumTimes++; m_apBuffers.Add(p);}
  73. void RemoveAlloc(void* p, DWORD dwAlloc)
  74. {m_dwTotalAlloc -= dwAlloc; m_dwNumTimes--; RemoveBuffer(p);}
  75. void ReduceAlloc(DWORD dwAlloc) {m_dwTotalAlloc -= dwAlloc;}
  76. void Dump(FILE* f) const;
  77. DWORD GetInternal() {return sizeof(CAllocRecord) + m_Stack.GetInternal();}
  78. BOOL IsEmpty() const {return (m_dwTotalAlloc == 0);}
  79. void RemoveBuffer(void* p)
  80. {
  81. for(int i = 0; i < m_apBuffers.Size(); i++)
  82. if(m_apBuffers[i] == p) {m_apBuffers.RemoveAt(i); return;}
  83. }
  84. void Subtract(CAllocRecord& Other);
  85. };
  86. typedef Ptr<CAllocRecord> PAllocRecord;
  87. typedef Ptr<CStackRecord> PStackRecord;
  88. class POLARITY CTls
  89. {
  90. protected:
  91. DWORD m_dwIndex;
  92. public:
  93. CTls() {m_dwIndex = TlsAlloc();}
  94. ~CTls() {TlsFree(m_dwIndex);}
  95. operator void*() {return (m_dwIndex)?TlsGetValue(m_dwIndex):NULL;}
  96. void operator=(void* p) {if(m_dwIndex)TlsSetValue(m_dwIndex, p);}
  97. };
  98. struct POLARITY CStackContinuation
  99. {
  100. CStackRecord* m_pPrevStack;
  101. void* m_pThisStackEnd;
  102. static CTls mtls_CurrentCont;
  103. public:
  104. static CStackContinuation* Set(CStackContinuation* pNew);
  105. static CStackContinuation* Get();
  106. };
  107. #endif