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.

164 lines
3.6 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1993 - 1998
  3. Module Name:
  4. queue.c
  5. Abstract:
  6. Creates a simple Queue that works in Kernel Mode.
  7. Author:
  8. Robbie Harris (Hewlett-Packard) 22-May-1998
  9. Environment:
  10. Kernel mode
  11. Revision History :
  12. --*/
  13. #include "pch.h"
  14. void Queue_Create(Queue *pQueue, int size)
  15. {
  16. if( !pQueue ) {
  17. DD(NULL,DDE,"Queue_Create: Queue is Bad");
  18. return;
  19. }
  20. if (pQueue->theArray) {
  21. Queue_Delete(pQueue);
  22. }
  23. pQueue->theArray = (UCHAR *)ExAllocatePool(NonPagedPool, size);
  24. pQueue->max = size;
  25. pQueue->head = pQueue->tail = 0;
  26. }
  27. BOOLEAN Queue_Delete(Queue *pQueue)
  28. {
  29. if( !pQueue ) {
  30. return FALSE;
  31. }
  32. if( pQueue->theArray ) {
  33. ExFreePool(pQueue->theArray);
  34. pQueue->theArray = NULL;
  35. }
  36. pQueue->head = 0;
  37. pQueue->tail = 0;
  38. pQueue->max = 0;
  39. return TRUE;
  40. }
  41. BOOLEAN Queue_Dequeue(Queue *pQueue, PUCHAR data)
  42. {
  43. // Validity of pQueue is checked in Queue_IsEmpty proc.
  44. if( Queue_IsEmpty( pQueue ) ) {
  45. DD(NULL,DDE,"Queue_Dequeue: Queue is Empty");
  46. return FALSE;
  47. }
  48. *data = pQueue->theArray[pQueue->head++];
  49. return TRUE;
  50. }
  51. BOOLEAN Queue_Enqueue(Queue *pQueue, UCHAR data)
  52. {
  53. // Validity of pQueue is checked in Queue_IsFull proc.
  54. if( Queue_IsFull( pQueue ) ) {
  55. DD(NULL,DDE,"Queue_Enqueue: Queue is full. Data is lost");
  56. return FALSE;
  57. } else {
  58. pQueue->theArray[pQueue->tail++] = data;
  59. }
  60. return TRUE;
  61. }
  62. // Return TRUE if we were able to free some space in the Queue
  63. BOOLEAN Queue_GarbageCollect(Queue *pQueue)
  64. {
  65. int iListSize;
  66. int i;
  67. if (!pQueue)
  68. {
  69. DD(NULL,DDE,"Queue_GarbageCollect: Queue is Bad");
  70. return FALSE;
  71. }
  72. iListSize = pQueue->tail - pQueue->head;
  73. // Check to see if there is any free entries
  74. if (pQueue->head == 0 && pQueue->tail == pQueue->max)
  75. return FALSE;
  76. for (i = 0; i < iListSize; i++) {
  77. pQueue->theArray[i] = pQueue->theArray[pQueue->head+i];
  78. }
  79. pQueue->head = 0;
  80. pQueue->tail = iListSize;
  81. return TRUE;
  82. }
  83. //============================================================================
  84. // NAME: HPKQueue::IsEmpty()
  85. //
  86. // PARAMETERS: None
  87. //
  88. // RETURNS: True is Queue is empty or doesn't exist. Otherwise False.
  89. //
  90. //============================================================================
  91. BOOLEAN Queue_IsEmpty(Queue *pQueue)
  92. {
  93. if (!pQueue)
  94. {
  95. DD(NULL,DDE,"Queue_IsEmpty: Queue is Bad");
  96. return TRUE;
  97. }
  98. if (pQueue->theArray) {
  99. return (BOOLEAN)(pQueue->head == pQueue->tail);
  100. }
  101. DD(NULL,DDE,"Queue_IsEmpty: Queue->theArray is Bad");
  102. return TRUE;
  103. }
  104. //============================================================================
  105. // NAME: HPKQueue::IsFull()
  106. //
  107. // PARAMETERS: None
  108. //
  109. // RETURNS: True is Queue is full or doesn't exist. Otherwise False.
  110. //
  111. //============================================================================
  112. BOOLEAN Queue_IsFull(Queue *pQueue)
  113. {
  114. if( !pQueue ) {
  115. DD(NULL,DDE,"Queue_IsFull: Queue is Bad");
  116. return TRUE;
  117. }
  118. if( pQueue->theArray ) {
  119. if( pQueue->tail == pQueue->max ) {
  120. return !Queue_GarbageCollect(pQueue);
  121. } else {
  122. return FALSE;
  123. }
  124. }
  125. DD(NULL,DDE,"Queue_IsFull: Queue->theArray is Bad");
  126. return TRUE;
  127. }