sq.h 9.9 KB

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