sq.h 8.9 KB

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