uint-table.c 5.4 KB

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