• 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 hash;
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   uint32_t tablelen;
52   uint32_t tablelenbits;
53 } nghttp2_map;
54 
55 /*
56  * Initializes the map |map|.
57  *
58  * This function returns 0 if it succeeds, or one of the following
59  * negative error codes:
60  *
61  * NGHTTP2_ERR_NOMEM
62  *   Out of memory
63  */
64 int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem);
65 
66 /*
67  * Deallocates any resources allocated for |map|. The stored entries
68  * are not freed by this function. Use nghttp2_map_each_free() to free
69  * each entries.
70  */
71 void nghttp2_map_free(nghttp2_map *map);
72 
73 /*
74  * Deallocates each entries using |func| function and any resources
75  * allocated for |map|. The |func| function is responsible for freeing
76  * given the |data| object. The |ptr| will be passed to the |func| as
77  * send argument. The return value of the |func| will be ignored.
78  */
79 void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),
80                            void *ptr);
81 
82 /*
83  * Inserts the new |data| with the |key| to the map |map|.
84  *
85  * This function returns 0 if it succeeds, or one of the following
86  * negative error codes:
87  *
88  * NGHTTP2_ERR_INVALID_ARGUMENT
89  *     The item associated by |key| already exists.
90  * NGHTTP2_ERR_NOMEM
91  *   Out of memory
92  */
93 int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data);
94 
95 /*
96  * Returns the data associated by the key |key|.  If there is no such
97  * data, this function returns NULL.
98  */
99 void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key);
100 
101 /*
102  * Removes the data associated by the key |key| from the |map|.  The
103  * removed data is not freed by this function.
104  *
105  * This function returns 0 if it succeeds, or one of the following
106  * negative error codes:
107  *
108  * NGHTTP2_ERR_INVALID_ARGUMENT
109  *     The data associated by |key| does not exist.
110  */
111 int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key);
112 
113 /*
114  * Removes all entries from |map|.
115  */
116 void nghttp2_map_clear(nghttp2_map *map);
117 
118 /*
119  * Returns the number of items stored in the map |map|.
120  */
121 size_t nghttp2_map_size(nghttp2_map *map);
122 
123 /*
124  * Applies the function |func| to each data in the |map| with the
125  * optional user supplied pointer |ptr|.
126  *
127  * If the |func| returns 0, this function calls the |func| with the
128  * next data.  If the |func| returns nonzero, it will not call the
129  * |func| for further entries and return the return value of the
130  * |func| immediately.  Thus, this function returns 0 if all the
131  * invocations of the |func| return 0, or nonzero value which the last
132  * invocation of |func| returns.
133  *
134  * Don't use this function to free each data. Use
135  * nghttp2_map_each_free() instead.
136  */
137 int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),
138                      void *ptr);
139 
140 void nghttp2_map_print_distance(nghttp2_map *map);
141 
142 #endif /* NGHTTP2_MAP_H */
143