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.

298 lines
8.4 KiB

  1. /*----------------------------------------------------------------------------
  2. * File: RRCMQUEU.C
  3. * Product: RTP/RTCP implementation.
  4. * Description: Provides queue management function for RRCM.
  5. *
  6. * INTEL Corporation Proprietary Information
  7. * This listing is supplied under the terms of a license agreement with
  8. * Intel Corporation and may not be copied nor disclosed except in
  9. * accordance with the terms of that agreement.
  10. * Copyright (c) 1995 Intel Corporation.
  11. *--------------------------------------------------------------------------*/
  12. #include "rrcm.h"
  13. /*---------------------------------------------------------------------------
  14. / Global Variables
  15. /--------------------------------------------------------------------------*/
  16. /*---------------------------------------------------------------------------
  17. / External Variables
  18. /--------------------------------------------------------------------------*/
  19. /*---------------------------------------------------------------------------
  20. * Function : allocateLinkedList
  21. * Description: Allocates all the necessary memory resource and link the
  22. * cells to the link list.
  23. *
  24. * Input : *listPtr : Address of head pointer.
  25. * hHeap : Heap to allocate the data from.
  26. * *numCells : -> to the number of cells to allocate.
  27. * elementSize : Element size.
  28. * pCritSect : -> to critical section
  29. *
  30. * Return: TRUE = Error Code, no queues allocated and linked
  31. * FALSE = OK
  32. --------------------------------------------------------------------------*/
  33. DWORD allocateLinkedList (PLINK_LIST pList,
  34. HANDLE hHeap,
  35. DWORD *numCells,
  36. DWORD elementSize,
  37. CRITICAL_SECTION *pCritSect)
  38. {
  39. DWORD cellsAllocated = *numCells;
  40. PLINK_LIST pHead;
  41. PLINK_LIST pTmp;
  42. IN_OUT_STR ("RTCP: Enter allocateLinkedList()\n");
  43. // allocate first cell
  44. pHead = (PLINK_LIST)HeapAlloc (hHeap, HEAP_ZERO_MEMORY, elementSize);
  45. if (pHead == NULL)
  46. {
  47. RRCM_DBG_MSG ("RTCP: ERROR - Resource allocation failed", 0,
  48. __FILE__, __LINE__, DBG_CRITICAL);
  49. IN_OUT_STR ("RTCP: Exit allocateLinkedList()\n");
  50. return (RRCMError_RTCPResources);
  51. }
  52. // protect the pointers
  53. EnterCriticalSection (pCritSect);
  54. // initialize list tail pointer
  55. pList->prev = pHead;
  56. // update number of cells allocated
  57. cellsAllocated--;
  58. while (cellsAllocated)
  59. {
  60. cellsAllocated--;
  61. pHead->next = (PLINK_LIST)HeapAlloc (hHeap, HEAP_ZERO_MEMORY,
  62. elementSize);
  63. if (pHead->next == NULL)
  64. break;
  65. // save head pointer
  66. pTmp = pHead;
  67. // update head ptr
  68. pHead = pHead->next;
  69. pHead->prev = pTmp;
  70. }
  71. // set number of cells allocated
  72. *numCells -= cellsAllocated;
  73. // set head/tail pointers
  74. pList->next = pHead;
  75. // unprotect the pointers
  76. LeaveCriticalSection (pCritSect);
  77. IN_OUT_STR ("RTCP: Exit allocateLinkedList()\n");
  78. return (RRCM_NoError);
  79. }
  80. /*--------------------------------------------------------------------------
  81. ** Function : addToHeadOfList
  82. ** Description: Add a new cell to the specified queue. The queue acts as a
  83. ** FIFO (cells enqueud on the next pointer and dequeued by the
  84. ** starting address of the queue).
  85. **
  86. ** Input : pHead = Address of head pointer of queue.
  87. ** pNew = Cell address to be added to the linked list.
  88. ** pCritSect = -> to critical section object.
  89. **
  90. ** Return: None.
  91. --------------------------------------------------------------------------*/
  92. void addToHeadOfList (PLINK_LIST pHead,
  93. PLINK_LIST pNew,
  94. CRITICAL_SECTION *pCritSect)
  95. {
  96. ASSERT (pHead);
  97. ASSERT (pNew);
  98. IN_OUT_STR ("RTCP: Enter addToHeadOfList()\n");
  99. // safe access to pointers
  100. EnterCriticalSection (pCritSect);
  101. if (pHead->next == NULL)
  102. {
  103. // head is NULL for the first cell. Assign the address of
  104. // the free cell
  105. pHead->next = pHead->prev = pNew;
  106. pNew->next = pNew->prev = NULL;
  107. }
  108. else
  109. // head ptr points to something
  110. {
  111. pNew->prev = pHead->next;
  112. (pHead->next)->next = pNew;
  113. pNew->next = NULL;
  114. // update the head pointer now
  115. pHead->next = pNew;
  116. }
  117. // unlock pointer access
  118. LeaveCriticalSection (pCritSect);
  119. IN_OUT_STR ("RTCP: Exit addToHeadOfList()\n");
  120. }
  121. /*--------------------------------------------------------------------------
  122. ** Function : addToTailOfList
  123. ** Description: Add a new cell to the specified queue. The queue acts as a
  124. ** FIFO (cells enqueud on the next pointer and dequeued by the
  125. ** starting address of the queue).
  126. **
  127. ** Input : pTail = Address of tail pointer to enqueue in.
  128. ** pNew = New cell address to be added to the linked list.
  129. **
  130. ** Return: None.
  131. --------------------------------------------------------------------------*/
  132. void addToTailOfList (PLINK_LIST pTail,
  133. PLINK_LIST pNew,
  134. CRITICAL_SECTION *pCritSect)
  135. {
  136. ASSERT (pTail);
  137. ASSERT (pNew);
  138. IN_OUT_STR ("RTCP: Enter addToTailOfList()\n");
  139. // safe access to pointers
  140. EnterCriticalSection (pCritSect);
  141. if (pTail->prev == NULL)
  142. {
  143. // head is NULL for the first cell. Assign the address of
  144. // the free cell
  145. pTail->next = pTail->prev = pNew;
  146. pNew->next = pNew->prev = NULL;
  147. }
  148. else
  149. // tail ptr points to something
  150. {
  151. pNew->next = pTail->prev;
  152. (pTail->prev)->prev = pNew;
  153. pNew->prev = NULL;
  154. // update the parent tail pointer now
  155. pTail->prev = pNew;
  156. }
  157. // unlock pointer access
  158. LeaveCriticalSection (pCritSect);
  159. IN_OUT_STR ("RTCP: Exit addToTailOfList()\n");
  160. }
  161. /*--------------------------------------------------------------------------
  162. ** Function : removePcktFromHead
  163. ** Description: Remove a cell from front of the specified queue.
  164. **
  165. ** Input : pQueue: -> to the list to remove the packet from
  166. **
  167. ** Return: NULL ==> Empty queue.
  168. ** Buffer Address ==> OK, cell removed
  169. --------------------------------------------------------------------------*/
  170. PLINK_LIST removePcktFromHead (PLINK_LIST pQueue,
  171. CRITICAL_SECTION *pCritSect)
  172. {
  173. PLINK_LIST pReturnQ;
  174. IN_OUT_STR ("RTCP: Enter removePcktFromHead()\n");
  175. // safe access to pointers
  176. EnterCriticalSection (pCritSect);
  177. if ((pReturnQ = pQueue->next) != NULL)
  178. {
  179. // We have a buffer. If this is the last buffer in the queue,
  180. // mark it empty.
  181. if (pReturnQ->prev == NULL)
  182. {
  183. pQueue->prev = NULL;
  184. pQueue->next = NULL;
  185. }
  186. else
  187. {
  188. // Have the new head buffer point to NULL
  189. (pReturnQ->prev)->next = NULL;
  190. // Have the queue head point to the new head buffer
  191. pQueue->next = pReturnQ->prev;
  192. }
  193. }
  194. // unlock pointer access
  195. LeaveCriticalSection (pCritSect);
  196. IN_OUT_STR ("RTCP: Exit removePcktFromHead()\n");
  197. return (pReturnQ);
  198. }
  199. /*--------------------------------------------------------------------------
  200. ** Function : removePcktFromTail
  201. ** Description: Remove a cell from end of the specified queue.
  202. **
  203. ** Input : pQueue: -> to the list to remove the packet from
  204. **
  205. ** Return: NULL ==> Empty queue.
  206. ** Buffer Address ==> OK, cell removed
  207. --------------------------------------------------------------------------*/
  208. PLINK_LIST removePcktFromTail (PLINK_LIST pQueue,
  209. CRITICAL_SECTION *pCritSect)
  210. {
  211. PLINK_LIST pReturnQ;
  212. IN_OUT_STR ("RTCP: Enter removePcktFromTail()\n");
  213. // safe access to pointers
  214. EnterCriticalSection (pCritSect);
  215. if ((pReturnQ = pQueue->prev) != NULL)
  216. {
  217. // We have a buffer. If this is the last buffer in the queue,
  218. // mark it empty.
  219. if (pReturnQ->next == NULL)
  220. {
  221. pQueue->prev = NULL;
  222. pQueue->next = NULL;
  223. }
  224. else
  225. {
  226. // In any event, the new prev pointer is NULL: end of list
  227. (pReturnQ->next)->prev = NULL;
  228. // have the queue prev pointer point to the new 'last' element
  229. pQueue->prev = pReturnQ->next;
  230. }
  231. }
  232. // unlock pointer access
  233. LeaveCriticalSection (pCritSect);
  234. IN_OUT_STR ("RTCP: Enter removePcktFromTail()\n");
  235. return (pReturnQ);
  236. }
  237. // [EOF]