nghttp2_map.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. * Copyright (c) 2012 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef NGHTTP2_MAP_H
  27. #define NGHTTP2_MAP_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* HAVE_CONFIG_H */
  31. #include <nghttp2/nghttp2.h>
  32. #include "nghttp2_mem.h"
  33. /* Implementation of unordered map */
  34. typedef int32_t nghttp2_map_key_type;
  35. typedef struct nghttp2_map_bucket {
  36. uint32_t hash;
  37. nghttp2_map_key_type key;
  38. void *data;
  39. } nghttp2_map_bucket;
  40. typedef struct nghttp2_map {
  41. nghttp2_map_bucket *table;
  42. nghttp2_mem *mem;
  43. size_t size;
  44. uint32_t tablelen;
  45. uint32_t tablelenbits;
  46. } nghttp2_map;
  47. /*
  48. * Initializes the map |map|.
  49. *
  50. * This function returns 0 if it succeeds, or one of the following
  51. * negative error codes:
  52. *
  53. * NGHTTP2_ERR_NOMEM
  54. * Out of memory
  55. */
  56. int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem);
  57. /*
  58. * Deallocates any resources allocated for |map|. The stored entries
  59. * are not freed by this function. Use nghttp2_map_each_free() to free
  60. * each entries.
  61. */
  62. void nghttp2_map_free(nghttp2_map *map);
  63. /*
  64. * Deallocates each entries using |func| function and any resources
  65. * allocated for |map|. The |func| function is responsible for freeing
  66. * given the |data| object. The |ptr| will be passed to the |func| as
  67. * send argument. The return value of the |func| will be ignored.
  68. */
  69. void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),
  70. void *ptr);
  71. /*
  72. * Inserts the new |data| with the |key| to the map |map|.
  73. *
  74. * This function returns 0 if it succeeds, or one of the following
  75. * negative error codes:
  76. *
  77. * NGHTTP2_ERR_INVALID_ARGUMENT
  78. * The item associated by |key| already exists.
  79. * NGHTTP2_ERR_NOMEM
  80. * Out of memory
  81. */
  82. int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data);
  83. /*
  84. * Returns the data associated by the key |key|. If there is no such
  85. * data, this function returns NULL.
  86. */
  87. void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key);
  88. /*
  89. * Removes the data associated by the key |key| from the |map|. The
  90. * removed data is not freed by this function.
  91. *
  92. * This function returns 0 if it succeeds, or one of the following
  93. * negative error codes:
  94. *
  95. * NGHTTP2_ERR_INVALID_ARGUMENT
  96. * The data associated by |key| does not exist.
  97. */
  98. int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key);
  99. /*
  100. * Removes all entries from |map|.
  101. */
  102. void nghttp2_map_clear(nghttp2_map *map);
  103. /*
  104. * Returns the number of items stored in the map |map|.
  105. */
  106. size_t nghttp2_map_size(nghttp2_map *map);
  107. /*
  108. * Applies the function |func| to each data in the |map| with the
  109. * optional user supplied pointer |ptr|.
  110. *
  111. * If the |func| returns 0, this function calls the |func| with the
  112. * next data. If the |func| returns nonzero, it will not call the
  113. * |func| for further entries and return the return value of the
  114. * |func| immediately. Thus, this function returns 0 if all the
  115. * invocations of the |func| return 0, or nonzero value which the last
  116. * invocation of |func| returns.
  117. *
  118. * Don't use this function to free each data. Use
  119. * nghttp2_map_each_free() instead.
  120. */
  121. int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),
  122. void *ptr);
  123. void nghttp2_map_print_distance(nghttp2_map *map);
  124. #endif /* NGHTTP2_MAP_H */