uint-spbset.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "uint-bset.h"
  26. #include "uint-spbset.h"
  27. /* The last 2 #include files should be in this order */
  28. #include "curl_memory.h"
  29. #include "memdebug.h"
  30. #ifdef DEBUGBUILD
  31. #define CURL_UINT_SPBSET_MAGIC 0x70737362
  32. #endif
  33. /* Clear the bitset, making it empty. */
  34. UNITTEST void Curl_uint_spbset_clear(struct uint_spbset *bset);
  35. void Curl_uint_spbset_init(struct uint_spbset *bset)
  36. {
  37. memset(bset, 0, sizeof(*bset));
  38. #ifdef DEBUGBUILD
  39. bset->init = CURL_UINT_SPBSET_MAGIC;
  40. #endif
  41. }
  42. void Curl_uint_spbset_destroy(struct uint_spbset *bset)
  43. {
  44. DEBUGASSERT(bset->init == CURL_UINT_SPBSET_MAGIC);
  45. Curl_uint_spbset_clear(bset);
  46. }
  47. unsigned int Curl_uint_spbset_count(struct uint_spbset *bset)
  48. {
  49. struct uint_spbset_chunk *chunk;
  50. unsigned int i, n = 0;
  51. for(chunk = &bset->head; chunk; chunk = chunk->next) {
  52. for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
  53. if(chunk->slots[i])
  54. n += CURL_POPCOUNT64(chunk->slots[i]);
  55. }
  56. }
  57. return n;
  58. }
  59. bool Curl_uint_spbset_empty(struct uint_spbset *bset)
  60. {
  61. struct uint_spbset_chunk *chunk;
  62. unsigned int i;
  63. for(chunk = &bset->head; chunk; chunk = chunk->next) {
  64. for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
  65. if(chunk->slots[i])
  66. return FALSE;
  67. }
  68. }
  69. return TRUE;
  70. }
  71. UNITTEST void Curl_uint_spbset_clear(struct uint_spbset *bset)
  72. {
  73. struct uint_spbset_chunk *next, *chunk;
  74. for(chunk = bset->head.next; chunk; chunk = next) {
  75. next = chunk->next;
  76. free(chunk);
  77. }
  78. memset(&bset->head, 0, sizeof(bset->head));
  79. }
  80. static struct uint_spbset_chunk *
  81. uint_spbset_get_chunk(struct uint_spbset *bset, unsigned int i, bool grow)
  82. {
  83. struct uint_spbset_chunk *chunk, **panchor = NULL;
  84. unsigned int i_offset = (i & ~CURL_UINT_SPBSET_CH_MASK);
  85. if(!bset)
  86. return NULL;
  87. for(chunk = &bset->head; chunk;
  88. panchor = &chunk->next, chunk = chunk->next) {
  89. if(chunk->offset == i_offset) {
  90. return chunk;
  91. }
  92. else if(chunk->offset > i_offset) {
  93. /* need new chunk here */
  94. chunk = NULL;
  95. break;
  96. }
  97. }
  98. if(!grow)
  99. return NULL;
  100. /* need a new one */
  101. chunk = calloc(1, sizeof(*chunk));
  102. if(!chunk)
  103. return NULL;
  104. if(panchor) { /* insert between panchor and *panchor */
  105. chunk->next = *panchor;
  106. *panchor = chunk;
  107. }
  108. else { /* prepend to head, switching places */
  109. memcpy(chunk, &bset->head, sizeof(*chunk));
  110. memset(&bset->head, 0, sizeof(bset->head));
  111. bset->head.next = chunk;
  112. }
  113. chunk->offset = i_offset;
  114. return chunk;
  115. }
  116. bool Curl_uint_spbset_add(struct uint_spbset *bset, unsigned int i)
  117. {
  118. struct uint_spbset_chunk *chunk;
  119. unsigned int i_chunk;
  120. chunk = uint_spbset_get_chunk(bset, i, TRUE);
  121. if(!chunk)
  122. return FALSE;
  123. DEBUGASSERT(i >= chunk->offset);
  124. i_chunk = (i - chunk->offset);
  125. DEBUGASSERT((i_chunk / 64) < CURL_UINT_SPBSET_CH_SLOTS);
  126. chunk->slots[(i_chunk / 64)] |= ((curl_uint64_t)1 << (i_chunk % 64));
  127. return TRUE;
  128. }
  129. void Curl_uint_spbset_remove(struct uint_spbset *bset, unsigned int i)
  130. {
  131. struct uint_spbset_chunk *chunk;
  132. unsigned int i_chunk;
  133. chunk = uint_spbset_get_chunk(bset, i, FALSE);
  134. if(chunk) {
  135. DEBUGASSERT(i >= chunk->offset);
  136. i_chunk = (i - chunk->offset);
  137. DEBUGASSERT((i_chunk / 64) < CURL_UINT_SPBSET_CH_SLOTS);
  138. chunk->slots[(i_chunk / 64)] &= ~((curl_uint64_t)1 << (i_chunk % 64));
  139. }
  140. }
  141. bool Curl_uint_spbset_contains(struct uint_spbset *bset, unsigned int i)
  142. {
  143. struct uint_spbset_chunk *chunk;
  144. unsigned int i_chunk;
  145. chunk = uint_spbset_get_chunk(bset, i, FALSE);
  146. if(chunk) {
  147. DEBUGASSERT(i >= chunk->offset);
  148. i_chunk = (i - chunk->offset);
  149. DEBUGASSERT((i_chunk / 64) < CURL_UINT_SPBSET_CH_SLOTS);
  150. return (chunk->slots[i_chunk / 64] &
  151. ((curl_uint64_t)1 << (i_chunk % 64))) != 0;
  152. }
  153. return FALSE;
  154. }
  155. bool Curl_uint_spbset_first(struct uint_spbset *bset, unsigned int *pfirst)
  156. {
  157. struct uint_spbset_chunk *chunk;
  158. unsigned int i;
  159. for(chunk = &bset->head; chunk; chunk = chunk->next) {
  160. for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
  161. if(chunk->slots[i]) {
  162. *pfirst = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
  163. return TRUE;
  164. }
  165. }
  166. }
  167. *pfirst = 0; /* give it a defined value even if it should not be used */
  168. return FALSE;
  169. }
  170. static bool uint_spbset_chunk_first(struct uint_spbset_chunk *chunk,
  171. unsigned int *pfirst)
  172. {
  173. unsigned int i;
  174. for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
  175. if(chunk->slots[i]) {
  176. *pfirst = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
  177. return TRUE;
  178. }
  179. }
  180. *pfirst = UINT_MAX; /* a value we cannot store */
  181. return FALSE;
  182. }
  183. static bool uint_spbset_chunk_next(struct uint_spbset_chunk *chunk,
  184. unsigned int last,
  185. unsigned int *pnext)
  186. {
  187. if(chunk->offset <= last) {
  188. curl_uint64_t x;
  189. unsigned int i = ((last - chunk->offset) / 64);
  190. if(i < CURL_UINT_SPBSET_CH_SLOTS) {
  191. x = (chunk->slots[i] >> (last % 64));
  192. if(x) {
  193. /* more bits set, next is `last` + trailing0s of the shifted slot */
  194. *pnext = last + CURL_CTZ64(x);
  195. return TRUE;
  196. }
  197. /* no more bits set in the last slot, scan forward */
  198. for(i = i + 1; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
  199. if(chunk->slots[i]) {
  200. *pnext = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
  201. return TRUE;
  202. }
  203. }
  204. }
  205. }
  206. *pnext = UINT_MAX;
  207. return FALSE;
  208. }
  209. bool Curl_uint_spbset_next(struct uint_spbset *bset, unsigned int last,
  210. unsigned int *pnext)
  211. {
  212. struct uint_spbset_chunk *chunk;
  213. unsigned int last_offset;
  214. ++last; /* look for the next higher number */
  215. last_offset = (last & ~CURL_UINT_SPBSET_CH_MASK);
  216. for(chunk = &bset->head; chunk; chunk = chunk->next) {
  217. if(chunk->offset >= last_offset) {
  218. break;
  219. }
  220. }
  221. if(chunk && (chunk->offset == last_offset)) {
  222. /* is there a number higher than last in this chunk? */
  223. if(uint_spbset_chunk_next(chunk, last, pnext))
  224. return TRUE;
  225. /* not in this chunk */
  226. chunk = chunk->next;
  227. }
  228. /* look for the first in the "higher" chunks, if there are any. */
  229. while(chunk) {
  230. if(uint_spbset_chunk_first(chunk, pnext))
  231. return TRUE;
  232. chunk = chunk->next;
  233. }
  234. *pnext = UINT_MAX;
  235. return FALSE;
  236. }