nghttp3_map.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2017 ngtcp2 contributors
  6. * Copyright (c) 2012 nghttp2 contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #ifndef NGHTTP3_MAP_H
  28. #define NGHTTP3_MAP_H
  29. #ifdef HAVE_CONFIG_H
  30. # include <config.h>
  31. #endif /* defined(HAVE_CONFIG_H) */
  32. #include <nghttp3/nghttp3.h>
  33. #include "nghttp3_mem.h"
  34. /* Implementation of unordered map */
  35. typedef uint64_t nghttp3_map_key_type;
  36. typedef struct nghttp3_map_bucket {
  37. uint32_t psl;
  38. nghttp3_map_key_type key;
  39. void *data;
  40. } nghttp3_map_bucket;
  41. typedef struct nghttp3_map {
  42. nghttp3_map_bucket *table;
  43. const nghttp3_mem *mem;
  44. size_t size;
  45. size_t hashbits;
  46. } nghttp3_map;
  47. /*
  48. * nghttp3_map_init initializes the map |map|.
  49. */
  50. void nghttp3_map_init(nghttp3_map *map, const nghttp3_mem *mem);
  51. /*
  52. * nghttp3_map_free deallocates any resources allocated for |map|.
  53. * The stored entries are not freed by this function. Use
  54. * nghttp3_map_each() to free each entry.
  55. */
  56. void nghttp3_map_free(nghttp3_map *map);
  57. /*
  58. * nghttp3_map_insert inserts the new |data| with the |key| to the map
  59. * |map|.
  60. *
  61. * This function returns 0 if it succeeds, or one of the following
  62. * negative error codes:
  63. *
  64. * NGHTTP3_ERR_INVALID_ARGUMENT
  65. * The item associated by |key| already exists.
  66. * NGHTTP3_ERR_NOMEM
  67. * Out of memory
  68. */
  69. int nghttp3_map_insert(nghttp3_map *map, nghttp3_map_key_type key, void *data);
  70. /*
  71. * nghttp3_map_find returns the entry associated by the key |key|. If
  72. * there is no such entry, this function returns NULL.
  73. */
  74. void *nghttp3_map_find(const nghttp3_map *map, nghttp3_map_key_type key);
  75. /*
  76. * nghttp3_map_remove removes the entry associated by the key |key|
  77. * from the |map|. The removed entry is not freed by this function.
  78. *
  79. * This function returns 0 if it succeeds, or one of the following
  80. * negative error codes:
  81. *
  82. * NGHTTP3_ERR_INVALID_ARGUMENT
  83. * The entry associated by |key| does not exist.
  84. */
  85. int nghttp3_map_remove(nghttp3_map *map, nghttp3_map_key_type key);
  86. /*
  87. * nghttp3_map_clear removes all entries from |map|. The removed
  88. * entry is not freed by this function.
  89. */
  90. void nghttp3_map_clear(nghttp3_map *map);
  91. /*
  92. * nghttp3_map_size returns the number of items stored in the map
  93. * |map|.
  94. */
  95. size_t nghttp3_map_size(const nghttp3_map *map);
  96. /*
  97. * nghttp3_map_each applies the function |func| to each entry in the
  98. * |map| with the optional user supplied pointer |ptr|.
  99. *
  100. * If the |func| returns 0, this function calls the |func| with the
  101. * next entry. If the |func| returns nonzero, it will not call the
  102. * |func| for further entries and return the return value of the
  103. * |func| immediately. Thus, this function returns 0 if all the
  104. * invocations of the |func| return 0, or nonzero value which the last
  105. * invocation of |func| returns.
  106. */
  107. int nghttp3_map_each(const nghttp3_map *map, int (*func)(void *data, void *ptr),
  108. void *ptr);
  109. #ifndef WIN32
  110. void nghttp3_map_print_distance(const nghttp3_map *map);
  111. #endif /* !defined(WIN32) */
  112. #endif /* !defined(NGHTTP3_MAP_H) */