queue.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "assert.h"
  37. struct queue {
  38. int head;
  39. int tail;
  40. int used;
  41. int usedhw;
  42. int size;
  43. void *items;
  44. int size_per_item;
  45. int iterator;
  46. };
  47. static inline int queue_init (struct queue *queue, int queue_items, int size_per_item) {
  48. queue->head = 0;
  49. queue->tail = queue_items - 1;
  50. queue->used = 0;
  51. queue->usedhw = 0;
  52. queue->size = queue_items;
  53. queue->size_per_item = size_per_item;
  54. queue->items = malloc (queue_items * size_per_item);
  55. if (queue->items == 0) {
  56. return (-ENOMEM);
  57. }
  58. memset (queue->items, 0, queue_items * size_per_item);
  59. return (0);
  60. }
  61. static inline int queue_reinit (struct queue *queue)
  62. {
  63. queue->head = 0;
  64. queue->tail = queue->size - 1;
  65. queue->used = 0;
  66. queue->usedhw = 0;
  67. memset (queue->items, 0, queue->size * queue->size_per_item);
  68. return (0);
  69. }
  70. static inline void queue_free (struct queue *queue) {
  71. free (queue->items);
  72. }
  73. static inline int queue_is_full (struct queue *queue) {
  74. return (queue->size - 1 == queue->used);
  75. }
  76. static inline int queue_is_empty (struct queue *queue) {
  77. return (queue->used == 0);
  78. }
  79. static inline void queue_item_add (struct queue *queue, void *item)
  80. {
  81. char *queue_item;
  82. int queue_position;
  83. queue_position = queue->head;
  84. queue_item = queue->items;
  85. queue_item += queue_position * queue->size_per_item;
  86. memcpy (queue_item, item, queue->size_per_item);
  87. assert (queue->tail != queue->head);
  88. queue->head = (queue->head + 1) % queue->size;
  89. queue->used++;
  90. if (queue->used > queue->usedhw) {
  91. queue->usedhw = queue->used;
  92. }
  93. }
  94. static inline void *queue_item_get (struct queue *queue)
  95. {
  96. char *queue_item;
  97. int queue_position;
  98. queue_position = (queue->tail + 1) % queue->size;
  99. queue_item = queue->items;
  100. queue_item += queue_position * queue->size_per_item;
  101. return ((void *)queue_item);
  102. }
  103. static inline void queue_item_remove (struct queue *queue) {
  104. queue->tail = (queue->tail + 1) % queue->size;
  105. assert (queue->tail != queue->head);
  106. queue->used--;
  107. }
  108. static inline void queue_items_remove (struct queue *queue, int rel_count)
  109. {
  110. queue->tail = (queue->tail + rel_count) % queue->size;
  111. assert (queue->tail != queue->head);
  112. queue->used -= rel_count;
  113. }
  114. static inline void queue_item_iterator_init (struct queue *queue)
  115. {
  116. queue->iterator = (queue->tail + 1) % queue->size;
  117. }
  118. static inline void *queue_item_iterator_get (struct queue *queue)
  119. {
  120. char *queue_item;
  121. int queue_position;
  122. queue_position = (queue->iterator) % queue->size;
  123. if (queue->iterator == queue->head) {
  124. return (0);
  125. }
  126. queue_item = queue->items;
  127. queue_item += queue_position * queue->size_per_item;
  128. return ((void *)queue_item);
  129. }
  130. static inline int queue_item_iterator_next (struct queue *queue)
  131. {
  132. queue->iterator = (queue->iterator + 1) % queue->size;
  133. return (queue->iterator == queue->head);
  134. }
  135. #endif /* QUEUE_H_DEFINED */