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.

890 lines
24 KiB

  1. //------------------------------------------------------------------------------
  2. // File: WXList.cpp
  3. //
  4. // Desc: DirectShow base classes - implements a non-MFC based generic list
  5. // template class.
  6. //
  7. //@@BEGIN_MSINTERNAL
  8. //
  9. // December 1994
  10. //
  11. //@@END_MSINTERNAL
  12. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  13. //------------------------------------------------------------------------------
  14. /* A generic list of pointers to objects.
  15. Objectives: avoid using MFC libraries in ndm kernel mode and
  16. provide a really useful list type.
  17. The class is thread safe in that separate threads may add and
  18. delete items in the list concurrently although the application
  19. must ensure that constructor and destructor access is suitably
  20. synchronised.
  21. The list name must not conflict with MFC classes as an
  22. application may use both
  23. The nodes form a doubly linked, NULL terminated chain with an anchor
  24. block (the list object per se) holding pointers to the first and last
  25. nodes and a count of the nodes.
  26. There is a node cache to reduce the allocation and freeing overhead.
  27. It optionally (determined at construction time) has an Event which is
  28. set whenever the list becomes non-empty and reset whenever it becomes
  29. empty.
  30. It optionally (determined at construction time) has a Critical Section
  31. which is entered during the important part of each operation. (About
  32. all you can do outside it is some parameter checking).
  33. The node cache is a repository of nodes that are NOT in the list to speed
  34. up storage allocation. Each list has its own cache to reduce locking and
  35. serialising. The list accesses are serialised anyway for a given list - a
  36. common cache would mean that we would have to separately serialise access
  37. of all lists within the cache. Because the cache only stores nodes that are
  38. not in the list, releasing the cache does not release any list nodes. This
  39. means that list nodes can be copied or rechained from one list to another
  40. without danger of creating a dangling reference if the original cache goes
  41. away.
  42. Questionable design decisions:
  43. 1. Retaining the warts for compatibility
  44. 2. Keeping an element count -i.e. counting whenever we do anything
  45. instead of only when we want the count.
  46. 3. Making the chain pointers NULL terminated. If the list object
  47. itself looks just like a node and the list is kept as a ring then
  48. it reduces the number of special cases. All inserts look the same.
  49. */
  50. #include <streams.h>
  51. /* set cursor to the position of each element of list in turn */
  52. #define INTERNALTRAVERSELIST(list, cursor) \
  53. for ( cursor = (list).GetHeadPositionI() \
  54. ; cursor!=NULL \
  55. ; cursor = (list).Next(cursor) \
  56. )
  57. /* set cursor to the position of each element of list in turn
  58. in reverse order
  59. */
  60. #define INTERNALREVERSETRAVERSELIST(list, cursor) \
  61. for ( cursor = (list).GetTailPositionI() \
  62. ; cursor!=NULL \
  63. ; cursor = (list).Prev(cursor) \
  64. )
  65. /* Constructor calls a separate initialisation function that
  66. creates a node cache, optionally creates a lock object
  67. and optionally creates a signaling object.
  68. By default we create a locking object, a DEFAULTCACHE sized
  69. cache but no event object so the list cannot be used in calls
  70. to WaitForSingleObject
  71. */
  72. CBaseList::CBaseList(TCHAR *pName, // Descriptive list name
  73. INT iItems) : // Node cache size
  74. #ifdef DEBUG
  75. CBaseObject(pName),
  76. #endif
  77. m_pFirst(NULL),
  78. m_pLast(NULL),
  79. m_Count(0),
  80. m_Cache(iItems)
  81. {
  82. } // constructor
  83. CBaseList::CBaseList(TCHAR *pName) : // Descriptive list name
  84. #ifdef DEBUG
  85. CBaseObject(pName),
  86. #endif
  87. m_pFirst(NULL),
  88. m_pLast(NULL),
  89. m_Count(0),
  90. m_Cache(DEFAULTCACHE)
  91. {
  92. } // constructor
  93. #ifdef UNICODE
  94. CBaseList::CBaseList(CHAR *pName, // Descriptive list name
  95. INT iItems) : // Node cache size
  96. #ifdef DEBUG
  97. CBaseObject(pName),
  98. #endif
  99. m_pFirst(NULL),
  100. m_pLast(NULL),
  101. m_Count(0),
  102. m_Cache(iItems)
  103. {
  104. } // constructor
  105. CBaseList::CBaseList(CHAR *pName) : // Descriptive list name
  106. #ifdef DEBUG
  107. CBaseObject(pName),
  108. #endif
  109. m_pFirst(NULL),
  110. m_pLast(NULL),
  111. m_Count(0),
  112. m_Cache(DEFAULTCACHE)
  113. {
  114. } // constructor
  115. #endif
  116. /* The destructor enumerates all the node objects in the list and
  117. in the cache deleting each in turn. We do not do any processing
  118. on the objects that the list holds (i.e. points to) so if they
  119. represent interfaces for example the creator of the list should
  120. ensure that each of them is released before deleting us
  121. */
  122. CBaseList::~CBaseList()
  123. {
  124. /* Delete all our list nodes */
  125. RemoveAll();
  126. } // destructor
  127. /* Remove all the nodes from the list but don't do anything
  128. with the objects that each node looks after (this is the
  129. responsibility of the creator).
  130. Aa a last act we reset the signalling event
  131. (if available) to indicate to clients that the list
  132. does not have any entries in it.
  133. */
  134. void CBaseList::RemoveAll()
  135. {
  136. /* Free up all the CNode objects NOTE we don't bother putting the
  137. deleted nodes into the cache as this method is only really called
  138. in serious times of change such as when we are being deleted at
  139. which point the cache will be deleted anway */
  140. CNode *pn = m_pFirst;
  141. while (pn) {
  142. CNode *op = pn;
  143. pn = pn->Next();
  144. delete op;
  145. }
  146. /* Reset the object count and the list pointers */
  147. m_Count = 0;
  148. m_pFirst = m_pLast = NULL;
  149. } // RemoveAll
  150. /* Return a position enumerator for the entire list.
  151. A position enumerator is a pointer to a node object cast to a
  152. transparent type so all we do is return the head/tail node
  153. pointer in the list.
  154. WARNING because the position is a pointer to a node there is
  155. an implicit assumption for users a the list class that after
  156. deleting an object from the list that any other position
  157. enumerators that you have may be invalid (since the node
  158. may be gone).
  159. */
  160. POSITION CBaseList::GetHeadPositionI() const
  161. {
  162. return (POSITION) m_pFirst;
  163. } // GetHeadPosition
  164. POSITION CBaseList::GetTailPositionI() const
  165. {
  166. return (POSITION) m_pLast;
  167. } // GetTailPosition
  168. /* Get the number of objects in the list,
  169. Get the lock before accessing the count.
  170. Locking may not be entirely necessary but it has the side effect
  171. of making sure that all operations are complete before we get it.
  172. So for example if a list is being added to this list then that
  173. will have completed in full before we continue rather than seeing
  174. an intermediate albeit valid state
  175. */
  176. int CBaseList::GetCountI() const
  177. {
  178. return m_Count;
  179. } // GetCount
  180. /* Return the object at rp, update rp to the next object from
  181. the list or NULL if you have moved over the last object.
  182. You may still call this function once we return NULL but
  183. we will continue to return a NULL position value
  184. */
  185. void *CBaseList::GetNextI(POSITION& rp) const
  186. {
  187. /* have we reached the end of the list */
  188. if (rp == NULL) {
  189. return NULL;
  190. }
  191. /* Lock the object before continuing */
  192. void *pObject;
  193. /* Copy the original position then step on */
  194. CNode *pn = (CNode *) rp;
  195. ASSERT(pn != NULL);
  196. rp = (POSITION) pn->Next();
  197. /* Get the object at the original position from the list */
  198. pObject = pn->GetData();
  199. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  200. return pObject;
  201. } //GetNext
  202. /* Return the object at p.
  203. Asking for the object at NULL ASSERTs then returns NULL
  204. The object is NOT locked. The list is not being changed
  205. in any way. If another thread is busy deleting the object
  206. then locking would only result in a change from one bad
  207. behaviour to another.
  208. */
  209. void *CBaseList::GetI(POSITION p) const
  210. {
  211. if (p == NULL) {
  212. return NULL;
  213. }
  214. CNode * pn = (CNode *) p;
  215. void *pObject = pn->GetData();
  216. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  217. return pObject;
  218. } //Get
  219. /* Return the first position in the list which holds the given pointer.
  220. Return NULL if it's not found.
  221. */
  222. POSITION CBaseList::FindI( void * pObj) const
  223. {
  224. POSITION pn;
  225. INTERNALTRAVERSELIST(*this, pn){
  226. if (GetI(pn)==pObj) {
  227. return pn;
  228. }
  229. }
  230. return NULL;
  231. } // Find
  232. /* Remove the first node in the list (deletes the pointer to its object
  233. from the list, does not free the object itself).
  234. Return the pointer to its object or NULL if empty
  235. */
  236. void *CBaseList::RemoveHeadI()
  237. {
  238. /* All we do is get the head position and ask for that to be deleted.
  239. We could special case this since some of the code path checking
  240. in Remove() is redundant as we know there is no previous
  241. node for example but it seems to gain little over the
  242. added complexity
  243. */
  244. return RemoveI((POSITION)m_pFirst);
  245. } // RemoveHead
  246. /* Remove the last node in the list (deletes the pointer to its object
  247. from the list, does not free the object itself).
  248. Return the pointer to its object or NULL if empty
  249. */
  250. void *CBaseList::RemoveTailI()
  251. {
  252. /* All we do is get the tail position and ask for that to be deleted.
  253. We could special case this since some of the code path checking
  254. in Remove() is redundant as we know there is no previous
  255. node for example but it seems to gain little over the
  256. added complexity
  257. */
  258. return RemoveI((POSITION)m_pLast);
  259. } // RemoveTail
  260. /* Remove the pointer to the object in this position from the list.
  261. Deal with all the chain pointers
  262. Return a pointer to the object removed from the list.
  263. The node object that is freed as a result
  264. of this operation is added to the node cache where
  265. it can be used again.
  266. Remove(NULL) is a harmless no-op - but probably is a wart.
  267. */
  268. void *CBaseList::RemoveI(POSITION pos)
  269. {
  270. /* Lock the critical section before continuing */
  271. // ASSERT (pos!=NULL); // Removing NULL is to be harmless!
  272. if (pos==NULL) return NULL;
  273. CNode *pCurrent = (CNode *) pos;
  274. ASSERT(pCurrent != NULL);
  275. /* Update the previous node */
  276. CNode *pNode = pCurrent->Prev();
  277. if (pNode == NULL) {
  278. m_pFirst = pCurrent->Next();
  279. } else {
  280. pNode->SetNext(pCurrent->Next());
  281. }
  282. /* Update the following node */
  283. pNode = pCurrent->Next();
  284. if (pNode == NULL) {
  285. m_pLast = pCurrent->Prev();
  286. } else {
  287. pNode->SetPrev(pCurrent->Prev());
  288. }
  289. /* Get the object this node was looking after */
  290. void *pObject = pCurrent->GetData();
  291. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  292. /* Try and add the node object to the cache -
  293. a NULL return code from the cache means we ran out of room.
  294. The cache size is fixed by a constructor argument when the
  295. list is created and defaults to DEFAULTCACHE.
  296. This means that the cache will have room for this many
  297. node objects. So if you have a list of media samples
  298. and you know there will never be more than five active at
  299. any given time of them for example then override the default
  300. constructor
  301. */
  302. m_Cache.AddToCache(pCurrent);
  303. /* If the list is empty then reset the list event */
  304. --m_Count;
  305. ASSERT(m_Count >= 0);
  306. return pObject;
  307. } // Remove
  308. /* Add this object to the tail end of our list
  309. Return the new tail position.
  310. */
  311. POSITION CBaseList::AddTailI(void *pObject)
  312. {
  313. /* Lock the critical section before continuing */
  314. CNode *pNode;
  315. // ASSERT(pObject); // NULL pointers in the list are allowed.
  316. /* If there is a node objects in the cache then use
  317. that otherwise we will have to create a new one */
  318. pNode = (CNode *) m_Cache.RemoveFromCache();
  319. if (pNode == NULL) {
  320. pNode = new CNode;
  321. }
  322. /* Check we have a valid object */
  323. if (pNode == NULL) {
  324. return NULL;
  325. }
  326. /* Initialise all the CNode object
  327. just in case it came from the cache
  328. */
  329. pNode->SetData(pObject);
  330. pNode->SetNext(NULL);
  331. pNode->SetPrev(m_pLast);
  332. if (m_pLast == NULL) {
  333. m_pFirst = pNode;
  334. } else {
  335. m_pLast->SetNext(pNode);
  336. }
  337. /* Set the new last node pointer and also increment the number
  338. of list entries, the critical section is unlocked when we
  339. exit the function
  340. */
  341. m_pLast = pNode;
  342. ++m_Count;
  343. return (POSITION) pNode;
  344. } // AddTail(object)
  345. /* Add this object to the head end of our list
  346. Return the new head position.
  347. */
  348. POSITION CBaseList::AddHeadI(void *pObject)
  349. {
  350. CNode *pNode;
  351. // ASSERT(pObject); // NULL pointers in the list are allowed.
  352. /* If there is a node objects in the cache then use
  353. that otherwise we will have to create a new one */
  354. pNode = (CNode *) m_Cache.RemoveFromCache();
  355. if (pNode == NULL) {
  356. pNode = new CNode;
  357. }
  358. /* Check we have a valid object */
  359. if (pNode == NULL) {
  360. return NULL;
  361. }
  362. /* Initialise all the CNode object
  363. just in case it came from the cache
  364. */
  365. pNode->SetData(pObject);
  366. /* chain it in (set four pointers) */
  367. pNode->SetPrev(NULL);
  368. pNode->SetNext(m_pFirst);
  369. if (m_pFirst == NULL) {
  370. m_pLast = pNode;
  371. } else {
  372. m_pFirst->SetPrev(pNode);
  373. }
  374. m_pFirst = pNode;
  375. ++m_Count;
  376. return (POSITION) pNode;
  377. } // AddHead(object)
  378. /* Add all the elements in *pList to the tail of this list.
  379. Return TRUE if it all worked, FALSE if it didn't.
  380. If it fails some elements may have been added.
  381. */
  382. BOOL CBaseList::AddTail(CBaseList *pList)
  383. {
  384. /* lock the object before starting then enumerate
  385. each entry in the source list and add them one by one to
  386. our list (while still holding the object lock)
  387. Lock the other list too.
  388. */
  389. POSITION pos = pList->GetHeadPositionI();
  390. while (pos) {
  391. if (NULL == AddTailI(pList->GetNextI(pos))) {
  392. return FALSE;
  393. }
  394. }
  395. return TRUE;
  396. } // AddTail(list)
  397. /* Add all the elements in *pList to the head of this list.
  398. Return TRUE if it all worked, FALSE if it didn't.
  399. If it fails some elements may have been added.
  400. */
  401. BOOL CBaseList::AddHead(CBaseList *pList)
  402. {
  403. /* lock the object before starting then enumerate
  404. each entry in the source list and add them one by one to
  405. our list (while still holding the object lock)
  406. Lock the other list too.
  407. To avoid reversing the list, traverse it backwards.
  408. */
  409. POSITION pos;
  410. INTERNALREVERSETRAVERSELIST(*pList, pos) {
  411. if (NULL== AddHeadI(pList->GetI(pos))){
  412. return FALSE;
  413. }
  414. }
  415. return TRUE;
  416. } // AddHead(list)
  417. /* Add the object after position p
  418. p is still valid after the operation.
  419. AddAfter(NULL,x) adds x to the start - same as AddHead
  420. Return the position of the new object, NULL if it failed
  421. */
  422. POSITION CBaseList::AddAfterI(POSITION pos, void * pObj)
  423. {
  424. if (pos==NULL)
  425. return AddHeadI(pObj);
  426. /* As someone else might be furkling with the list -
  427. Lock the critical section before continuing
  428. */
  429. CNode *pAfter = (CNode *) pos;
  430. ASSERT(pAfter != NULL);
  431. if (pAfter==m_pLast)
  432. return AddTailI(pObj);
  433. /* set pnode to point to a new node, preferably from the cache */
  434. CNode *pNode = (CNode *) m_Cache.RemoveFromCache();
  435. if (pNode == NULL) {
  436. pNode = new CNode;
  437. }
  438. /* Check we have a valid object */
  439. if (pNode == NULL) {
  440. return NULL;
  441. }
  442. /* Initialise all the CNode object
  443. just in case it came from the cache
  444. */
  445. pNode->SetData(pObj);
  446. /* It is to be added to the middle of the list - there is a before
  447. and after node. Chain it after pAfter, before pBefore.
  448. */
  449. CNode * pBefore = pAfter->Next();
  450. ASSERT(pBefore != NULL);
  451. /* chain it in (set four pointers) */
  452. pNode->SetPrev(pAfter);
  453. pNode->SetNext(pBefore);
  454. pBefore->SetPrev(pNode);
  455. pAfter->SetNext(pNode);
  456. ++m_Count;
  457. return (POSITION) pNode;
  458. } // AddAfter(object)
  459. BOOL CBaseList::AddAfter(POSITION p, CBaseList *pList)
  460. {
  461. POSITION pos;
  462. INTERNALTRAVERSELIST(*pList, pos) {
  463. /* p follows along the elements being added */
  464. p = AddAfterI(p, pList->GetI(pos));
  465. if (p==NULL) return FALSE;
  466. }
  467. return TRUE;
  468. } // AddAfter(list)
  469. /* Mirror images:
  470. Add the element or list after position p.
  471. p is still valid after the operation.
  472. AddBefore(NULL,x) adds x to the end - same as AddTail
  473. */
  474. POSITION CBaseList::AddBeforeI(POSITION pos, void * pObj)
  475. {
  476. if (pos==NULL)
  477. return AddTailI(pObj);
  478. /* set pnode to point to a new node, preferably from the cache */
  479. CNode *pBefore = (CNode *) pos;
  480. ASSERT(pBefore != NULL);
  481. if (pBefore==m_pFirst)
  482. return AddHeadI(pObj);
  483. CNode * pNode = (CNode *) m_Cache.RemoveFromCache();
  484. if (pNode == NULL) {
  485. pNode = new CNode;
  486. }
  487. /* Check we have a valid object */
  488. if (pNode == NULL) {
  489. return NULL;
  490. }
  491. /* Initialise all the CNode object
  492. just in case it came from the cache
  493. */
  494. pNode->SetData(pObj);
  495. /* It is to be added to the middle of the list - there is a before
  496. and after node. Chain it after pAfter, before pBefore.
  497. */
  498. CNode * pAfter = pBefore->Prev();
  499. ASSERT(pAfter != NULL);
  500. /* chain it in (set four pointers) */
  501. pNode->SetPrev(pAfter);
  502. pNode->SetNext(pBefore);
  503. pBefore->SetPrev(pNode);
  504. pAfter->SetNext(pNode);
  505. ++m_Count;
  506. return (POSITION) pNode;
  507. } // Addbefore(object)
  508. BOOL CBaseList::AddBefore(POSITION p, CBaseList *pList)
  509. {
  510. POSITION pos;
  511. INTERNALREVERSETRAVERSELIST(*pList, pos) {
  512. /* p follows along the elements being added */
  513. p = AddBeforeI(p, pList->GetI(pos));
  514. if (p==NULL) return FALSE;
  515. }
  516. return TRUE;
  517. } // AddBefore(list)
  518. /* Split *this after position p in *this
  519. Retain as *this the tail portion of the original *this
  520. Add the head portion to the tail end of *pList
  521. Return TRUE if it all worked, FALSE if it didn't.
  522. e.g.
  523. foo->MoveToTail(foo->GetHeadPosition(), bar);
  524. moves one element from the head of foo to the tail of bar
  525. foo->MoveToTail(NULL, bar);
  526. is a no-op
  527. foo->MoveToTail(foo->GetTailPosition, bar);
  528. concatenates foo onto the end of bar and empties foo.
  529. A better, except excessively long name might be
  530. MoveElementsFromHeadThroughPositionToOtherTail
  531. */
  532. BOOL CBaseList::MoveToTail
  533. (POSITION pos, CBaseList *pList)
  534. {
  535. /* Algorithm:
  536. Note that the elements (including their order) in the concatenation
  537. of *pList to the head of *this is invariant.
  538. 1. Count elements to be moved
  539. 2. Join *pList onto the head of this to make one long chain
  540. 3. Set first/Last pointers in *this and *pList
  541. 4. Break the chain at the new place
  542. 5. Adjust counts
  543. 6. Set/Reset any events
  544. */
  545. if (pos==NULL) return TRUE; // no-op. Eliminates special cases later.
  546. /* Make cMove the number of nodes to move */
  547. CNode * p = (CNode *)pos;
  548. int cMove = 0; // number of nodes to move
  549. while(p!=NULL) {
  550. p = p->Prev();
  551. ++cMove;
  552. }
  553. /* Join the two chains together */
  554. if (pList->m_pLast!=NULL)
  555. pList->m_pLast->SetNext(m_pFirst);
  556. if (m_pFirst!=NULL)
  557. m_pFirst->SetPrev(pList->m_pLast);
  558. /* set first and last pointers */
  559. p = (CNode *)pos;
  560. if (pList->m_pFirst==NULL)
  561. pList->m_pFirst = m_pFirst;
  562. m_pFirst = p->Next();
  563. if (m_pFirst==NULL)
  564. m_pLast = NULL;
  565. pList->m_pLast = p;
  566. /* Break the chain after p to create the new pieces */
  567. if (m_pFirst!=NULL)
  568. m_pFirst->SetPrev(NULL);
  569. p->SetNext(NULL);
  570. /* Adjust the counts */
  571. m_Count -= cMove;
  572. pList->m_Count += cMove;
  573. return TRUE;
  574. } // MoveToTail
  575. /* Mirror image of MoveToTail:
  576. Split *this before position p in *this.
  577. Retain in *this the head portion of the original *this
  578. Add the tail portion to the start (i.e. head) of *pList
  579. Return TRUE if it all worked, FALSE if it didn't.
  580. e.g.
  581. foo->MoveToHead(foo->GetTailPosition(), bar);
  582. moves one element from the tail of foo to the head of bar
  583. foo->MoveToHead(NULL, bar);
  584. is a no-op
  585. foo->MoveToHead(foo->GetHeadPosition, bar);
  586. concatenates foo onto the start of bar and empties foo.
  587. */
  588. BOOL CBaseList::MoveToHead
  589. (POSITION pos, CBaseList *pList)
  590. {
  591. /* See the comments on the algorithm in MoveToTail */
  592. if (pos==NULL) return TRUE; // no-op. Eliminates special cases later.
  593. /* Make cMove the number of nodes to move */
  594. CNode * p = (CNode *)pos;
  595. int cMove = 0; // number of nodes to move
  596. while(p!=NULL) {
  597. p = p->Next();
  598. ++cMove;
  599. }
  600. /* Join the two chains together */
  601. if (pList->m_pFirst!=NULL)
  602. pList->m_pFirst->SetPrev(m_pLast);
  603. if (m_pLast!=NULL)
  604. m_pLast->SetNext(pList->m_pFirst);
  605. /* set first and last pointers */
  606. p = (CNode *)pos;
  607. if (pList->m_pLast==NULL)
  608. pList->m_pLast = m_pLast;
  609. m_pLast = p->Prev();
  610. if (m_pLast==NULL)
  611. m_pFirst = NULL;
  612. pList->m_pFirst = p;
  613. /* Break the chain after p to create the new pieces */
  614. if (m_pLast!=NULL)
  615. m_pLast->SetNext(NULL);
  616. p->SetPrev(NULL);
  617. /* Adjust the counts */
  618. m_Count -= cMove;
  619. pList->m_Count += cMove;
  620. return TRUE;
  621. } // MoveToHead
  622. /* Reverse the order of the [pointers to] objects in *this
  623. */
  624. void CBaseList::Reverse()
  625. {
  626. /* algorithm:
  627. The obvious booby trap is that you flip pointers around and lose
  628. addressability to the node that you are going to process next.
  629. The easy way to avoid this is do do one chain at a time.
  630. Run along the forward chain,
  631. For each node, set the reverse pointer to the one ahead of us.
  632. The reverse chain is now a copy of the old forward chain, including
  633. the NULL termination.
  634. Run along the reverse chain (i.e. old forward chain again)
  635. For each node set the forward pointer of the node ahead to point back
  636. to the one we're standing on.
  637. The first node needs special treatment,
  638. it's new forward pointer is NULL.
  639. Finally set the First/Last pointers
  640. */
  641. CNode * p;
  642. // Yes we COULD use a traverse, but it would look funny!
  643. p = m_pFirst;
  644. while (p!=NULL) {
  645. CNode * q;
  646. q = p->Next();
  647. p->SetNext(p->Prev());
  648. p->SetPrev(q);
  649. p = q;
  650. }
  651. p = m_pFirst;
  652. m_pFirst = m_pLast;
  653. m_pLast = p;
  654. #if 0 // old version
  655. if (m_pFirst==NULL) return; // empty list
  656. if (m_pFirst->Next()==NULL) return; // single node list
  657. /* run along forward chain */
  658. for ( p = m_pFirst
  659. ; p!=NULL
  660. ; p = p->Next()
  661. ){
  662. p->SetPrev(p->Next());
  663. }
  664. /* special case first element */
  665. m_pFirst->SetNext(NULL); // fix the old first element
  666. /* run along new reverse chain i.e. old forward chain again */
  667. for ( p = m_pFirst // start at the old first element
  668. ; p->Prev()!=NULL // while there's a node still to be set
  669. ; p = p->Prev() // work in the same direction as before
  670. ){
  671. p->Prev()->SetNext(p);
  672. }
  673. /* fix forward and reverse pointers
  674. - the triple XOR swap would work but all the casts look hideous */
  675. p = m_pFirst;
  676. m_pFirst = m_pLast;
  677. m_pLast = p;
  678. #endif
  679. } // Reverse