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.

284 lines
7.1 KiB

  1. /*++
  2. Copyright (C) 1992-2001 Microsoft Corporation
  3. Module Name:
  4. ARRAY_P.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. // This is a part of the Microsoft Foundation Classes C++ library.
  9. // Copyright (C) 1992-1993 Microsoft Corporation
  10. // All rights reserved.
  11. //
  12. // This source code is only intended as a supplement to the
  13. // Microsoft Foundation Classes Reference and Microsoft
  14. // QuickHelp and/or WinHelp documentation provided with the library.
  15. // See these sources for detailed information regarding the
  16. // Microsoft Foundation Classes product.
  17. /////////////////////////////////////////////////////////////////////////////
  18. //
  19. // Implementation of parameterized Array
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22. // NOTE: we allocate an array of 'm_nMaxSize' elements, but only
  23. // the current size 'm_nSize' contains properly constructed
  24. // objects.
  25. #include "precomp.h"
  26. #define ASSERT_VALID(x)
  27. #define ASSERT(x)
  28. /////////////////////////////////////////////////////////////////////////////
  29. CPtrArray::CPtrArray()
  30. {
  31. m_pData = NULL;
  32. m_nSize = m_nMaxSize = m_nGrowBy = 0;
  33. }
  34. CPtrArray::~CPtrArray()
  35. {
  36. ASSERT_VALID(this);
  37. delete (BYTE*) m_pData;
  38. }
  39. BOOL CPtrArray::SetSize(int nNewSize, int nGrowBy /* = -1 */)
  40. {
  41. ASSERT_VALID(this);
  42. ASSERT(nNewSize >= 0);
  43. if (nGrowBy != -1)
  44. m_nGrowBy = nGrowBy; // set new size
  45. if (nNewSize == 0)
  46. {
  47. // shrink to nothing
  48. delete (BYTE *) m_pData;
  49. m_pData = NULL;
  50. m_nSize = m_nMaxSize = 0;
  51. }
  52. else if (m_pData == NULL)
  53. {
  54. // create one with exact size
  55. #ifdef SIZE_T_MAX
  56. ASSERT((long)nNewSize * sizeof(void*) <= SIZE_T_MAX); // no overflow
  57. #endif
  58. m_pData = (void**) new BYTE[nNewSize * sizeof(void*)];
  59. if(m_pData == NULL)
  60. return FALSE;
  61. memset(m_pData, 0, nNewSize * sizeof(void*)); // zero fill
  62. m_nSize = m_nMaxSize = nNewSize;
  63. }
  64. else if (nNewSize <= m_nMaxSize)
  65. {
  66. // it fits
  67. if (nNewSize > m_nSize)
  68. {
  69. // initialize the new elements
  70. memset(&m_pData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(void*));
  71. }
  72. m_nSize = nNewSize;
  73. }
  74. else
  75. {
  76. // Otherwise grow array
  77. int nNewMax;
  78. if (nNewSize < m_nMaxSize + m_nGrowBy)
  79. nNewMax = m_nMaxSize + m_nGrowBy; // granularity
  80. else
  81. nNewMax = nNewSize; // no slush
  82. #ifdef SIZE_T_MAX
  83. ASSERT((long)nNewMax * sizeof(void*) <= SIZE_T_MAX); // no overflow
  84. #endif
  85. void** pNewData = (void**) new BYTE[nNewMax * sizeof(void*)];
  86. if(pNewData == NULL)
  87. return FALSE;
  88. // copy new data from old
  89. memcpy(pNewData, m_pData, m_nSize * sizeof(void*));
  90. // construct remaining elements
  91. ASSERT(nNewSize > m_nSize);
  92. memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(void*));
  93. // get rid of old stuff (note: no destructors called)
  94. delete (BYTE*)m_pData;
  95. m_pData = pNewData;
  96. m_nSize = nNewSize;
  97. m_nMaxSize = nNewMax;
  98. }
  99. return TRUE;
  100. }
  101. void CPtrArray::FreeExtra()
  102. {
  103. ASSERT_VALID(this);
  104. if (m_nSize != m_nMaxSize)
  105. {
  106. // shrink to desired size
  107. #ifdef SIZE_T_MAX
  108. ASSERT((long)m_nSize * sizeof(void*) <= SIZE_T_MAX); // no overflow
  109. #endif
  110. void** pNewData = NULL;
  111. if (m_nSize != 0)
  112. {
  113. pNewData = (void**) new BYTE[m_nSize * sizeof(void*)];
  114. // copy new data from old
  115. memcpy(pNewData, m_pData, m_nSize * sizeof(void*));
  116. }
  117. // get rid of old stuff (note: no destructors called)
  118. delete (BYTE*)m_pData;
  119. m_pData = pNewData;
  120. m_nMaxSize = m_nSize;
  121. }
  122. }
  123. /////////////////////////////////////////////////////////////////////////////
  124. BOOL CPtrArray::SetAtGrow(int nIndex, void* newElement)
  125. {
  126. ASSERT_VALID(this);
  127. ASSERT(nIndex >= 0);
  128. if (nIndex >= m_nSize)
  129. {
  130. if(!SetSize(nIndex+1))
  131. return FALSE;
  132. }
  133. m_pData[nIndex] = newElement;
  134. return TRUE;
  135. }
  136. void CPtrArray::InsertAt(int nIndex, void* newElement, int nCount /*=1*/)
  137. {
  138. ASSERT_VALID(this);
  139. ASSERT(nIndex >= 0); // will expand to meet need
  140. ASSERT(nCount > 0); // zero or negative size not allowed
  141. if (nIndex >= m_nSize)
  142. {
  143. // adding after the end of the array
  144. SetSize(nIndex + nCount); // grow so nIndex is valid
  145. }
  146. else
  147. {
  148. // inserting in the middle of the array
  149. int nOldSize = m_nSize;
  150. SetSize(m_nSize + nCount); // grow it to new size
  151. // shift old data up to fill gap
  152. memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
  153. (nOldSize-nIndex) * sizeof(void*));
  154. // re-init slots we copied from
  155. memset(&m_pData[nIndex], 0, nCount * sizeof(void*));
  156. }
  157. // insert new value in the gap
  158. ASSERT(nIndex + nCount <= m_nSize);
  159. while (nCount--)
  160. m_pData[nIndex++] = newElement;
  161. }
  162. void CPtrArray::RemoveAt(int nIndex, int nCount /* = 1 */)
  163. {
  164. ASSERT_VALID(this);
  165. ASSERT(nIndex >= 0);
  166. ASSERT(nCount >= 0);
  167. ASSERT(nIndex + nCount <= m_nSize);
  168. // just remove a range
  169. int nMoveCount = m_nSize - (nIndex + nCount);
  170. if (nMoveCount)
  171. memcpy(&m_pData[nIndex], &m_pData[nIndex + nCount],
  172. nMoveCount * sizeof(void*));
  173. m_nSize -= nCount;
  174. }
  175. void CPtrArray::InsertAt(int nStartIndex, CPtrArray* pNewArray)
  176. {
  177. ASSERT_VALID(this);
  178. ASSERT(pNewArray != NULL);
  179. ASSERT(pNewArray->IsKindOf(RUNTIME_CLASS(CPtrArray)));
  180. ASSERT_VALID(pNewArray);
  181. ASSERT(nStartIndex >= 0);
  182. if (pNewArray->GetSize() > 0)
  183. {
  184. InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
  185. for (int i = 0; i < pNewArray->GetSize(); i++)
  186. SetAt(nStartIndex + i, pNewArray->GetAt(i));
  187. }
  188. }
  189. /////////////////////////////////////////////////////////////////////////////
  190. // Serialization
  191. /////////////////////////////////////////////////////////////////////////////
  192. // Diagnostics
  193. /*
  194. #ifdef _DEBUG
  195. void CPtrArray::Dump(CDumpContext& dc) const
  196. {
  197. ASSERT_VALID(this);
  198. #define MAKESTRING(x) #x
  199. AFX_DUMP1(dc, "a " MAKESTRING(CPtrArray) " with ", m_nSize);
  200. AFX_DUMP0(dc, " elements");
  201. #undef MAKESTRING
  202. if (dc.GetDepth() > 0)
  203. {
  204. AFX_DUMP0(dc, "\n");
  205. for (int i = 0; i < m_nSize; i++)
  206. {
  207. AFX_DUMP1(dc, "\n\t[", i);
  208. AFX_DUMP1(dc, "] = ", m_pData[i]);
  209. }
  210. }
  211. }
  212. void CPtrArray::AssertValid() const
  213. {
  214. CObject::AssertValid();
  215. if (m_pData == NULL)
  216. {
  217. ASSERT(m_nSize == 0);
  218. ASSERT(m_nMaxSize == 0);
  219. }
  220. else
  221. {
  222. ASSERT(m_nSize >= 0);
  223. ASSERT(m_nMaxSize >= 0);
  224. ASSERT(m_nSize <= m_nMaxSize);
  225. ASSERT(AfxIsValidAddress(m_pData, m_nMaxSize * sizeof(void*)));
  226. }
  227. }
  228. #endif //_DEBUG
  229. */
  230. /////////////////////////////////////////////////////////////////////////////