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.

204 lines
4.6 KiB

  1. /*++
  2. Copyright (C) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. NEWNEW.H
  5. Abstract:
  6. CReuseMemoryManager
  7. History:
  8. --*/
  9. #ifndef __WBEM_NEW_NEW__H_
  10. #define __WBEM_NEW_NEW__H_
  11. #include "esscpol.h"
  12. #include <stack>
  13. class ESSCLI_POLARITY CReuseMemoryManager
  14. {
  15. protected:
  16. CCritSec m_cs;
  17. CFlexQueue m_Available;
  18. int m_nSize;
  19. int m_nMaxQueued;
  20. public:
  21. CReuseMemoryManager(size_t nSize, size_t nMaxQueued = 0 )
  22. : m_nSize(nSize)
  23. {
  24. m_nMaxQueued = nMaxQueued == 0 ? 256 : nMaxQueued;
  25. }
  26. ~CReuseMemoryManager()
  27. {
  28. Clear();
  29. }
  30. void* Allocate()
  31. {
  32. CInCritSec ics(&m_cs);
  33. if(m_Available.GetQueueSize() == 0)
  34. {
  35. return new BYTE[m_nSize];
  36. }
  37. else
  38. {
  39. void* p = m_Available.Unqueue();
  40. return p;
  41. }
  42. }
  43. void Free(void* p)
  44. {
  45. CInCritSec ics(&m_cs);
  46. if ( m_Available.GetQueueSize() < m_nMaxQueued )
  47. {
  48. m_Available.Enqueue(p);
  49. }
  50. else
  51. {
  52. delete [] (BYTE*)p;
  53. }
  54. }
  55. void Clear()
  56. {
  57. CInCritSec ics(&m_cs);
  58. while(m_Available.GetQueueSize())
  59. {
  60. delete [] (BYTE*)m_Available.Unqueue();
  61. }
  62. }
  63. };
  64. #define DWORD_ALIGNED(x) (((x) + 3) & ~3)
  65. #define QWORD_ALIGNED(x) (((x) + 7) & ~7)
  66. #ifdef _WIN64
  67. #define DEF_ALIGNED QWORD_ALIGNED
  68. #else
  69. #define DEF_ALIGNED DWORD_ALIGNED
  70. #endif
  71. class ESSCLI_POLARITY CTempMemoryManager
  72. {
  73. protected:
  74. CCritSec m_cs;
  75. class CAllocation
  76. {
  77. private:
  78. size_t m_dwAllocationSize;
  79. size_t m_dwUsed;
  80. size_t m_dwFirstFree;
  81. static inline size_t GetHeaderSize() {return sizeof(CAllocation);}
  82. inline byte* GetStart() {return ((byte*)this) + GetHeaderSize();}
  83. inline byte* GetEnd() {return ((byte*)this) + m_dwAllocationSize;}
  84. public:
  85. size_t GetAllocationSize() {return m_dwAllocationSize;}
  86. size_t GetUsedSize() {return m_dwUsed;}
  87. static size_t GetMinAllocationSize(size_t dwBlock)
  88. {return dwBlock + GetHeaderSize();}
  89. void Init(size_t dwAllocationSize);
  90. void* Alloc(size_t nBlockSize);
  91. bool Contains(void* p);
  92. bool Free(size_t nBlockSize);
  93. void Destroy();
  94. };
  95. CPointerArray<CAllocation>* m_pAllocations;
  96. DWORD m_dwTotalUsed;
  97. DWORD m_dwTotalAllocated;
  98. DWORD m_dwNumAllocations;
  99. DWORD m_dwNumMisses;
  100. protected:
  101. inline size_t RoundUp(size_t nSize) {return DEF_ALIGNED(nSize);}
  102. public:
  103. CTempMemoryManager();
  104. ~CTempMemoryManager();
  105. void* Allocate(size_t nBlockSize);
  106. void Free(void* p, size_t nBlockSize);
  107. void Clear();
  108. };
  109. #define MAX_ALLOCA_USE 100
  110. template <class TArg>
  111. class CTempArray
  112. {
  113. protected:
  114. BYTE* m_a;
  115. BOOL m_bStack;
  116. int m_nByteSize;
  117. TArg* GetArray() {return (TArg*)m_a;}
  118. const TArg* GetArray() const {return (const TArg*)m_a;}
  119. public:
  120. inline CTempArray() : m_a(NULL), m_bStack(TRUE), m_nByteSize(0){}
  121. inline ~CTempArray()
  122. {
  123. if(!m_bStack)
  124. delete [] m_a;
  125. }
  126. operator TArg*() {return (TArg*)m_a;}
  127. operator const TArg*() const {return (const TArg*)m_a;}
  128. TArg& operator[](int nIndex) {return GetArray()[nIndex];}
  129. const TArg& operator[](int nIndex) const {return GetArray()[nIndex];}
  130. TArg& operator[](long lIndex) {return GetArray()[lIndex];}
  131. const TArg& operator[](long lIndex) const {return GetArray()[lIndex];}
  132. void SetMem(void* p) {m_a = (BYTE*)p;}
  133. BOOL SetSize(int nSize)
  134. {
  135. m_nByteSize = nSize * sizeof(TArg);
  136. if(m_nByteSize < MAX_ALLOCA_USE)
  137. {
  138. m_bStack = TRUE;
  139. return TRUE;
  140. }
  141. else
  142. {
  143. m_bStack = FALSE;
  144. return FALSE;
  145. }
  146. }
  147. int GetByteSize() {return m_nByteSize;}
  148. BOOL IsNull() {return (m_a == NULL);}
  149. };
  150. // This macro initializes the CTempArray with a given size. First it "sets" the
  151. // size into the array, which returns TRUE if _alloca can be used. If so, it
  152. // uses alloca on the now-computed byte size of the array.
  153. #define INIT_TEMP_ARRAY(ARRAY, SIZE) \
  154. ((ARRAY.SetMem( \
  155. (ARRAY.SetSize(SIZE)) ? \
  156. _alloca(ARRAY.GetByteSize()) : \
  157. new BYTE[ARRAY.GetByteSize()] \
  158. ) \
  159. ), !ARRAY.IsNull()) \
  160. #endif