home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / dmgq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  5.2 KB  |  197 lines

  1. /****************************** Module Header ******************************\
  2. * Module Name: DMGQ.C
  3. *
  4. * DDE Manager queue control functions.
  5. *
  6. * Created: 9/1/89 Sanford Staab
  7. *
  8. * This is a general queue manager - yes another one!
  9. * Queues are each allocated within their own segment and have a
  10. * QST structure associated with that heap.  Each queue item
  11. * is allocated within the heap segment.  The offset of the items
  12. * address combined with an instance count is used as the item ID.
  13. * This is both unique and allows for instant location of an item.
  14. * New items are added to the head of the queue which is a doubly linked
  15. * list.  The next links point to more recent entries, the prev pointers
  16. * to older entries.  The next of the head is the tail.  The prev of the
  17. * tail is the head.  All pointers are far.
  18. * Queue Data may be of any structure type that begins identical to
  19. * a QUEUEITEM structure.  Functions that require an cbItem perameter
  20. * should be given the size of the specialized queue item structure.
  21. *
  22. * Copyright (c) 1988, 1989  Microsoft Corporation
  23. \***************************************************************************/
  24.  
  25. #include "ddemlp.h"
  26.  
  27.  
  28. /***************************** Private Function ****************************\
  29. *
  30. * Creates a Queue for items of cbItem.
  31. * Returns NULL on error.
  32. *
  33. *
  34. * History:
  35. *   Created     9/1/89    Sanfords
  36. \***************************************************************************/
  37. PQST CreateQ(cbItem)
  38. USHORT cbItem;
  39. {
  40.     QST cq;
  41.     PQST pQ;
  42.     
  43.     cq.cItems = 0;
  44.     cq.instLast = 0;
  45.     cq.cbItem = cbItem;
  46.     cq.pqiHead = NULL;
  47.     if (!(cq.hheap = MyCreateHeap(0, sizeof(QST) + cbItem << 3,
  48.             cbItem << 3, cbItem, cbItem, HEAPFLAGS)))
  49.         return(NULL);
  50.     if (!(pQ = (PQST)FarAllocMem(cq.hheap, sizeof(QST)))) {
  51.         MyDestroyHeap(cq.hheap);
  52.         return(0);
  53.     }
  54.     *pQ = cq;
  55.     return(pQ);
  56. }
  57.  
  58.  
  59.  
  60. /***************************** Private Function ****************************\
  61. *
  62. *
  63. * History:
  64. *   Created     9/1/89    Sanfords
  65. \***************************************************************************/
  66. BOOL DestroyQ(pQ)
  67. PQST pQ;
  68. {
  69.     if (pQ)
  70.         MyDestroyHeap(pQ->hheap);
  71.     return(TRUE);
  72. }
  73.  
  74.  
  75.  
  76. /***************************** Private Function ****************************\
  77. *
  78. * returns a long pointer to the queue item data created.  The new item
  79. * is added to the head of the queue.  The queue's cbItem specified at
  80. * creation is used for allocation.
  81. *
  82. *
  83. * History:
  84. *   Created     9/1/89    Sanfords
  85. \***************************************************************************/
  86. PQUEUEITEM Addqi(pQ)
  87. PQST pQ;
  88. {
  89.     PQUEUEITEM pqi;
  90.  
  91.     if ((pqi = (PQUEUEITEM)FarAllocMem(pQ->hheap, pQ->cbItem)) == NULL) {
  92.         if (pQ->cItems == 0)
  93.             return(0);
  94.         /*
  95.          * remove the oldest item to make room for the new.
  96.          */
  97.         pqi = pQ->pqiHead->next;
  98.         SemEnter();
  99.         pqi->prev->next = pqi->next;
  100.         pqi->next->prev = pqi->prev;
  101.         SemLeave();
  102.     }
  103.  
  104.     SemEnter();
  105.     if (pQ->cItems == 0) {
  106.         pQ->pqiHead = pqi->prev = pqi->next = pqi;
  107.     } else {
  108.         pqi->prev = pQ->pqiHead;
  109.         pqi->next = pQ->pqiHead->next;
  110.         pQ->pqiHead->next->prev = pqi;
  111.         pQ->pqiHead->next = pqi;
  112.         pQ->pqiHead = pqi;
  113.     }
  114.     SemLeave();
  115.     pQ->cItems++;
  116.     pqi->inst = ++pQ->instLast;
  117.     return(pqi);
  118. }
  119.  
  120.  
  121.  
  122.  
  123. /***************************** Private Function ****************************\
  124. *
  125. *  The id given is an external LONG id, not an item instance number.
  126. *  If id is QID_NEWEST, the head item is deleted.
  127. *  If id is QID_OLDEST, the tail item is deleted.
  128. *
  129. *
  130. * History:
  131. *   Created     9/1/89    Sanfords
  132. \***************************************************************************/
  133. void Deleteqi(pQ, id)
  134. PQST pQ;
  135. ULONG id;
  136. {
  137.     PQUEUEITEM pqi;
  138.  
  139.     SemEnter();
  140.     pqi = Findqi(pQ, id);
  141.     if (pqi == NULL) {
  142.         SemLeave();
  143.         return;
  144.     }
  145.     pqi->prev->next = pqi->next;
  146.     pqi->next->prev = pqi->prev;
  147.     if (pqi == pQ->pqiHead)
  148.         pQ->pqiHead = pqi->prev;
  149.     if (!(--pQ->cItems))
  150.         pQ->pqiHead = NULL;
  151.     FarFreeMem(pQ->hheap, pqi, pQ->cbItem);
  152.     SemLeave();
  153. }
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. /***************************** Private Function ****************************\
  161. *
  162. *  The id given is an external LONG id, not an item instance number.
  163. *
  164. *  if id == QID_NEWEST, returns the head queue data item.
  165. *  if id == QID_OLDEST, returns the tail queue data item.
  166. *  if the id is not found or the queue is empty, NULL is returned.
  167. *  if found, pqi is returned.
  168. *
  169. *
  170. * History:
  171. *   Created     9/1/89    Sanfords
  172. \***************************************************************************/
  173. PQUEUEITEM Findqi(pQ, id)
  174. PQST pQ;
  175. ULONG id;
  176. {
  177.     PQUEUEITEM pqi;
  178.  
  179.     SemCheckIn();
  180.     if (pQ == NULL || pQ->pqiHead == NULL)
  181.         return(NULL);
  182.         
  183.     if (id == QID_OLDEST) 
  184.         return(pQ->pqiHead->next);
  185.         
  186.     if (id == QID_NEWEST) 
  187.         return(pQ->pqiHead);
  188.         
  189.     if (id) {
  190.         pqi = PFROMID(pQ, id);
  191.         if (pqi->inst == HIUSHORT(id)) {
  192.             return(pqi);
  193.         }
  194.         return(NULL);
  195.     }
  196. }
  197.