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.

335 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. ptrmap.cxx
  5. Abstract:
  6. A helper class for mapping ID to 32 or 64 bit ptr
  7. Author:
  8. Kestutis Patiejunas (kestutip) 08-Dec-1998
  9. Revision History:
  10. Notes:
  11. --*/
  12. #include "precomp.hxx"
  13. #ifdef _X86_
  14. /*++
  15. Routine Description:
  16. Constructor
  17. Arguments: ignored
  18. Return Value: NA
  19. --*/
  20. CIdToPointerMapper::CIdToPointerMapper(
  21. DWORD ,
  22. DWORD )
  23. {
  24. }
  25. CIdToPointerMapper::~CIdToPointerMapper()
  26. {
  27. }
  28. VOID
  29. CIdToPointerMapper::VerifyOutstandinMappings()
  30. {
  31. }
  32. /*++
  33. Routine Description:
  34. Finds a mapping in mapping table between DWORD ID and pointer associated
  35. Arguments:
  36. DWORD ID - and ID to which mapping should be deleted.
  37. Return Value:
  38. PVOID - the pointer associated with the given ID.
  39. NULL indicates a failure.
  40. --*/
  41. PVOID
  42. CIdToPointerMapper::FindMapping(
  43. DWORD id)
  44. {
  45. return (PVOID)id;
  46. }
  47. /*++
  48. Routine Description:
  49. Deletes a mapping from mapping table between dword ID and PTR
  50. Arguments: ignored
  51. Return Value:
  52. BOOL TRUE is succeded
  53. --*/
  54. BOOL
  55. CIdToPointerMapper::DeleteMapping(
  56. DWORD )
  57. {
  58. return TRUE;
  59. }
  60. /*++
  61. Routine Description:
  62. Takes a PVOID pointer and returns a DWORD ID associated,which should be used
  63. in mapping it back to ptr
  64. Arguments:
  65. PVOID ptr - a pointer of 32/64 bits which should be mapped into dword
  66. Return Value:
  67. DWORD - an ID associated with a given pointer . Starts from 1.
  68. Zero indicates a failure to craete mapping.
  69. --*/
  70. DWORD
  71. CIdToPointerMapper::AddMapping(
  72. PVOID ptr)
  73. {
  74. return (DWORD)ptr;
  75. }
  76. #else // ifdef _X86_
  77. /*++
  78. Routine Description:
  79. Constructor
  80. Arguments:
  81. nStartMaps - initial nubmer of possible mappings in table
  82. nIncreaseMaps - number of increase for table when there is not enought space
  83. Return Value:
  84. sucess is tored in m_fInitialized
  85. --*/
  86. CIdToPointerMapper::CIdToPointerMapper(DWORD nStartMaps,DWORD nIncreaseMaps):
  87. m_nStartMaps(nStartMaps),
  88. m_nIncreaseMaps(nIncreaseMaps)
  89. {
  90. if (!m_nStartMaps)
  91. {
  92. m_nStartMaps = DEFAULT_START_NUMBER_OF_MAPS;
  93. }
  94. if (!nIncreaseMaps)
  95. {
  96. m_nIncreaseMaps = DEFAULT_INCREASE_NUMBER_OF_MAPS;
  97. }
  98. // initial empty list head index
  99. m_EmptyPlace = 0;
  100. m_nMappings = 0;
  101. m_Map = NULL;
  102. // allocate a mem for mapping
  103. m_pBufferObj = new BUFFER (m_nStartMaps * sizeof(MapperElement));
  104. if( m_pBufferObj )
  105. {
  106. m_Map = (MapperElement *) m_pBufferObj->QueryPtr();
  107. }
  108. if (m_Map)
  109. {
  110. // initialized mappaing space so the every element point to next one
  111. for (DWORD i=0; i< m_nStartMaps; i++)
  112. {
  113. m_Map[i].fInUse = FALSE;
  114. m_Map[i].dwIndex = i+1;
  115. }
  116. // just the last has special value
  117. m_Map[m_nStartMaps-1].dwIndex = MAPPING_NO_EMPTY_PLACE_IDX;
  118. m_fInitialized = TRUE;
  119. }
  120. else
  121. {
  122. m_fInitialized =FALSE;
  123. }
  124. }
  125. CIdToPointerMapper::~CIdToPointerMapper()
  126. {
  127. VerifyOutstandinMappings ();
  128. if (m_fInitialized)
  129. {
  130. delete m_pBufferObj;
  131. }
  132. }
  133. VOID CIdToPointerMapper::VerifyOutstandinMappings ()
  134. {
  135. MD_ASSERT (m_nMappings==0);
  136. }
  137. /*++
  138. Routine Description:
  139. Finds a mapping in mapping table between DWORD ID and pointer associated
  140. Arguments:
  141. DWORD ID - and ID to which mapping should be deleted.
  142. Return Value:
  143. DWORD - an ID associated with a given pointer . Starts from 1.
  144. Zero indicates a failure to craete mapping.
  145. --*/
  146. PVOID CIdToPointerMapper::FindMapping (DWORD id)
  147. {
  148. PVOID retVal = NULL;
  149. if (m_fInitialized)
  150. {
  151. id--;
  152. MD_ASSERT (id < m_nStartMaps);
  153. if (id < m_nStartMaps &&
  154. m_Map[id].fInUse)
  155. {
  156. retVal = m_Map[id].pData;
  157. }
  158. }
  159. return retVal;
  160. }
  161. /*++
  162. Routine Description:
  163. Deletes a mapping from mapping table between dword ID and PTR
  164. Arguments:
  165. DWORD ID - and ID to which mapping should be deleted.
  166. Return Value:
  167. BOOL TRUE is succeded
  168. --*/
  169. BOOL CIdToPointerMapper::DeleteMapping (DWORD id)
  170. {
  171. BOOL retVal = FALSE;
  172. if (m_fInitialized)
  173. {
  174. id--;
  175. MD_ASSERT (id < m_nStartMaps);
  176. if (id < m_nStartMaps)
  177. {
  178. MD_ASSERT (m_Map[id].fInUse);
  179. // get the ptr from element with index [id]
  180. if (m_Map[id].fInUse)
  181. {
  182. // add elemen to empty elements list
  183. m_Map[id].fInUse = FALSE;
  184. m_Map[id].dwIndex = m_EmptyPlace;
  185. m_EmptyPlace = id;
  186. MD_ASSERT(m_nMappings);
  187. m_nMappings--;
  188. retVal = TRUE;
  189. }
  190. }
  191. }
  192. return retVal;
  193. }
  194. /*++
  195. Routine Description:
  196. Takes a PVOID pointer and returns a DWORD ID associated,which should be used
  197. in mapping it back to ptr
  198. Arguments:
  199. PVOID ptr - a pointer of 32/64 bits which should be mapped into dword
  200. Return Value:
  201. DWORD - an ID associated with a given pointer . Starts from 1.
  202. Zero indicates a failure to craete mapping.
  203. --*/
  204. DWORD CIdToPointerMapper::AddMapping (PVOID ptr)
  205. {
  206. DWORD retVal=0;
  207. DWORD newSize, i, dwPlace;
  208. if (m_fInitialized)
  209. {
  210. if (m_EmptyPlace == MAPPING_NO_EMPTY_PLACE_IDX)
  211. {
  212. // case when there is not enough mem , so then try to realloc
  213. newSize = m_nStartMaps + m_nIncreaseMaps;
  214. if (!m_pBufferObj->Resize(newSize * sizeof(MapperElement)))
  215. {
  216. return 0;
  217. }
  218. m_Map = (MapperElement *) m_pBufferObj->QueryPtr();
  219. // realloc succeded initialize the remainder as free list
  220. for (i=m_nStartMaps; i<newSize; i++)
  221. {
  222. m_Map[i].fInUse = FALSE;
  223. m_Map[i].dwIndex = i+1;
  224. }
  225. m_Map[newSize-1].dwIndex = MAPPING_NO_EMPTY_PLACE_IDX;
  226. m_EmptyPlace = m_nStartMaps;
  227. m_nStartMaps = newSize;
  228. }
  229. // case when there was at least one empty element in free list
  230. MD_ASSERT(!m_Map[m_EmptyPlace].fInUse);
  231. dwPlace = m_EmptyPlace;
  232. m_EmptyPlace = m_Map[m_EmptyPlace].dwIndex;
  233. // add a pointer into array and return associated ID
  234. // note, that we return dwPlace+1 ,a s we don't use ID zero
  235. m_Map[dwPlace].pData = ptr;
  236. m_Map[dwPlace].fInUse = TRUE;
  237. retVal = dwPlace + 1;
  238. m_nMappings++;
  239. }
  240. return retVal;
  241. }
  242. #endif // ifdef _X86_