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.

139 lines
3.2 KiB

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