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. 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 = malloc (item_count * size_per_item);
  97. if (sq->items == NULL) {
  98. return (-ENOMEM);
  99. }
  100. memset (sq->items, 0, item_count * size_per_item);
  101. if ((sq->items_inuse = malloc (item_count * sizeof (unsigned int)))
  102. == NULL) {
  103. return (-ENOMEM);
  104. }
  105. memset (sq->items_inuse, 0, item_count * sizeof (unsigned int));
  106. return (0);
  107. }
  108. static inline void sq_reinit (struct sq *sq, unsigned int head_seqid)
  109. {
  110. sq->head = 0;
  111. sq->head_seqid = head_seqid;
  112. sq->pos_max = 0;
  113. memset (sq->items, 0, sq->item_count * sq->size_per_item);
  114. memset (sq->items_inuse, 0, sq->item_count * sizeof (unsigned int));
  115. }
  116. static inline void sq_assert (const struct sq *sq, unsigned int pos)
  117. {
  118. unsigned int i;
  119. // printf ("Instrument[%d] Asserting from %d to %d\n",
  120. // pos, sq->pos_max, sq->size);
  121. for (i = sq->pos_max + 1; i < sq->size; i++) {
  122. assert (sq->items_inuse[i] == 0);
  123. }
  124. }
  125. static inline void sq_copy (struct sq *sq_dest, const struct sq *sq_src)
  126. {
  127. sq_assert (sq_src, 20);
  128. sq_dest->head = sq_src->head;
  129. sq_dest->size = sq_src->item_count;
  130. sq_dest->size_per_item = sq_src->size_per_item;
  131. sq_dest->head_seqid = sq_src->head_seqid;
  132. sq_dest->item_count = sq_src->item_count;
  133. sq_dest->pos_max = sq_src->pos_max;
  134. memcpy (sq_dest->items, sq_src->items,
  135. sq_src->item_count * sq_src->size_per_item);
  136. memcpy (sq_dest->items_inuse, sq_src->items_inuse,
  137. sq_src->item_count * sizeof (unsigned int));
  138. }
  139. static inline void sq_free (struct sq *sq) {
  140. free (sq->items);
  141. free (sq->items_inuse);
  142. }
  143. static inline void *sq_item_add (
  144. struct sq *sq,
  145. void *item,
  146. unsigned int seqid)
  147. {
  148. char *sq_item;
  149. unsigned int sq_position;
  150. sq_position = (sq->head + seqid - sq->head_seqid) % sq->size;
  151. if (sq_position > sq->pos_max) {
  152. sq->pos_max = sq_position;
  153. }
  154. sq_item = sq->items;
  155. sq_item += sq_position * sq->size_per_item;
  156. assert(sq->items_inuse[sq_position] == 0);
  157. memcpy (sq_item, item, sq->size_per_item);
  158. if (seqid == 0) {
  159. sq->items_inuse[sq_position] = 1;
  160. } else {
  161. sq->items_inuse[sq_position] = seqid;
  162. }
  163. return (sq_item);
  164. }
  165. static inline unsigned int sq_item_inuse (
  166. const struct sq *sq,
  167. unsigned int seq_id) {
  168. unsigned int sq_position;
  169. /*
  170. * We need to say that the seqid is in use if it shouldn't
  171. * be here in the first place.
  172. * To keep old messages from being inserted.
  173. */
  174. #ifdef COMPILE_OUT
  175. if (seq_id < sq->head_seqid) {
  176. fprintf(stderr, "sq_item_inuse: seqid %d, head %d\n",
  177. seq_id, sq->head_seqid);
  178. return 1;
  179. }
  180. #endif
  181. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  182. return (sq->items_inuse[sq_position] != 0);
  183. }
  184. static inline unsigned int sq_size_get (
  185. const struct sq *sq)
  186. {
  187. return sq->size;
  188. }
  189. static inline unsigned int sq_in_range (
  190. const struct sq *sq,
  191. unsigned int seq_id)
  192. {
  193. int res = 1;
  194. if (sq->head_seqid > ADJUST_ROLLOVER_POINT) {
  195. if (seq_id - ADJUST_ROLLOVER_VALUE <
  196. sq->head_seqid - ADJUST_ROLLOVER_VALUE) {
  197. res = 0;
  198. }
  199. if ((seq_id - ADJUST_ROLLOVER_VALUE) >=
  200. ((sq->head_seqid - ADJUST_ROLLOVER_VALUE) + sq->size)) {
  201. res = 0;
  202. }
  203. } else {
  204. if (seq_id < sq->head_seqid) {
  205. res = 0;
  206. }
  207. if ((seq_id) >= ((sq->head_seqid) + sq->size)) {
  208. res = 0;
  209. }
  210. }
  211. return (res);
  212. }
  213. static inline unsigned int sq_item_get (
  214. const struct sq *sq,
  215. unsigned int seq_id,
  216. void **sq_item_out)
  217. {
  218. char *sq_item;
  219. unsigned int sq_position;
  220. if (seq_id > ADJUST_ROLLOVER_POINT) {
  221. assert ((seq_id - ADJUST_ROLLOVER_POINT) <
  222. ((sq->head_seqid - ADJUST_ROLLOVER_POINT) + sq->size));
  223. sq_position = ((sq->head - ADJUST_ROLLOVER_VALUE) -
  224. (sq->head_seqid - ADJUST_ROLLOVER_VALUE) + seq_id) % sq->size;
  225. } else {
  226. assert (seq_id < (sq->head_seqid + sq->size));
  227. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  228. }
  229. //printf ("seqid %x head %x head %x pos %x\n", seq_id, sq->head, sq->head_seqid, sq_position);
  230. // sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  231. //printf ("sq_position = %x\n", sq_position);
  232. //printf ("ITEMGET %d %d %d %d\n", sq_position, sq->head, sq->head_seqid, seq_id);
  233. assert (sq_position >= 0);
  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 */