sq.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. memcpy (sq_item, item, sq->size_per_item);
  92. sq->items_inuse[sq_position] = 1;
  93. return (0);
  94. }
  95. static inline int sq_item_inuse (
  96. struct sq *sq,
  97. int seq_id) {
  98. int sq_position;
  99. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  100. //printf ("in use %d\n", sq_position);
  101. return (sq->items_inuse[sq_position]);
  102. }
  103. static inline int sq_size_get (
  104. struct sq *sq)
  105. {
  106. return sq->size;
  107. }
  108. static inline int sq_item_get (
  109. struct sq *sq,
  110. int seq_id,
  111. void **sq_item_out)
  112. {
  113. char *sq_item;
  114. int sq_position;
  115. if (seq_id == -1) {
  116. return (ENOENT);
  117. }
  118. assert (seq_id < (sq->head_seqid + sq->size));
  119. sq_position = (sq->head - sq->head_seqid + seq_id) % sq->size;
  120. //printf ("ITEMGET %d %d %d %d\n", sq_position, sq->head, sq->head_seqid, seq_id);
  121. assert (sq_position >= 0);
  122. //printf ("itme get in use %d\n", sq_position);
  123. if (sq->items_inuse[sq_position] == 0) {
  124. //printf ("ENOENT\n");
  125. return (ENOENT);
  126. }
  127. sq_item = sq->items;
  128. sq_item += sq_position * sq->size_per_item;
  129. *sq_item_out = sq_item;
  130. return (0);
  131. }
  132. static inline void sq_items_release (struct sq *sq, int seqid)
  133. {
  134. int oldhead;
  135. char *sq_item;
  136. if (seqid < sq->head_seqid) {
  137. //printf ("%d %d\n", seqid, sq->head_seqid);
  138. return;
  139. }
  140. //printf ("releasing %d\n", seqid);
  141. oldhead = sq->head;
  142. //printf ("before sq->head %d\n", sq->head);
  143. sq->head = (sq->head + seqid - sq->head_seqid + 1) % sq->size;
  144. //printf ("after sq->head %d\n", sq->head);
  145. if ((oldhead + seqid - sq->head_seqid + 1) > sq->size) {
  146. //printf ("memset 1\n");
  147. //printf ("%d %d %d %d\n", seqid, sq->head_seqid, sq->head, sq->size);
  148. memset (&sq->items_inuse[oldhead], 0, sq->size - oldhead);
  149. memset (sq->items_inuse, 0, sq->head * sizeof (char));
  150. //printf ("SIZEOF %d %d\n", sq->head, sq->head * sizeof (char));
  151. // memset (sq->items, 0, (sq->head) * (sq->size_per_item));
  152. } else {
  153. assert (seqid - sq->head_seqid + 1);
  154. //printf ("memset 2\n");
  155. //printf ("releasing %d for %d\n", oldhead, seqid - sq->head_seqid + 1);
  156. memset (&sq->items_inuse[oldhead - 1], 0, (seqid - sq->head_seqid + 2) * sizeof (char));
  157. sq_item = sq->items;
  158. sq_item += oldhead * sq->size_per_item;
  159. // memset (sq_item[oldhead], 0, (seqid - sq->head_seqid + 1) * (sq->size_per_item));
  160. }
  161. sq->head_seqid = seqid + 1;
  162. }
  163. #endif /* SORTQUEUE_H_DEFINED */