sq.h 8.0 KB

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