sq.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2003-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 SORTQUEUE_H_DEFINED
  35. #define SORTQUEUE_H_DEFINED
  36. #include "errno.h"
  37. struct sq {
  38. int head;
  39. int size;
  40. void *items;
  41. unsigned char *items_inuse;
  42. int size_per_item;
  43. int head_seqid;
  44. int item_count;
  45. int pos_max;
  46. };
  47. static inline int sq_init (
  48. struct sq *sq,
  49. int item_count,
  50. int size_per_item,
  51. int head_seqid)
  52. {
  53. sq->head = 0;
  54. sq->size = item_count;
  55. sq->size_per_item = size_per_item;
  56. sq->head_seqid = head_seqid;
  57. sq->item_count = item_count;
  58. sq->pos_max = 0;
  59. sq->items = (void *)malloc (item_count * size_per_item);
  60. if (sq->items == 0) {
  61. return (-ENOMEM);
  62. }
  63. memset (sq->items, 0, item_count * size_per_item);
  64. sq->items_inuse = (void *)malloc (item_count * sizeof (char));
  65. memset (sq->items_inuse, 0, item_count * sizeof (char));
  66. return (0);
  67. }
  68. static inline void sq_reinit (struct sq *sq, int head_seqid)
  69. {
  70. sq->head = 0;
  71. sq->head_seqid = head_seqid;
  72. sq->pos_max = 0;
  73. memset (sq->items, 0, sq->item_count * sq->size_per_item);
  74. memset (sq->items_inuse, 0, sq->item_count * sizeof (char));
  75. }
  76. static inline void sq_assert (struct sq *sq, int pos)
  77. {
  78. int i;
  79. // printf ("Instrument[%d] Asserting from %d to %d\n",
  80. // pos, sq->pos_max, sq->size);
  81. for (i = sq->pos_max + 1; i < sq->size; i++) {
  82. assert (sq->items_inuse[i] == 0);
  83. }
  84. }
  85. static inline void sq_copy (struct sq *sq_dest, struct sq *sq_src)
  86. {
  87. sq_assert (sq_src, 20);
  88. sq_dest->head = sq_src->head;
  89. sq_dest->size = sq_src->item_count;
  90. sq_dest->size_per_item = sq_src->size_per_item;
  91. sq_dest->head_seqid = sq_src->head_seqid;
  92. sq_dest->item_count = sq_src->item_count;
  93. sq_dest->pos_max = sq_src->pos_max;
  94. memcpy (sq_dest->items, sq_src->items,
  95. sq_src->item_count * sq_src->size_per_item);
  96. memcpy (sq_dest->items_inuse, sq_src->items_inuse,
  97. sq_src->item_count * sizeof (char));
  98. }
  99. static inline void sq_free (struct sq *sq) {
  100. free (sq->items);
  101. free (sq->items_inuse);
  102. }
  103. static inline int sq_item_add (
  104. struct sq *sq,
  105. void *item,
  106. int seqid)
  107. {
  108. char *sq_item;
  109. int sq_position;
  110. if (seqid - sq->head_seqid >= sq->size) {
  111. return E2BIG;
  112. }
  113. sq_position = (sq->head + seqid - sq->head_seqid) % sq->size;
  114. if (sq_position > sq->pos_max) {
  115. sq->pos_max = sq_position;
  116. }
  117. assert (sq_position >= 0);
  118. //printf ("item add position %d seqid %d head seqid %d\n", sq_position, seqid, sq->head_seqid);
  119. sq_item = sq->items;
  120. sq_item += sq_position * sq->size_per_item;
  121. assert(sq->items_inuse[sq_position] == 0);
  122. memcpy (sq_item, item, sq->size_per_item);
  123. sq->items_inuse[sq_position] = 1;
  124. return (0);
  125. }
  126. static inline int sq_item_inuse (
  127. struct sq *sq,
  128. int seq_id) {
  129. int sq_position;
  130. /*
  131. * We need to say that the seqid is in use if it shouldn't
  132. * be here in the first place.
  133. * To keep old messages from being inserted.
  134. */
  135. #ifdef COMPILE_OUT
  136. if (seq_id < sq->head_seqid) {
  137. fprintf(stderr, "sq_item_inuse: seqid %d, head %d\n",
  138. seq_id, sq->head_seqid);
  139. return 1;
  140. }
  141. #endif
  142. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  143. //printf ("in use %d\n", sq_position);
  144. return (sq->items_inuse[sq_position]);
  145. }
  146. static inline int sq_size_get (
  147. struct sq *sq)
  148. {
  149. return sq->size;
  150. }
  151. static inline int sq_item_get (
  152. struct sq *sq,
  153. int seq_id,
  154. void **sq_item_out)
  155. {
  156. char *sq_item;
  157. int sq_position;
  158. if (seq_id == -1) {
  159. return (ENOENT);
  160. }
  161. assert (seq_id < (sq->head_seqid + sq->size));
  162. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  163. //printf ("ITEMGET %d %d %d %d\n", sq_position, sq->head, sq->head_seqid, seq_id);
  164. assert (sq_position >= 0);
  165. //printf ("itme get in use %d\n", sq_position);
  166. if (sq->items_inuse[sq_position] == 0) {
  167. //printf ("not in use %d\n", sq_position);
  168. return (ENOENT);
  169. }
  170. sq_item = sq->items;
  171. sq_item += sq_position * sq->size_per_item;
  172. *sq_item_out = sq_item;
  173. return (0);
  174. }
  175. static inline void sq_items_release (struct sq *sq, int seqid)
  176. {
  177. int oldhead;
  178. if (seqid < sq->head_seqid) {
  179. return;
  180. }
  181. oldhead = sq->head;
  182. sq->head = (sq->head + seqid - sq->head_seqid + 1) % sq->size;
  183. if ((oldhead + seqid - sq->head_seqid + 1) > sq->size) {
  184. // printf ("releasing %d for %d\n", oldhead, sq->size - oldhead);
  185. // printf ("releasing %d for %d\n", 0, sq->head);
  186. memset (&sq->items_inuse[oldhead], 0, sq->size - oldhead);
  187. memset (sq->items_inuse, 0, sq->head * sizeof (char));
  188. } else {
  189. // printf ("releasing %d for %d\n", oldhead, seqid - sq->head_seqid + 1);
  190. memset (&sq->items_inuse[oldhead], 0,
  191. (seqid - sq->head_seqid + 1) * sizeof (char));
  192. }
  193. sq->head_seqid = seqid + 1;
  194. }
  195. #endif /* SORTQUEUE_H_DEFINED */