nghttp2_map.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include "nghttp2_map.h"
  5. #include <string.h>
  6. #define INITIAL_TABLE_LENGTH 256
  7. int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
  8. map->mem = mem;
  9. map->tablelen = INITIAL_TABLE_LENGTH;
  10. map->table =
  11. nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_entry *));
  12. if (map->table == NULL) {
  13. return NGHTTP2_ERR_NOMEM;
  14. }
  15. map->size = 0;
  16. return 0;
  17. }
  18. void nghttp2_map_free(nghttp2_map *map) {
  19. nghttp2_mem_free(map->mem, map->table);
  20. }
  21. void nghttp2_map_each_free(nghttp2_map *map,
  22. int (*func)(nghttp2_map_entry *entry, void *ptr),
  23. void *ptr) {
  24. uint32_t i;
  25. for (i = 0; i < map->tablelen; ++i) {
  26. nghttp2_map_entry *entry;
  27. for (entry = map->table[i]; entry;) {
  28. nghttp2_map_entry *next = entry->next;
  29. func(entry, ptr);
  30. entry = next;
  31. }
  32. map->table[i] = NULL;
  33. }
  34. }
  35. int nghttp2_map_each(nghttp2_map *map,
  36. int (*func)(nghttp2_map_entry *entry, void *ptr),
  37. void *ptr) {
  38. int rv;
  39. uint32_t i;
  40. for (i = 0; i < map->tablelen; ++i) {
  41. nghttp2_map_entry *entry;
  42. for (entry = map->table[i]; entry; entry = entry->next) {
  43. rv = func(entry, ptr);
  44. if (rv != 0) {
  45. return rv;
  46. }
  47. }
  48. }
  49. return 0;
  50. }
  51. void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key) {
  52. entry->key = key;
  53. entry->next = NULL;
  54. }
  55. /* Same hash function in android HashMap source code. */
  56. /* The |mod| must be power of 2 */
  57. static uint32_t hash(int32_t key, uint32_t mod) {
  58. uint32_t h = (uint32_t)key;
  59. h ^= (h >> 20) ^ (h >> 12);
  60. h ^= (h >> 7) ^ (h >> 4);
  61. return h & (mod - 1);
  62. }
  63. static int insert(nghttp2_map_entry **table, uint32_t tablelen,
  64. nghttp2_map_entry *entry) {
  65. uint32_t h = hash(entry->key, tablelen);
  66. if (table[h] == NULL) {
  67. table[h] = entry;
  68. } else {
  69. nghttp2_map_entry *p;
  70. /* We won't allow duplicated key, so check it out. */
  71. for (p = table[h]; p; p = p->next) {
  72. if (p->key == entry->key) {
  73. return NGHTTP2_ERR_INVALID_ARGUMENT;
  74. }
  75. }
  76. entry->next = table[h];
  77. table[h] = entry;
  78. }
  79. return 0;
  80. }
  81. /* new_tablelen must be power of 2 */
  82. static int resize(nghttp2_map *map, uint32_t new_tablelen) {
  83. uint32_t i;
  84. nghttp2_map_entry **new_table;
  85. new_table =
  86. nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_entry *));
  87. if (new_table == NULL) {
  88. return NGHTTP2_ERR_NOMEM;
  89. }
  90. for (i = 0; i < map->tablelen; ++i) {
  91. nghttp2_map_entry *entry;
  92. for (entry = map->table[i]; entry;) {
  93. nghttp2_map_entry *next = entry->next;
  94. entry->next = NULL;
  95. /* This function must succeed */
  96. insert(new_table, new_tablelen, entry);
  97. entry = next;
  98. }
  99. }
  100. nghttp2_mem_free(map->mem, map->table);
  101. map->tablelen = new_tablelen;
  102. map->table = new_table;
  103. return 0;
  104. }
  105. int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) {
  106. int rv;
  107. /* Load factor is 0.75 */
  108. if ((map->size + 1) * 4 > map->tablelen * 3) {
  109. rv = resize(map, map->tablelen * 2);
  110. if (rv != 0) {
  111. return rv;
  112. }
  113. }
  114. rv = insert(map->table, map->tablelen, new_entry);
  115. if (rv != 0) {
  116. return rv;
  117. }
  118. ++map->size;
  119. return 0;
  120. }
  121. nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key) {
  122. uint32_t h;
  123. nghttp2_map_entry *entry;
  124. h = hash(key, map->tablelen);
  125. for (entry = map->table[h]; entry; entry = entry->next) {
  126. if (entry->key == key) {
  127. return entry;
  128. }
  129. }
  130. return NULL;
  131. }
  132. int nghttp2_map_remove(nghttp2_map *map, key_type key) {
  133. uint32_t h;
  134. nghttp2_map_entry **dst;
  135. h = hash(key, map->tablelen);
  136. for (dst = &map->table[h]; *dst; dst = &(*dst)->next) {
  137. if ((*dst)->key != key) {
  138. continue;
  139. }
  140. *dst = (*dst)->next;
  141. --map->size;
  142. return 0;
  143. }
  144. return NGHTTP2_ERR_INVALID_ARGUMENT;
  145. }
  146. size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }