nghttp2_map.h 4.5 KB

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