nghttp2_map.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #ifndef NGHTTP2_MAP_H
  5. #define NGHTTP2_MAP_H
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif /* HAVE_CONFIG_H */
  9. #include "nghttp2.h"
  10. #include "nghttp2_int.h"
  11. #include "nghttp2_mem.h"
  12. /* Implementation of unordered map */
  13. typedef int32_t key_type;
  14. typedef struct nghttp2_map_entry {
  15. struct nghttp2_map_entry *next;
  16. key_type key;
  17. #if SIZEOF_INT_P == 4
  18. /* we requires 8 bytes aligment */
  19. int64_t pad;
  20. #endif
  21. } nghttp2_map_entry;
  22. typedef struct {
  23. nghttp2_map_entry **table;
  24. nghttp2_mem *mem;
  25. size_t size;
  26. uint32_t tablelen;
  27. } nghttp2_map;
  28. /*
  29. * Initializes the map |map|.
  30. *
  31. * This function returns 0 if it succeeds, or one of the following
  32. * negative error codes:
  33. *
  34. * NGHTTP2_ERR_NOMEM
  35. * Out of memory
  36. */
  37. int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem);
  38. /*
  39. * Deallocates any resources allocated for |map|. The stored entries
  40. * are not freed by this function. Use nghttp2_map_each_free() to free
  41. * each entries.
  42. */
  43. void nghttp2_map_free(nghttp2_map *map);
  44. /*
  45. * Deallocates each entries using |func| function and any resources
  46. * allocated for |map|. The |func| function is responsible for freeing
  47. * given the |entry| object. The |ptr| will be passed to the |func| as
  48. * send argument. The return value of the |func| will be ignored.
  49. */
  50. void nghttp2_map_each_free(nghttp2_map *map,
  51. int (*func)(nghttp2_map_entry *entry, void *ptr),
  52. void *ptr);
  53. /*
  54. * Initializes the |entry| with the |key|. All entries to be inserted
  55. * to the map must be initialized with this function.
  56. */
  57. void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key);
  58. /*
  59. * Inserts the new |entry| with the key |entry->key| to the map |map|.
  60. *
  61. * This function returns 0 if it succeeds, or one of the following
  62. * negative error codes:
  63. *
  64. * NGHTTP2_ERR_INVALID_ARGUMENT
  65. * The item associated by |key| already exists.
  66. * NGHTTP2_ERR_NOMEM
  67. * Out of memory
  68. */
  69. int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *entry);
  70. /*
  71. * Returns the entry associated by the key |key|. If there is no such
  72. * entry, this function returns NULL.
  73. */
  74. nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key);
  75. /*
  76. * Removes the entry associated by the key |key| from the |map|. The
  77. * 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. * NGHTTP2_ERR_INVALID_ARGUMENT
  83. * The entry associated by |key| does not exist.
  84. */
  85. int nghttp2_map_remove(nghttp2_map *map, key_type key);
  86. /*
  87. * Returns the number of items stored in the map |map|.
  88. */
  89. size_t nghttp2_map_size(nghttp2_map *map);
  90. /*
  91. * Applies the function |func| to each entry in the |map| with the
  92. * optional user supplied pointer |ptr|.
  93. *
  94. * If the |func| returns 0, this function calls the |func| with the
  95. * next entry. If the |func| returns nonzero, it will not call the
  96. * |func| for further entries and return the return value of the
  97. * |func| immediately. Thus, this function returns 0 if all the
  98. * invocations of the |func| return 0, or nonzero value which the last
  99. * invocation of |func| returns.
  100. *
  101. * Don't use this function to free each entry. Use
  102. * nghttp2_map_each_free() instead.
  103. */
  104. int nghttp2_map_each(nghttp2_map *map,
  105. int (*func)(nghttp2_map_entry *entry, void *ptr),
  106. void *ptr);
  107. #endif /* NGHTTP2_MAP_H */