sq.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.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. #include <string.h>
  38. struct sq {
  39. unsigned int head;
  40. unsigned int size;
  41. void *items;
  42. unsigned int *items_inuse;
  43. unsigned int size_per_item;
  44. unsigned int head_seqid;
  45. unsigned int item_count;
  46. unsigned int pos_max;
  47. };
  48. /*
  49. * Compare a unsigned rollover-safe value to an unsigned rollover-safe value
  50. */
  51. /*
  52. * ADJUST_ROLLOVER_POINT is the value used to determine when a window should be
  53. * used to calculate a less-then or less-then-equal comparison.
  54. *
  55. * ADJUST_ROLLOVER_VALUE is the value by which both values in a comparison are
  56. * adjusted if either value in a comparison is greater then
  57. * ADJUST_ROLLOVER_POINT.
  58. */
  59. #define ADJUST_ROLLOVER_POINT 0x80000000
  60. #define ADJUST_ROLLOVER_VALUE 0x10000
  61. static inline int sq_lt_compare (unsigned int a, unsigned int b) {
  62. if ((a > ADJUST_ROLLOVER_POINT) || (b > ADJUST_ROLLOVER_POINT)) {
  63. if ((a - ADJUST_ROLLOVER_VALUE) < (b - ADJUST_ROLLOVER_VALUE)) {
  64. return (1);
  65. }
  66. } else {
  67. if (a < b) {
  68. return (1);
  69. }
  70. }
  71. return (0);
  72. }
  73. static inline int sq_lte_compare (unsigned int a, unsigned int b) {
  74. if ((a > ADJUST_ROLLOVER_POINT) || (b > ADJUST_ROLLOVER_POINT)) {
  75. if ((a - ADJUST_ROLLOVER_VALUE) <= (b - ADJUST_ROLLOVER_VALUE)) {
  76. return (1);
  77. }
  78. } else {
  79. if (a <= b) {
  80. return (1);
  81. }
  82. }
  83. return (0);
  84. }
  85. static inline int sq_init (
  86. struct sq *sq,
  87. int item_count,
  88. int size_per_item,
  89. int head_seqid)
  90. {
  91. sq->head = 0;
  92. sq->size = item_count;
  93. sq->size_per_item = size_per_item;
  94. sq->head_seqid = head_seqid;
  95. sq->item_count = item_count;
  96. sq->pos_max = 0;
  97. sq->items = malloc (item_count * size_per_item);
  98. if (sq->items == NULL) {
  99. return (-ENOMEM);
  100. }
  101. memset (sq->items, 0, item_count * size_per_item);
  102. if ((sq->items_inuse = malloc (item_count * sizeof (unsigned int)))
  103. == NULL) {
  104. return (-ENOMEM);
  105. }
  106. memset (sq->items_inuse, 0, item_count * sizeof (unsigned int));
  107. return (0);
  108. }
  109. static inline void sq_reinit (struct sq *sq, unsigned int head_seqid)
  110. {
  111. sq->head = 0;
  112. sq->head_seqid = head_seqid;
  113. sq->pos_max = 0;
  114. memset (sq->items, 0, sq->item_count * sq->size_per_item);
  115. memset (sq->items_inuse, 0, sq->item_count * sizeof (unsigned int));
  116. }
  117. static inline void sq_assert (const struct sq *sq, unsigned int pos)
  118. {
  119. unsigned int i;
  120. // printf ("Instrument[%d] Asserting from %d to %d\n",
  121. // pos, sq->pos_max, sq->size);
  122. for (i = sq->pos_max + 1; i < sq->size; i++) {
  123. assert (sq->items_inuse[i] == 0);
  124. }
  125. }
  126. static inline void sq_copy (struct sq *sq_dest, const struct sq *sq_src)
  127. {
  128. sq_assert (sq_src, 20);
  129. sq_dest->head = sq_src->head;
  130. sq_dest->size = sq_src->item_count;
  131. sq_dest->size_per_item = sq_src->size_per_item;
  132. sq_dest->head_seqid = sq_src->head_seqid;
  133. sq_dest->item_count = sq_src->item_count;
  134. sq_dest->pos_max = sq_src->pos_max;
  135. memcpy (sq_dest->items, sq_src->items,
  136. sq_src->item_count * sq_src->size_per_item);
  137. memcpy (sq_dest->items_inuse, sq_src->items_inuse,
  138. sq_src->item_count * sizeof (unsigned int));
  139. }
  140. static inline void sq_free (struct sq *sq) {
  141. free (sq->items);
  142. free (sq->items_inuse);
  143. }
  144. static inline void *sq_item_add (
  145. struct sq *sq,
  146. void *item,
  147. unsigned int seqid)
  148. {
  149. char *sq_item;
  150. unsigned int sq_position;
  151. sq_position = (sq->head + seqid - sq->head_seqid) % sq->size;
  152. if (sq_position > sq->pos_max) {
  153. sq->pos_max = sq_position;
  154. }
  155. sq_item = sq->items;
  156. sq_item += sq_position * sq->size_per_item;
  157. assert(sq->items_inuse[sq_position] == 0);
  158. memcpy (sq_item, item, sq->size_per_item);
  159. if (seqid == 0) {
  160. sq->items_inuse[sq_position] = 1;
  161. } else {
  162. sq->items_inuse[sq_position] = seqid;
  163. }
  164. return (sq_item);
  165. }
  166. static inline unsigned int sq_item_inuse (
  167. const struct sq *sq,
  168. unsigned int seq_id) {
  169. unsigned int sq_position;
  170. /*
  171. * We need to say that the seqid is in use if it shouldn't
  172. * be here in the first place.
  173. * To keep old messages from being inserted.
  174. */
  175. #ifdef COMPILE_OUT
  176. if (seq_id < sq->head_seqid) {
  177. fprintf(stderr, "sq_item_inuse: seqid %d, head %d\n",
  178. seq_id, sq->head_seqid);
  179. return 1;
  180. }
  181. #endif
  182. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  183. return (sq->items_inuse[sq_position] != 0);
  184. }
  185. static inline unsigned int sq_size_get (
  186. const struct sq *sq)
  187. {
  188. return sq->size;
  189. }
  190. static inline unsigned int sq_in_range (
  191. const struct sq *sq,
  192. unsigned int seq_id)
  193. {
  194. int res = 1;
  195. if (sq->head_seqid > ADJUST_ROLLOVER_POINT) {
  196. if (seq_id - ADJUST_ROLLOVER_VALUE <
  197. sq->head_seqid - ADJUST_ROLLOVER_VALUE) {
  198. res = 0;
  199. }
  200. if ((seq_id - ADJUST_ROLLOVER_VALUE) >=
  201. ((sq->head_seqid - ADJUST_ROLLOVER_VALUE) + sq->size)) {
  202. res = 0;
  203. }
  204. } else {
  205. if (seq_id < sq->head_seqid) {
  206. res = 0;
  207. }
  208. if ((seq_id) >= ((sq->head_seqid) + sq->size)) {
  209. res = 0;
  210. }
  211. }
  212. return (res);
  213. }
  214. static inline unsigned int sq_item_get (
  215. const struct sq *sq,
  216. unsigned int seq_id,
  217. void **sq_item_out)
  218. {
  219. char *sq_item;
  220. unsigned int sq_position;
  221. if (seq_id > ADJUST_ROLLOVER_POINT) {
  222. assert ((seq_id - ADJUST_ROLLOVER_POINT) <
  223. ((sq->head_seqid - ADJUST_ROLLOVER_POINT) + sq->size));
  224. sq_position = ((sq->head - ADJUST_ROLLOVER_VALUE) -
  225. (sq->head_seqid - ADJUST_ROLLOVER_VALUE) + seq_id) % sq->size;
  226. } else {
  227. assert (seq_id < (sq->head_seqid + sq->size));
  228. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  229. }
  230. //printf ("seqid %x head %x head %x pos %x\n", seq_id, sq->head, sq->head_seqid, sq_position);
  231. // sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  232. //printf ("sq_position = %x\n", sq_position);
  233. //printf ("ITEMGET %d %d %d %d\n", sq_position, sq->head, sq->head_seqid, seq_id);
  234. if (sq->items_inuse[sq_position] == 0) {
  235. return (ENOENT);
  236. }
  237. sq_item = sq->items;
  238. sq_item += sq_position * sq->size_per_item;
  239. *sq_item_out = sq_item;
  240. return (0);
  241. }
  242. static inline void sq_items_release (struct sq *sq, unsigned int seqid)
  243. {
  244. unsigned int oldhead;
  245. oldhead = sq->head;
  246. sq->head = (sq->head + seqid - sq->head_seqid + 1) % sq->size;
  247. if ((oldhead + seqid - sq->head_seqid + 1) > sq->size) {
  248. // printf ("releasing %d for %d\n", oldhead, sq->size - oldhead);
  249. // printf ("releasing %d for %d\n", 0, sq->head);
  250. memset (&sq->items_inuse[oldhead], 0, (sq->size - oldhead) * sizeof (unsigned int));
  251. memset (sq->items_inuse, 0, sq->head * sizeof (unsigned int));
  252. } else {
  253. // printf ("releasing %d for %d\n", oldhead, seqid - sq->head_seqid + 1);
  254. memset (&sq->items_inuse[oldhead], 0,
  255. (seqid - sq->head_seqid + 1) * sizeof (unsigned int));
  256. }
  257. sq->head_seqid = seqid + 1;
  258. }
  259. #endif /* SORTQUEUE_H_DEFINED */