sq.h 8.9 KB

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