nghttp2_map.c 5.0 KB

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