uint-table.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-table.h"
  26. #ifdef DEBUGBUILD
  27. #define CURL_UINT32_TBL_MAGIC 0x62757473
  28. #endif
  29. /* Clear the table, making it empty. */
  30. UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl);
  31. void Curl_uint32_tbl_init(struct uint32_tbl *tbl,
  32. Curl_uint32_tbl_entry_dtor *entry_dtor)
  33. {
  34. memset(tbl, 0, sizeof(*tbl));
  35. tbl->entry_dtor = entry_dtor;
  36. tbl->last_key_added = UINT32_MAX;
  37. #ifdef DEBUGBUILD
  38. tbl->init = CURL_UINT32_TBL_MAGIC;
  39. #endif
  40. }
  41. static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
  42. uint32_t from,
  43. uint32_t upto_excluding)
  44. {
  45. uint32_t i, end;
  46. end = CURLMIN(upto_excluding, tbl->nrows);
  47. for(i = from; i < end; ++i) {
  48. if(tbl->rows[i]) {
  49. if(tbl->entry_dtor)
  50. tbl->entry_dtor(i, tbl->rows[i]);
  51. tbl->rows[i] = NULL;
  52. tbl->nentries--;
  53. }
  54. }
  55. }
  56. CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
  57. {
  58. /* we use `tbl->nrows + 1` during iteration, want that to work */
  59. DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
  60. if(!nrows)
  61. return CURLE_BAD_FUNCTION_ARGUMENT;
  62. if(nrows != tbl->nrows) {
  63. void **rows = curlx_calloc(nrows, sizeof(void *));
  64. if(!rows)
  65. return CURLE_OUT_OF_MEMORY;
  66. if(tbl->rows) {
  67. memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *)));
  68. if(nrows < tbl->nrows)
  69. uint32_tbl_clear_rows(tbl, nrows, tbl->nrows);
  70. curlx_free(tbl->rows);
  71. }
  72. tbl->rows = rows;
  73. tbl->nrows = nrows;
  74. }
  75. return CURLE_OK;
  76. }
  77. void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
  78. {
  79. DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
  80. Curl_uint32_tbl_clear(tbl);
  81. curlx_free(tbl->rows);
  82. memset(tbl, 0, sizeof(*tbl));
  83. }
  84. UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl)
  85. {
  86. DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
  87. uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
  88. DEBUGASSERT(!tbl->nentries);
  89. tbl->last_key_added = UINT32_MAX;
  90. }
  91. uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
  92. {
  93. return tbl->nrows;
  94. }
  95. uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
  96. {
  97. return tbl->nentries;
  98. }
  99. void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
  100. {
  101. return (key < tbl->nrows) ? tbl->rows[key] : NULL;
  102. }
  103. bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
  104. {
  105. uint32_t key, start_pos;
  106. DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
  107. if(!entry || !pkey)
  108. return FALSE;
  109. *pkey = UINT32_MAX;
  110. if(tbl->nentries == tbl->nrows) /* full */
  111. return FALSE;
  112. start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
  113. for(key = start_pos; key < tbl->nrows; ++key) {
  114. if(!tbl->rows[key]) {
  115. tbl->rows[key] = entry;
  116. tbl->nentries++;
  117. tbl->last_key_added = key;
  118. *pkey = key;
  119. return TRUE;
  120. }
  121. }
  122. /* no free entry at or above tbl->maybe_next_key, wrap around */
  123. for(key = 0; key < start_pos; ++key) {
  124. if(!tbl->rows[key]) {
  125. tbl->rows[key] = entry;
  126. tbl->nentries++;
  127. tbl->last_key_added = key;
  128. *pkey = key;
  129. return TRUE;
  130. }
  131. }
  132. /* Did not find any free row? Should not happen */
  133. DEBUGASSERT(0);
  134. return FALSE;
  135. }
  136. void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key)
  137. {
  138. uint32_tbl_clear_rows(tbl, key, key + 1);
  139. }
  140. bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
  141. {
  142. return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
  143. }
  144. static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
  145. uint32_t *pkey, void **pentry)
  146. {
  147. for(; key < tbl->nrows; ++key) {
  148. if(tbl->rows[key]) {
  149. *pkey = key;
  150. *pentry = tbl->rows[key];
  151. return TRUE;
  152. }
  153. }
  154. *pkey = UINT32_MAX; /* always invalid */
  155. *pentry = NULL;
  156. return FALSE;
  157. }
  158. bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
  159. uint32_t *pkey, void **pentry)
  160. {
  161. if(!pkey || !pentry)
  162. return FALSE;
  163. if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
  164. return TRUE;
  165. DEBUGASSERT(!tbl->nentries);
  166. *pkey = UINT32_MAX; /* always invalid */
  167. *pentry = NULL;
  168. return FALSE;
  169. }
  170. bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key,
  171. uint32_t *pkey, void **pentry)
  172. {
  173. if(!pkey || !pentry)
  174. return FALSE;
  175. if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
  176. return TRUE;
  177. *pkey = UINT32_MAX; /* always invalid */
  178. *pentry = NULL;
  179. return FALSE;
  180. }