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.

290 lines
6.9 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // queue.c - queue functions
  24. ////
  25. #include "winlocal.h"
  26. #include "queue.h"
  27. #include "list.h"
  28. #include "mem.h"
  29. #include "trace.h"
  30. ////
  31. // private definitions
  32. ////
  33. // queue
  34. //
  35. typedef struct QUEUE
  36. {
  37. DWORD dwVersion;
  38. HINSTANCE hInst;
  39. HTASK hTask;
  40. HLIST hList;
  41. } QUEUE, FAR *LPQUEUE;
  42. // helper functions
  43. //
  44. static LPQUEUE QueueGetPtr(HQUEUE hQueue);
  45. static HQUEUE QueueGetHandle(LPQUEUE lpQueue);
  46. ////
  47. // public functions
  48. ////
  49. ////
  50. // queue constructor and destructor functions
  51. ////
  52. // QueueCreate - queue constructor
  53. // <dwVersion> (i) must be QUEUE_VERSION
  54. // <hInst> (i) instance handle of calling module
  55. // return new queue handle (NULL if error)
  56. //
  57. HQUEUE DLLEXPORT WINAPI QueueCreate(DWORD dwVersion, HINSTANCE hInst)
  58. {
  59. BOOL fSuccess = TRUE;
  60. LPQUEUE lpQueue = NULL;
  61. if (dwVersion != QUEUE_VERSION)
  62. fSuccess = TraceFALSE(NULL);
  63. else if (hInst == NULL)
  64. fSuccess = TraceFALSE(NULL);
  65. // memory is allocated such that the client app owns it
  66. //
  67. else if ((lpQueue = (LPQUEUE) MemAlloc(NULL, sizeof(QUEUE), 0)) == NULL)
  68. fSuccess = TraceFALSE(NULL);
  69. else if ((lpQueue->hList = ListCreate(LIST_VERSION, hInst)) == NULL)
  70. fSuccess = TraceFALSE(NULL);
  71. else
  72. {
  73. // initially the queue is empty
  74. //
  75. lpQueue->dwVersion = dwVersion;
  76. lpQueue->hInst = hInst;
  77. lpQueue->hTask = GetCurrentTask();
  78. }
  79. if (!fSuccess)
  80. {
  81. QueueDestroy(QueueGetHandle(lpQueue));
  82. lpQueue = NULL;
  83. }
  84. return fSuccess ? QueueGetHandle(lpQueue) : NULL;
  85. }
  86. // QueueDestroy - queue destructor
  87. // <hQueue> (i) handle returned from QueueCreate
  88. // return 0 if success
  89. //
  90. int DLLEXPORT WINAPI QueueDestroy(HQUEUE hQueue)
  91. {
  92. BOOL fSuccess = TRUE;
  93. LPQUEUE lpQueue;
  94. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  95. fSuccess = TraceFALSE(NULL);
  96. else if (ListDestroy(lpQueue->hList) != 0)
  97. fSuccess = TraceFALSE(NULL);
  98. else if ((lpQueue = MemFree(NULL, lpQueue)) != NULL)
  99. fSuccess = TraceFALSE(NULL);
  100. return fSuccess ? 0 : -1;
  101. }
  102. ////
  103. // queue status functions
  104. ////
  105. // QueueGetCount - return count of nodes in queue
  106. // <hQueue> (i) handle returned from QueueCreate
  107. // return node count (-1 if error)
  108. //
  109. long DLLEXPORT WINAPI QueueGetCount(HQUEUE hQueue)
  110. {
  111. BOOL fSuccess = TRUE;
  112. LPQUEUE lpQueue;
  113. long cNodes;
  114. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  115. fSuccess = TraceFALSE(NULL);
  116. else if ((cNodes = ListGetCount(lpQueue->hList)) < 0)
  117. fSuccess = TraceFALSE(NULL);
  118. return fSuccess ? cNodes : -1;
  119. }
  120. // QueueIsEmpty - return TRUE if queue has no nodes
  121. // <hQueue> (i) handle returned from QueueCreate
  122. // return TRUE or FALSE
  123. //
  124. BOOL DLLEXPORT WINAPI QueueIsEmpty(HQUEUE hQueue)
  125. {
  126. BOOL fSuccess = TRUE;
  127. LPQUEUE lpQueue;
  128. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  129. fSuccess = TraceFALSE(NULL);
  130. return fSuccess ? ListIsEmpty(lpQueue->hList) : TRUE;
  131. }
  132. ////
  133. // queue element insertion functions
  134. ////
  135. // QueueAddTail - add new node with data <elem> to end of queue
  136. // <hQueue> (i) handle returned from QueueCreate
  137. // <elem> (i) new data element
  138. // returns 0 if success
  139. //
  140. int DLLEXPORT WINAPI QueueAddTail(HQUEUE hQueue, QUEUEELEM elem)
  141. {
  142. BOOL fSuccess = TRUE;
  143. LPQUEUE lpQueue;
  144. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  145. fSuccess = TraceFALSE(NULL);
  146. else if (ListAddTail(lpQueue->hList, elem) == NULL)
  147. fSuccess = TraceFALSE(NULL);
  148. return fSuccess ? 0 : -1;
  149. }
  150. ////
  151. // queue element removal functions
  152. ////
  153. // QueueRemoveHead - remove node from head of queue
  154. // <hQueue> (i) handle returned from QueueCreate
  155. // returns removed data element (NULL of error or empty)
  156. //
  157. QUEUEELEM DLLEXPORT WINAPI QueueRemoveHead(HQUEUE hQueue)
  158. {
  159. BOOL fSuccess = TRUE;
  160. LPQUEUE lpQueue;
  161. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  162. fSuccess = TraceFALSE(NULL);
  163. else if (ListIsEmpty(lpQueue->hList))
  164. fSuccess = TraceFALSE(NULL);
  165. return fSuccess ? (QUEUEELEM) ListRemoveHead(lpQueue->hList) : NULL;
  166. }
  167. // QueueRemoveAll - remove all nodes from queue
  168. // <hQueue> (i) handle returned from QueueCreate
  169. // return 0 if success
  170. //
  171. int DLLEXPORT WINAPI QueueRemoveAll(HQUEUE hQueue)
  172. {
  173. BOOL fSuccess = TRUE;
  174. LPQUEUE lpQueue;
  175. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  176. fSuccess = TraceFALSE(NULL);
  177. else if (ListRemoveAll(lpQueue->hList) != 0)
  178. fSuccess = TraceFALSE(NULL);
  179. return fSuccess ? 0 : -1;
  180. }
  181. ////
  182. // queue element get value functions
  183. ////
  184. // QueuePeek - return node from head of queue, but leave it on queue
  185. // <hQueue> (i) handle returned from QueueCreate
  186. // returns data element (NULL if error or empty)
  187. //
  188. QUEUEELEM DLLEXPORT WINAPI QueuePeek(HQUEUE hQueue)
  189. {
  190. BOOL fSuccess = TRUE;
  191. LPQUEUE lpQueue;
  192. if ((lpQueue = QueueGetPtr(hQueue)) == NULL)
  193. fSuccess = TraceFALSE(NULL);
  194. else if (ListIsEmpty(lpQueue->hList))
  195. fSuccess = TraceFALSE(NULL);
  196. return fSuccess ? (QUEUEELEM) ListGetHead(lpQueue->hList) : NULL;
  197. }
  198. ////
  199. // private functions
  200. ////
  201. // QueueGetPtr - verify that queue handle is valid,
  202. // <hQueue> (i) handle returned from QueueCreate
  203. // return corresponding queue pointer (NULL if error)
  204. //
  205. static LPQUEUE QueueGetPtr(HQUEUE hQueue)
  206. {
  207. BOOL fSuccess = TRUE;
  208. LPQUEUE lpQueue;
  209. if ((lpQueue = (LPQUEUE) hQueue) == NULL)
  210. fSuccess = TraceFALSE(NULL);
  211. else if (IsBadWritePtr(lpQueue, sizeof(QUEUE)))
  212. fSuccess = TraceFALSE(NULL);
  213. #ifdef CHECKTASK
  214. // make sure current task owns the queue handle
  215. //
  216. else if (lpQueue->hTask != GetCurrentTask())
  217. fSuccess = TraceFALSE(NULL);
  218. #endif
  219. return fSuccess ? lpQueue : NULL;
  220. }
  221. // QueueGetHandle - verify that queue pointer is valid,
  222. // <lpQueue> (i) pointer to QUEUE struct
  223. // return corresponding queue handle (NULL if error)
  224. //
  225. static HQUEUE QueueGetHandle(LPQUEUE lpQueue)
  226. {
  227. BOOL fSuccess = TRUE;
  228. HQUEUE hQueue;
  229. if ((hQueue = (HQUEUE) lpQueue) == NULL)
  230. fSuccess = TraceFALSE(NULL);
  231. return fSuccess ? hQueue : NULL;
  232. }