sq.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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. int head;
  39. int size;
  40. void *items;
  41. unsigned char *items_inuse;
  42. int size_per_item;
  43. int head_seqid;
  44. int item_count;
  45. };
  46. static inline int sq_init (
  47. struct sq *sq,
  48. int item_count,
  49. int size_per_item,
  50. int head_seqid)
  51. {
  52. sq->head = 0;
  53. sq->size = item_count;
  54. sq->size_per_item = size_per_item;
  55. sq->head_seqid = head_seqid;
  56. sq->item_count = item_count;
  57. sq->items = (void *)malloc (item_count * size_per_item);
  58. if (sq->items == 0) {
  59. return (-ENOMEM);
  60. }
  61. memset (sq->items, 0, item_count * size_per_item);
  62. sq->items_inuse = (void *)malloc (item_count * sizeof (char));
  63. memset (sq->items_inuse, 0, item_count * sizeof (char));
  64. return (0);
  65. }
  66. static inline void sq_reinit (struct sq *sq, int head_seqid)
  67. {
  68. sq->head = 0;
  69. sq->head_seqid = head_seqid;
  70. memset (sq->items, 0, sq->item_count * sq->size_per_item);
  71. memset (sq->items_inuse, 0, sq->item_count * sizeof (char));
  72. }
  73. static inline void sq_free (struct sq *sq) {
  74. free (sq->items);
  75. free (sq->items_inuse);
  76. }
  77. static inline int sq_item_add (
  78. struct sq *sq,
  79. void *item,
  80. int seqid)
  81. {
  82. char *sq_item;
  83. int sq_position;
  84. if (seqid - sq->head_seqid >= sq->size) {
  85. return E2BIG;
  86. }
  87. sq_position = (sq->head + seqid - sq->head_seqid) % sq->size;
  88. //printf ("item add %d %d %d\n", sq_position, seqid, sq->head_seqid);
  89. sq_item = sq->items;
  90. sq_item += sq_position * sq->size_per_item;
  91. assert(sq->items_inuse[sq_position] == 0);
  92. memcpy (sq_item, item, sq->size_per_item);
  93. sq->items_inuse[sq_position] = 1;
  94. return (0);
  95. }
  96. static inline int sq_item_inuse (
  97. struct sq *sq,
  98. int seq_id) {
  99. int sq_position;
  100. /*
  101. * We need to say that the seqid is in use if it shouldn't
  102. * be here in the first place.
  103. * To keep old messages from being inserted.
  104. */
  105. if (seq_id < sq->head_seqid) {
  106. fprintf(stderr, "sq_item_inuse: seqid %d, head %d\n",
  107. seq_id, sq->head_seqid);
  108. return 1;
  109. }
  110. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  111. //printf ("in use %d\n", sq_position);
  112. return (sq->items_inuse[sq_position]);
  113. }
  114. static inline int sq_size_get (
  115. struct sq *sq)
  116. {
  117. return sq->size;
  118. }
  119. static inline int sq_item_get (
  120. struct sq *sq,
  121. int seq_id,
  122. void **sq_item_out)
  123. {
  124. char *sq_item;
  125. int sq_position;
  126. if (seq_id == -1) {
  127. return (ENOENT);
  128. }
  129. assert (seq_id < (sq->head_seqid + sq->size));
  130. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  131. //printf ("ITEMGET %d %d %d %d\n", sq_position, sq->head, sq->head_seqid, seq_id);
  132. assert (sq_position >= 0);
  133. //printf ("itme get in use %d\n", sq_position);
  134. if (sq->items_inuse[sq_position] == 0) {
  135. //printf ("ENOENT\n");
  136. return (ENOENT);
  137. }
  138. sq_item = sq->items;
  139. sq_item += sq_position * sq->size_per_item;
  140. *sq_item_out = sq_item;
  141. return (0);
  142. }
  143. static inline void sq_items_release (struct sq *sq, int seqid)
  144. {
  145. int oldhead;
  146. if (seqid < sq->head_seqid) {
  147. return;
  148. }
  149. oldhead = sq->head;
  150. sq->head = (sq->head + seqid - sq->head_seqid + 1) % sq->size;
  151. if ((oldhead + seqid - sq->head_seqid + 1) > sq->size) {
  152. //printf ("releasing %d for %d\n", oldhead, sq->size - oldhead);
  153. //printf ("releasing %d for %d\n", 0, sq->head);
  154. memset (&sq->items_inuse[oldhead], 0, sq->size - oldhead);
  155. memset (sq->items_inuse, 0, sq->head * sizeof (char));
  156. } else {
  157. //printf ("releasing %d for %d\n", oldhead, seqid - sq->head_seqid + 1);
  158. memset (&sq->items_inuse[oldhead], 0,
  159. (seqid - sq->head_seqid + 1) * sizeof (char));
  160. }
  161. sq->head_seqid = seqid + 1;
  162. }
  163. #endif /* SORTQUEUE_H_DEFINED */