• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif /* HAVE_CONFIG_H */
32 
33 #include <nghttp2/nghttp2.h>
34 
35 #include "nghttp2_mem.h"
36 
37 /* Implementation of unordered map */
38 
39 typedef int32_t nghttp2_map_key_type;
40 
41 typedef struct nghttp2_map_bucket {
42   uint32_t psl;
43   nghttp2_map_key_type key;
44   void *data;
45 } nghttp2_map_bucket;
46 
47 typedef struct nghttp2_map {
48   nghttp2_map_bucket *table;
49   nghttp2_mem *mem;
50   size_t size;
51   size_t hashbits;
52 } nghttp2_map;
53 
54 /*
55  * nghttp2_map_init initializes the map |map|.
56  */
57 void nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem);
58 
59 /*
60  * nghttp2_map_free deallocates any resources allocated for |map|.
61  * The stored entries are not freed by this function.  Use
62  * nghttp2_map_each() to free each entry.
63  */
64 void nghttp2_map_free(nghttp2_map *map);
65 
66 /*
67  * nghttp2_map_insert inserts the new |data| with the |key| to the map
68  * |map|.
69  *
70  * This function returns 0 if it succeeds, or one of the following
71  * negative error codes:
72  *
73  * NGHTTP2_ERR_INVALID_ARGUMENT
74  *     The item associated by |key| already exists.
75  * NGHTTP2_ERR_NOMEM
76  *     Out of memory
77  */
78 int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data);
79 
80 /*
81  * nghttp2_map_find returns the entry associated by the key |key|.  If
82  * there is no such entry, this function returns NULL.
83  */
84 void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key);
85 
86 /*
87  * nghttp2_map_remove removes the entry associated by the key |key|
88  * from the |map|.  The removed entry is not freed by this function.
89  *
90  * This function returns 0 if it succeeds, or one of the following
91  * negative error codes:
92  *
93  * NGHTTP2_ERR_INVALID_ARGUMENT
94  *     The entry associated by |key| does not exist.
95  */
96 int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key);
97 
98 /*
99  * nghttp2_map_clear removes all entries from |map|.  The removed
100  * entry is not freed by this function.
101  */
102 void nghttp2_map_clear(nghttp2_map *map);
103 
104 /*
105  * nghttp2_map_size returns the number of items stored in the map
106  * |map|.
107  */
108 size_t nghttp2_map_size(const nghttp2_map *map);
109 
110 /*
111  * nghttp2_map_each applies the function |func| to each entry in the
112  * |map| with the optional user supplied pointer |ptr|.
113  *
114  * If the |func| returns 0, this function calls the |func| with the
115  * next entry.  If the |func| returns nonzero, it will not call the
116  * |func| for further entries and return the return value of the
117  * |func| immediately.  Thus, this function returns 0 if all the
118  * invocations of the |func| return 0, or nonzero value which the last
119  * invocation of |func| returns.
120  */
121 int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr),
122                      void *ptr);
123 
124 #ifndef WIN32
125 void nghttp2_map_print_distance(const nghttp2_map *map);
126 #endif /* !WIN32 */
127 
128 #endif /* NGHTTP2_MAP_H */
129