uint-table.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef HEADER_CURL_UINT_TABLE_H
  2. #define HEADER_CURL_UINT_TABLE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <curl/curl.h>
  28. /* Destructor for a single table entry */
  29. typedef void Curl_uint_tbl_entry_dtor(unsigned int key, void *entry);
  30. struct uint_tbl {
  31. void **rows; /* array of void* holding entries */
  32. Curl_uint_tbl_entry_dtor *entry_dtor;
  33. unsigned int nrows; /* length of `rows` array */
  34. unsigned int nentries; /* entries in table */
  35. unsigned int last_key_added; /* UINT_MAX or last key added */
  36. #ifdef DEBUGBUILD
  37. int init;
  38. #endif
  39. };
  40. /* Initialize the table with 0 capacity.
  41. * The optional `entry_dtor` is called when a table entry is removed,
  42. * Passing NULL means no action is taken on removal. */
  43. void Curl_uint_tbl_init(struct uint_tbl *tbl,
  44. Curl_uint_tbl_entry_dtor *entry_dtor);
  45. /* Resize the table to change capacity `nmax`. When `nmax` is reduced,
  46. * all present entries with key equal or larger to `nmax` are removed. */
  47. CURLcode Curl_uint_tbl_resize(struct uint_tbl *tbl, unsigned int nmax);
  48. /* Destroy the table, freeing all entries. */
  49. void Curl_uint_tbl_destroy(struct uint_tbl *tbl);
  50. /* Get the table capacity. */
  51. unsigned int Curl_uint_tbl_capacity(struct uint_tbl *tbl);
  52. /* Get the number of entries in the table. */
  53. unsigned int Curl_uint_tbl_count(struct uint_tbl *tbl);
  54. /* Get the entry for key or NULL if not present */
  55. void *Curl_uint_tbl_get(struct uint_tbl *tbl, unsigned int key);
  56. /* Add a new entry to the table and assign it a free key.
  57. * Returns FALSE if the table is full.
  58. *
  59. * Keys are assigned in a round-robin manner.
  60. * No matter the capacity, UINT_MAX is never assigned. */
  61. bool Curl_uint_tbl_add(struct uint_tbl *tbl, void *entry, unsigned int *pkey);
  62. /* Remove the entry with `key`. */
  63. void Curl_uint_tbl_remove(struct uint_tbl *tbl, unsigned int key);
  64. /* Return TRUE if the table contains an tryn with that keys. */
  65. bool Curl_uint_tbl_contains(struct uint_tbl *tbl, unsigned int key);
  66. /* Get the first entry in the table (with the smallest `key`).
  67. * Returns FALSE if the table is empty. */
  68. bool Curl_uint_tbl_first(struct uint_tbl *tbl,
  69. unsigned int *pkey, void **pentry);
  70. /* Get the next key in the table, following `last_key` in natural order.
  71. * Put another way, this is the smallest key greater than `last_key` in
  72. * the table. `last_key` does not have to be present in the table.
  73. *
  74. * Returns FALSE when no such entry is in the table.
  75. *
  76. * This allows to iterate the table while being modified:
  77. * - added keys higher than 'last_key' will be picked up by the iteration.
  78. * - added keys lower than 'last_key' will not show up.
  79. * - removed keys lower or equal to 'last_key' will not show up.
  80. * - removed keys higher than 'last_key' will not be visited. */
  81. bool Curl_uint_tbl_next(struct uint_tbl *tbl, unsigned int last_key,
  82. unsigned int *pkey, void **pentry);
  83. #endif /* HEADER_CURL_UINT_TABLE_H */