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.

343 lines
8.3 KiB

  1. /***************************************************************************
  2. *
  3. * File: queue.c
  4. *
  5. * INTEL Corporation Proprietary Information
  6. * Copyright (c) 1996 Intel Corporation.
  7. *
  8. * This listing is supplied under the terms of a license agreement
  9. * with INTEL Corporation and may not be used, copied, nor disclosed
  10. * except in accordance with the terms of that agreement.
  11. *
  12. ***************************************************************************
  13. *
  14. * $Workfile: queue.c $
  15. * $Revision: 1.8 $
  16. * $Modtime: 13 Dec 1996 11:48:16 $
  17. * $Log: S:\sturgeon\src\h245ws\vcs\queue.c_v $
  18. *
  19. * Rev 1.8 13 Dec 1996 12:13:12 SBELL1
  20. * moved ifdef _cplusplus to after includes
  21. *
  22. * Rev 1.7 May 28 1996 10:39:00 plantz
  23. * Change QFree to not free objects on the queue; instead it insists that
  24. * the queue be empty.
  25. *
  26. * Rev 1.6 21 May 1996 16:21:36 EHOWARDX
  27. * Added DeleteCriticalSection to QFree().
  28. *
  29. * Rev 1.5 Apr 24 1996 16:18:58 plantz
  30. * Removed include winsock2.h and incommon.h
  31. *
  32. * Rev 1.3.1.0 Apr 24 1996 16:16:42 plantz
  33. * Removed include winsock2.h and callcont.h
  34. *
  35. * Rev 1.3 01 Apr 1996 14:53:28 EHOWARDX
  36. * Changed pQUEUE to PQUEUE.
  37. *
  38. * Rev 1.1 09 Mar 1996 21:12:34 EHOWARDX
  39. * Fixes as result of testing.
  40. *
  41. * Rev 1.0 08 Mar 1996 20:22:38 unknown
  42. * Initial revision.
  43. *
  44. ***************************************************************************/
  45. #ifndef STRICT
  46. #define STRICT
  47. #endif // not defined STRICT
  48. #pragma warning ( disable : 4115 4201 4214 4514 )
  49. #undef _WIN32_WINNT // override bogus platform definition in our common build environment
  50. #include "precomp.h"
  51. #include "queue.h"
  52. #include "linkapi.h"
  53. #include "h245ws.h"
  54. #if defined(__cplusplus)
  55. extern "C"
  56. {
  57. #endif // (__cplusplus)
  58. /*-*-------------------------------------------------------------------------
  59. Function Name:
  60. QCreate
  61. Syntax:
  62. PQUEUE QCreate(void);
  63. Parameters:
  64. None.
  65. Summary:
  66. Allocates and initializes a new queue.
  67. Returns:
  68. NULL - Allocation of memory for new queue failed.
  69. Otherwise - Address of new queue created.
  70. -------------------------------------------------------------------------*-*/
  71. PQUEUE QCreate(void)
  72. {
  73. register PQUEUE pQueue; /* pointer to the new queue */
  74. /* Allocate a new queue */
  75. pQueue = (PQUEUE)MemAlloc(sizeof(QUEUE));
  76. if (pQueue != NULL)
  77. {
  78. /* Initialize the new queue */
  79. pQueue->nHead = pQueue->nTail = Q_NULL;
  80. InitializeCriticalSection(&pQueue->CriticalSection);
  81. }
  82. return pQueue;
  83. } /* QCreate */
  84. /*-*-------------------------------------------------------------------------
  85. Function Name:
  86. QFree
  87. Syntax:
  88. void QFree(PQUEUE pQueue);
  89. Parameters:
  90. pQueue -pointer to the queue to free
  91. Summary:
  92. Deallocates a queue that was allocated by QCreate.
  93. -------------------------------------------------------------------------*-*/
  94. void QFree(PQUEUE pQueue)
  95. {
  96. /* The queue must be empty before it is freed. */
  97. HWSASSERT(pQueue->nHead == Q_NULL);
  98. /* Free the queue. */
  99. DeleteCriticalSection(&pQueue->CriticalSection);
  100. MemFree(pQueue);
  101. } /* QFree */
  102. /*
  103. * NAME
  104. * QRemove - remove object from head of queue
  105. *
  106. * ARGUMENTS
  107. * pQueue Pointer to queue
  108. *
  109. * RETURN VALUE
  110. * Pointer to object dequeued or NULL of queue empty
  111. */
  112. /*-*-------------------------------------------------------------------------
  113. Function Name:
  114. QRemove
  115. Syntax:
  116. LPVOID QRemove(PQUEUE pQueue);
  117. Parameters:
  118. pQueue - Pointer to queue.
  119. Summary:
  120. Removes and returns object from head of queue.
  121. Returns:
  122. NULL - Queue was empty.
  123. Otherwise - Address of object dequeued.
  124. -------------------------------------------------------------------------*-*/
  125. LPVOID QRemove(PQUEUE pQueue)
  126. {
  127. register LPVOID pObject; /* pointer to the object to remove */
  128. EnterCriticalSection(&pQueue->CriticalSection);
  129. if (pQueue->nHead == Q_NULL)
  130. {
  131. /* If the queue is empty, we will return NULL */
  132. pObject = NULL;
  133. }
  134. else
  135. {
  136. /* Get the pointer, NULL it in the apObjects array. */
  137. pObject = pQueue->apObjects[pQueue->nHead];
  138. pQueue->apObjects[pQueue->nHead] = NULL;
  139. /* Check to see if we've just emptied the queue; if so, set */
  140. /* the nHead and nTail indices to Q_NULL. If not, set the nHead */
  141. /* index to the right value. */
  142. if (pQueue->nHead == pQueue->nTail)
  143. {
  144. pQueue->nHead = pQueue->nTail = Q_NULL;
  145. }
  146. else
  147. {
  148. pQueue->nHead = (pQueue->nHead + 1) % MAX_QUEUE_SIZE;
  149. }
  150. }
  151. LeaveCriticalSection(&pQueue->CriticalSection);
  152. return pObject;
  153. } /* QRemove */
  154. /*-*-------------------------------------------------------------------------
  155. Function Name:
  156. QInsert
  157. Syntax:
  158. BOOL QInsert(PQUEUE pQueue, LPVOID pObject);
  159. Parameters:
  160. pQueue - Pointer to queue to insert object into.
  161. pObject - Pointer to object to insert into queue.
  162. Summary:
  163. Inserts an object at tail of queue.
  164. Returns:
  165. TRUE - Object successfully added to queue.
  166. FALSE - Queue full; object could not be added.
  167. -------------------------------------------------------------------------*-*/
  168. BOOL QInsert(PQUEUE pQueue, LPVOID pObject)
  169. {
  170. register int iTemp;
  171. EnterCriticalSection(&pQueue->CriticalSection);
  172. /* If the queue is full, set the return value to FALSE and do */
  173. /* nothing; if not, update the indices appropriately and set the */
  174. /* return value to TRUE. */
  175. if (pQueue->nHead == Q_NULL)
  176. {
  177. /* Queue is empty */
  178. pQueue->apObjects[0] = pObject;
  179. pQueue->nHead = pQueue->nTail = 0;
  180. iTemp = TRUE;
  181. }
  182. else
  183. {
  184. iTemp = (pQueue->nTail + 1) % MAX_QUEUE_SIZE;
  185. if (iTemp == pQueue->nHead)
  186. {
  187. /* Queue is full */
  188. iTemp = FALSE;
  189. }
  190. else
  191. {
  192. pQueue->apObjects[iTemp] = pObject;
  193. pQueue->nTail = iTemp;
  194. iTemp = TRUE;
  195. }
  196. }
  197. LeaveCriticalSection(&pQueue->CriticalSection);
  198. return (BOOL) iTemp;
  199. }
  200. /*-*-------------------------------------------------------------------------
  201. Function Name:
  202. QInsertAtHead
  203. Syntax:
  204. BOOL QInsertAtHead(PQUEUE pQueue, LPVOID pObject);
  205. Parameters:
  206. pQueue - Pointer to queue to insert object into.
  207. pObject - Pointer to object to insert into queue.
  208. Summary:
  209. Inserts an object at head of queue.
  210. Returns:
  211. TRUE - Object successfully added to queue.
  212. FALSE - Queue full; object could not be added.
  213. -------------------------------------------------------------------------*-*/
  214. BOOL QInsertAtHead(PQUEUE pQueue, LPVOID pObject)
  215. {
  216. register int iTemp;
  217. EnterCriticalSection(&pQueue->CriticalSection);
  218. if (pQueue->nHead == Q_NULL)
  219. {
  220. /* Queue is empty */
  221. pQueue->apObjects[0] = pObject;
  222. pQueue->nHead = pQueue->nTail = 0;
  223. iTemp = TRUE;
  224. }
  225. else
  226. {
  227. iTemp = (pQueue->nHead + (MAX_QUEUE_SIZE - 1)) % MAX_QUEUE_SIZE;
  228. if (iTemp == pQueue->nTail)
  229. {
  230. /* Queue is full */
  231. iTemp = FALSE;
  232. }
  233. else
  234. {
  235. pQueue->apObjects[iTemp] = pObject;
  236. pQueue->nHead = iTemp;
  237. iTemp = TRUE;
  238. }
  239. }
  240. LeaveCriticalSection(&pQueue->CriticalSection);
  241. return (BOOL) iTemp;
  242. } /* QInsertAtHead */
  243. /*-*-------------------------------------------------------------------------
  244. Function Name:
  245. IsQEmpty
  246. Syntax:
  247. BOOL IsQEmpty(PQUEUE pQueue);
  248. Parameters:
  249. pQueue - Pointer to queue to check.
  250. Summary:
  251. Checks if a queue is empty.
  252. Returns:
  253. TRUE - Queue is empty.
  254. FALSE - Queue contains at least one object.
  255. -------------------------------------------------------------------------*-*/
  256. BOOL IsQEmpty(PQUEUE pQueue)
  257. {
  258. return (pQueue->nHead == Q_NULL ? TRUE : FALSE);
  259. } /* IsQEmpty */
  260. #if defined(__cplusplus)
  261. }
  262. #endif // (__cplusplus)