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.

405 lines
8.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: alias.cxx
  7. //
  8. // Contents: Alias implementations
  9. //
  10. // History: 26-May-94 DrewB Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "headers.cxx"
  14. #pragma hdrstop
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Member: CAliasBlock::CAliasBlock, public
  18. //
  19. // Synopsis: Constructor
  20. //
  21. // History: 26-May-94 DrewB Created
  22. //
  23. //----------------------------------------------------------------------------
  24. CAliasBlock::CAliasBlock(ALIAS aliasBase,
  25. CAliasBlock *pabNext)
  26. {
  27. _aliasBase = aliasBase;
  28. _iFilled = 0;
  29. _pabNext = pabNext;
  30. // Since INVALID_VALUE is a DWORD we can't directly memset it,
  31. // but we'd like to use memset so assert that it's a known value
  32. // and go ahead
  33. thkAssert(INVALID_VALUE == 0);
  34. memset(_dwValues, 0, sizeof(_dwValues));
  35. }
  36. //+---------------------------------------------------------------------------
  37. //
  38. // Member: CAliasBlock::ValueAlias, public
  39. //
  40. // Synopsis: Find the alias for a value
  41. //
  42. // Arguments: [dwValue] - Value
  43. //
  44. // Returns: Alias or INVALID_ALIAS
  45. //
  46. // History: 26-May-94 DrewB Created
  47. //
  48. //----------------------------------------------------------------------------
  49. ALIAS CAliasBlock::ValueAlias(DWORD dwValue)
  50. {
  51. int i;
  52. DWORD *pdw;
  53. thkAssert(dwValue != INVALID_VALUE);
  54. #if DBG == 1
  55. CheckFree();
  56. #endif
  57. if (_iFilled == 0)
  58. {
  59. return INVALID_ALIAS;
  60. }
  61. pdw = _dwValues;
  62. for (i = 0; i < ALIAS_BLOCK_SIZE; i++)
  63. {
  64. if (*pdw == dwValue)
  65. {
  66. return IndexAlias(i);
  67. }
  68. pdw++;
  69. }
  70. return INVALID_ALIAS;
  71. }
  72. //+---------------------------------------------------------------------------
  73. //
  74. // Member: CAliasBlock::AddValue, public
  75. //
  76. // Synopsis: Adds a new value
  77. //
  78. // Arguments: [dwValue] - New value
  79. //
  80. // Returns: Alias for block or INVALID_ALIAS
  81. //
  82. // History: 26-May-94 DrewB Created
  83. //
  84. // Notes: Duplicates are not allowed
  85. //
  86. //----------------------------------------------------------------------------
  87. ALIAS CAliasBlock::AddValue(DWORD dwValue)
  88. {
  89. int i;
  90. DWORD *pdw;
  91. thkAssert(dwValue != INVALID_VALUE);
  92. #if DBG == 1
  93. CheckFree();
  94. #endif
  95. if (_iFilled == ALIAS_BLOCK_SIZE)
  96. {
  97. return INVALID_ALIAS;
  98. }
  99. // Check for duplicates
  100. thkAssert(ValueAlias(dwValue) == INVALID_ALIAS);
  101. pdw = _dwValues;
  102. for (i = 0; i < ALIAS_BLOCK_SIZE; i++)
  103. {
  104. if (*pdw == INVALID_VALUE)
  105. {
  106. break;
  107. }
  108. pdw++;
  109. }
  110. thkAssert(i < ALIAS_BLOCK_SIZE);
  111. _iFilled++;
  112. _dwValues[i] = dwValue;
  113. return IndexAlias(i);
  114. }
  115. //+---------------------------------------------------------------------------
  116. //
  117. // Member: CAliasBlock::CheckFree, public debug
  118. //
  119. // Synopsis: Checks to make sure that _iFilled is correct
  120. //
  121. // History: 30-May-94 DrewB Created
  122. //
  123. //----------------------------------------------------------------------------
  124. #if DBG == 1
  125. void CAliasBlock::CheckFree(void)
  126. {
  127. int i, iFilled;
  128. DWORD *pdw;
  129. iFilled = 0;
  130. pdw = _dwValues;
  131. for (i = 0; i < ALIAS_BLOCK_SIZE; i++)
  132. {
  133. if (*pdw != INVALID_VALUE)
  134. {
  135. iFilled++;
  136. }
  137. pdw++;
  138. }
  139. thkAssert(iFilled == _iFilled);
  140. }
  141. #endif
  142. //+---------------------------------------------------------------------------
  143. //
  144. // Function: CAliases::~CAliases, public
  145. //
  146. // Synopsis: Destructor
  147. //
  148. // History: 26-May-94 DrewB Created
  149. //
  150. //----------------------------------------------------------------------------
  151. CAliases::~CAliases(void)
  152. {
  153. CAliasBlock *pab;
  154. while (_pabAliases)
  155. {
  156. pab = _pabAliases->GetNext();
  157. //
  158. // The first alias block added to the list is a static one. We
  159. // cannot call the heap to deallocate it.
  160. //
  161. if (_pabAliases != &_abStatic)
  162. {
  163. delete _pabAliases;
  164. }
  165. _pabAliases = pab;
  166. }
  167. }
  168. //+---------------------------------------------------------------------------
  169. //
  170. // Member: CAliases::AliasValue, public
  171. //
  172. // Synopsis: Returns the value for an alias
  173. //
  174. // Arguments: [alias] - Alias
  175. //
  176. // Returns: Value
  177. //
  178. // History: 26-May-94 DrewB Created
  179. //
  180. // Notes: Alias must be valid
  181. //
  182. //----------------------------------------------------------------------------
  183. DWORD CAliases::AliasValue(ALIAS alias)
  184. {
  185. CAliasBlock *pab;
  186. for (pab = _pabAliases; pab; pab = pab->GetNext())
  187. {
  188. if (pab->ContainsAlias(alias))
  189. {
  190. return pab->AliasValue(alias);
  191. }
  192. }
  193. thkAssert(!"Invalid alias in CAliases::AliasValue");
  194. return 0xffffffff;
  195. }
  196. //+---------------------------------------------------------------------------
  197. //
  198. // Function: CAliases::ValueAlias, public
  199. //
  200. // Synopsis: Returns the alias for a value
  201. //
  202. // Arguments: [dwValue] - Value
  203. //
  204. // Returns: Alias
  205. //
  206. // History: 26-May-94 DrewB Created
  207. //
  208. //----------------------------------------------------------------------------
  209. ALIAS CAliases::ValueAlias(DWORD dwValue)
  210. {
  211. CAliasBlock *pab;
  212. ALIAS alias;
  213. for (pab = _pabAliases; pab; pab = pab->GetNext())
  214. {
  215. alias = pab->ValueAlias(dwValue);
  216. if (alias != INVALID_ALIAS)
  217. {
  218. return alias;
  219. }
  220. }
  221. return INVALID_ALIAS;
  222. }
  223. //+---------------------------------------------------------------------------
  224. //
  225. // Function: CAliases::AddValue, public
  226. //
  227. // Synopsis: Adds a value and returns its alias
  228. //
  229. // Arguments: [dwValue] - Value
  230. //
  231. // Returns: Alias or INVALID_ALIAS
  232. //
  233. // History: 26-May-94 DrewB Created
  234. //
  235. //----------------------------------------------------------------------------
  236. ALIAS CAliases::AddValue(DWORD dwValue)
  237. {
  238. CAliasBlock *pab;
  239. ALIAS alias;
  240. for (pab = _pabAliases; pab; pab = pab->GetNext())
  241. {
  242. alias = pab->AddValue(dwValue);
  243. if (alias != INVALID_ALIAS)
  244. {
  245. return alias;
  246. }
  247. }
  248. if ((long)_aliasBase+ALIAS_BLOCK_SIZE >= INVALID_ALIAS)
  249. {
  250. return INVALID_ALIAS;
  251. }
  252. pab = new CAliasBlock(_aliasBase+ALIAS_BLOCK_SIZE, _pabAliases);
  253. if (pab == NULL)
  254. {
  255. return INVALID_ALIAS;
  256. }
  257. _aliasBase += ALIAS_BLOCK_SIZE;
  258. _pabAliases = pab;
  259. alias = pab->AddValue(dwValue);
  260. thkAssert(alias != INVALID_ALIAS);
  261. return alias;
  262. }
  263. //+---------------------------------------------------------------------------
  264. //
  265. // Function: CAliases::RemoveAlias, public
  266. //
  267. // Synopsis: Removes an alias
  268. //
  269. // Arguments: [alias] - Alias
  270. //
  271. // History: 26-May-94 DrewB Created
  272. //
  273. //----------------------------------------------------------------------------
  274. void CAliases::RemoveAlias(ALIAS alias)
  275. {
  276. CAliasBlock *pab, *pabPrev;
  277. pabPrev = NULL;
  278. for (pab = _pabAliases; pab; pabPrev = pab, pab = pab->GetNext())
  279. {
  280. if (pab->ContainsAlias(alias))
  281. {
  282. pab->RemoveAlias(alias);
  283. if (pab->AliasesFilled() == 0)
  284. {
  285. DeleteBlock(pab, pabPrev);
  286. }
  287. return;
  288. }
  289. }
  290. thkAssert(!"Invalid alias in CAliases::RemoveAlias");
  291. }
  292. //+---------------------------------------------------------------------------
  293. //
  294. // Function: CAliases::SetValue, public
  295. //
  296. // Synopsis: Sets the value for an alias
  297. //
  298. // Arguments: [alias] - Alias
  299. // [dwValue] - Value
  300. //
  301. // History: 26-May-94 DrewB Created
  302. //
  303. //----------------------------------------------------------------------------
  304. void CAliases::SetValue(ALIAS alias, DWORD dwValue)
  305. {
  306. CAliasBlock *pab;
  307. for (pab = _pabAliases; pab; pab = pab->GetNext())
  308. {
  309. if (pab->ContainsAlias(alias))
  310. {
  311. pab->SetValue(alias, dwValue);
  312. }
  313. }
  314. }
  315. //+---------------------------------------------------------------------------
  316. //
  317. // Member: CAliases::DeleteBlock, private
  318. //
  319. // Synopsis: Deletes an alias block if it's not the static block
  320. //
  321. // Arguments: [pab] - Alias block
  322. // [pabPrev] - Previous alias block
  323. //
  324. // History: 27-May-94 DrewB Created
  325. //
  326. //----------------------------------------------------------------------------
  327. void CAliases::DeleteBlock(CAliasBlock *pab, CAliasBlock *pabPrev)
  328. {
  329. if (pab == &_abStatic)
  330. {
  331. return;
  332. }
  333. if (pabPrev)
  334. {
  335. pabPrev->SetNext(pab->GetNext());
  336. }
  337. else
  338. {
  339. _pabAliases = pab->GetNext();
  340. }
  341. delete pab;
  342. }