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.

182 lines
4.3 KiB

  1. //+------------------------------------------------------------
  2. //
  3. // Copyright (C) 1998, Microsoft Corporation
  4. //
  5. // File: simparray.h
  6. //
  7. // Contents: Simple growable array class
  8. //
  9. // Classes: CSimpArray
  10. //
  11. // Functions:
  12. //
  13. // History:
  14. // jstamerj 1998/07/14 11:30:13: Created.
  15. //
  16. //-------------------------------------------------------------
  17. #ifndef __SIMPARRAY_H__
  18. #define __SIMPARRAY_H__
  19. #include <windows.h>
  20. #include "spinlock.h"
  21. #include <dbgtrace.h>
  22. //
  23. // If you want this array to behave as follows:
  24. // When Inserting an array element and the allocated array size is
  25. // not sufficient,
  26. // Alloc an array size of CSIMPARRAY_DEFAULT_INITIAL_SIZE the
  27. // first time
  28. // Double the current array size until sufficient thereafter
  29. // Then define CSIMPARRAY_DOUBLE and CSIMPARRAY_DEFAULT_INITIAL_SIZE
  30. //
  31. // Otherwise, it will allocate only as much space as needed when
  32. // needed.
  33. //
  34. // Define this to attempt to double the array size when reallocing is necessary
  35. //#undef CSIMPARRAY_DOUBLE
  36. // Default initial allocation
  37. //#undef CSIMPARRAY_DEFAULT_INITIAL_SIZE 20
  38. //+------------------------------------------------------------
  39. //
  40. // Class: CSimpArray
  41. //
  42. // Synopsis: Simple array class with usefull msgcat utility functions
  43. //
  44. // Hungarian: csa, pcsa
  45. //
  46. // History:
  47. // jstamerj 1998/07/15 12:15:50: Created.
  48. //
  49. //-------------------------------------------------------------
  50. template <class T> class CSimpArray
  51. {
  52. public:
  53. CSimpArray();
  54. ~CSimpArray();
  55. // Optinal Initialize function - reserves array memory for a
  56. // specified array size
  57. HRESULT Initialize(DWORD dwSize);
  58. // Add one element to the array
  59. HRESULT Add(T Data);
  60. // Add a real array to this array
  61. HRESULT AddArray(DWORD dwSize, T *pData);
  62. // Number of valid elements added to the array
  63. DWORD Size();
  64. // Direct access to the array
  65. operator T * ();
  66. private:
  67. HRESULT AllocArrayRange(DWORD dwSize, PDWORD pdwIndex);
  68. HRESULT ReAllocArrayIfNecessary(DWORD dwSize);
  69. #define SIGNATURE_CSIMPARRAY (DWORD)'SArr'
  70. #define SIGNATURE_CSIMPARRAY_INVALID (DWORD) 'XArr'
  71. DWORD m_dwSignature;
  72. DWORD m_dwArrayAllocSize;
  73. DWORD m_dwArrayClaimedSize;
  74. DWORD m_dwArrayValidSize;
  75. T * m_rgData;
  76. SPIN_LOCK m_slAllocate;
  77. };
  78. //+------------------------------------------------------------
  79. //
  80. // Function: CSimpArray::CSimpArary (constructor)
  81. //
  82. // Synopsis: Initialize member data
  83. //
  84. // Arguments: NONE
  85. //
  86. // Returns: NOTHIGN
  87. //
  88. // History:
  89. // jstamerj 1998/07/14 11:39:08: Created.
  90. //
  91. //-------------------------------------------------------------
  92. template <class T> inline CSimpArray<T>::CSimpArray()
  93. {
  94. m_dwSignature = SIGNATURE_CSIMPARRAY;
  95. m_dwArrayAllocSize = m_dwArrayClaimedSize = m_dwArrayValidSize = 0;
  96. m_rgData = NULL;
  97. InitializeSpinLock(&m_slAllocate);
  98. }
  99. //+------------------------------------------------------------
  100. //
  101. // Function: CSimpArray::~CSimpArray (destructor)
  102. //
  103. // Synopsis: Free's memory
  104. //
  105. // Arguments: NONE
  106. //
  107. // Returns: NOTHING
  108. //
  109. // History:
  110. // jstamerj 1998/07/14 12:19:10: Created.
  111. //
  112. //-------------------------------------------------------------
  113. template <class T> inline CSimpArray<T>::~CSimpArray()
  114. {
  115. _ASSERT(m_dwSignature == SIGNATURE_CSIMPARRAY);
  116. m_dwSignature = SIGNATURE_CSIMPARRAY_INVALID;
  117. delete m_rgData;
  118. }
  119. //+------------------------------------------------------------
  120. //
  121. // Function: CSimpArray::operator T*
  122. //
  123. // Synopsis: Returns pointer to the array
  124. //
  125. // Arguments: NONE
  126. //
  127. // Returns: pointer to array of T's or NULL (if nothing is allocated)
  128. //
  129. // History:
  130. // jstamerj 1998/07/14 14:15:21: Created.
  131. //
  132. //-------------------------------------------------------------
  133. template <class T> inline CSimpArray<T>::operator T*()
  134. {
  135. return m_rgData;
  136. }
  137. //+------------------------------------------------------------
  138. //
  139. // Function: CSimpArray::Size
  140. //
  141. // Synopsis: Returns the count of (valid) array elements.
  142. //
  143. // Arguments: NONE
  144. //
  145. // Returns: DWORD size
  146. //
  147. // History:
  148. // jstamerj 1998/07/14 14:16:36: Created.
  149. //
  150. //-------------------------------------------------------------
  151. template <class T> inline DWORD CSimpArray<T>::Size()
  152. {
  153. return m_dwArrayValidSize;
  154. }
  155. #endif //__SIMPARRAY_H__