queue.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef QUEUE_H_DEFINED
  35. #define QUEUE_H_DEFINED
  36. #include <string.h>
  37. #include "assert.h"
  38. struct queue {
  39. int head;
  40. int tail;
  41. int used;
  42. int usedhw;
  43. int size;
  44. void *items;
  45. int size_per_item;
  46. int iterator;
  47. };
  48. static inline int queue_init (struct queue *queue, int queue_items, int size_per_item) {
  49. queue->head = 0;
  50. queue->tail = queue_items - 1;
  51. queue->used = 0;
  52. queue->usedhw = 0;
  53. queue->size = queue_items;
  54. queue->size_per_item = size_per_item;
  55. queue->items = malloc (queue_items * size_per_item);
  56. if (queue->items == 0) {
  57. return (-ENOMEM);
  58. }
  59. memset (queue->items, 0, queue_items * size_per_item);
  60. return (0);
  61. }
  62. static inline int queue_reinit (struct queue *queue)
  63. {
  64. queue->head = 0;
  65. queue->tail = queue->size - 1;
  66. queue->used = 0;
  67. queue->usedhw = 0;
  68. memset (queue->items, 0, queue->size * queue->size_per_item);
  69. return (0);
  70. }
  71. static inline void queue_free (struct queue *queue) {
  72. free (queue->items);
  73. }
  74. static inline int queue_is_full (struct queue *queue) {
  75. return (queue->size - 1 == queue->used);
  76. }
  77. static inline int queue_is_empty (struct queue *queue) {
  78. return (queue->used == 0);
  79. }
  80. static inline void queue_item_add (struct queue *queue, void *item)
  81. {
  82. char *queue_item;
  83. int queue_position;
  84. queue_position = queue->head;
  85. queue_item = queue->items;
  86. queue_item += queue_position * queue->size_per_item;
  87. memcpy (queue_item, item, queue->size_per_item);
  88. assert (queue->tail != queue->head);
  89. queue->head = (queue->head + 1) % queue->size;
  90. queue->used++;
  91. if (queue->used > queue->usedhw) {
  92. queue->usedhw = queue->used;
  93. }
  94. }
  95. static inline void *queue_item_get (struct queue *queue)
  96. {
  97. char *queue_item;
  98. int queue_position;
  99. queue_position = (queue->tail + 1) % queue->size;
  100. queue_item = queue->items;
  101. queue_item += queue_position * queue->size_per_item;
  102. return ((void *)queue_item);
  103. }
  104. static inline void queue_item_remove (struct queue *queue) {
  105. queue->tail = (queue->tail + 1) % queue->size;
  106. assert (queue->tail != queue->head);
  107. queue->used--;
  108. }
  109. static inline void queue_items_remove (struct queue *queue, int rel_count)
  110. {
  111. queue->tail = (queue->tail + rel_count) % queue->size;
  112. assert (queue->tail != queue->head);
  113. queue->used -= rel_count;
  114. }
  115. static inline void queue_item_iterator_init (struct queue *queue)
  116. {
  117. queue->iterator = (queue->tail + 1) % queue->size;
  118. }
  119. static inline void *queue_item_iterator_get (struct queue *queue)
  120. {
  121. char *queue_item;
  122. int queue_position;
  123. queue_position = (queue->iterator) % queue->size;
  124. if (queue->iterator == queue->head) {
  125. return (0);
  126. }
  127. queue_item = queue->items;
  128. queue_item += queue_position * queue->size_per_item;
  129. return ((void *)queue_item);
  130. }
  131. static inline int queue_item_iterator_next (struct queue *queue)
  132. {
  133. queue->iterator = (queue->iterator + 1) % queue->size;
  134. return (queue->iterator == queue->head);
  135. }
  136. static inline void queue_avail (struct queue *queue, int *avail)
  137. {
  138. *avail = queue->size - queue->used - 1;
  139. assert (*avail >= 0);
  140. }
  141. #endif /* QUEUE_H_DEFINED */