• 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 #include "nghttp2_ksl.h"
37 
38 /* Implementation of unordered map */
39 
40 typedef int32_t key_type;
41 
42 typedef struct nghttp2_map_entry {
43   struct nghttp2_map_entry *next;
44   key_type key;
45 #if SIZEOF_INT_P == 4
46   /* we requires 8 bytes aligment */
47   int64_t pad;
48 #endif
49 } nghttp2_map_entry;
50 
51 typedef struct nghttp2_map_bucket {
52   nghttp2_map_entry *ptr;
53   nghttp2_ksl *ksl;
54 } nghttp2_map_bucket;
55 
56 typedef struct {
57   nghttp2_map_bucket *table;
58   nghttp2_mem *mem;
59   size_t size;
60   uint32_t tablelen;
61 } nghttp2_map;
62 
63 /*
64  * Initializes the map |map|.
65  *
66  * This function returns 0 if it succeeds, or one of the following
67  * negative error codes:
68  *
69  * NGHTTP2_ERR_NOMEM
70  *   Out of memory
71  */
72 int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem);
73 
74 /*
75  * Deallocates any resources allocated for |map|. The stored entries
76  * are not freed by this function. Use nghttp2_map_each_free() to free
77  * each entries.
78  */
79 void nghttp2_map_free(nghttp2_map *map);
80 
81 /*
82  * Deallocates each entries using |func| function and any resources
83  * allocated for |map|. The |func| function is responsible for freeing
84  * given the |entry| object. The |ptr| will be passed to the |func| as
85  * send argument. The return value of the |func| will be ignored.
86  */
87 void nghttp2_map_each_free(nghttp2_map *map,
88                            int (*func)(nghttp2_map_entry *entry, void *ptr),
89                            void *ptr);
90 
91 /*
92  * Initializes the |entry| with the |key|. All entries to be inserted
93  * to the map must be initialized with this function.
94  */
95 void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key);
96 
97 /*
98  * Inserts the new |entry| with the key |entry->key| to the map |map|.
99  *
100  * This function returns 0 if it succeeds, or one of the following
101  * negative error codes:
102  *
103  * NGHTTP2_ERR_INVALID_ARGUMENT
104  *     The item associated by |key| already exists.
105  * NGHTTP2_ERR_NOMEM
106  *   Out of memory
107  */
108 int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *entry);
109 
110 /*
111  * Returns the entry associated by the key |key|.  If there is no such
112  * entry, this function returns NULL.
113  */
114 nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key);
115 
116 /*
117  * Removes the entry associated by the key |key| from the |map|.  The
118  * removed entry is not freed by this function.
119  *
120  * This function returns 0 if it succeeds, or one of the following
121  * negative error codes:
122  *
123  * NGHTTP2_ERR_INVALID_ARGUMENT
124  *     The entry associated by |key| does not exist.
125  */
126 int nghttp2_map_remove(nghttp2_map *map, key_type key);
127 
128 /*
129  * Removes all entries from |map|.
130  */
131 void nghttp2_map_clear(nghttp2_map *map);
132 
133 /*
134  * Returns the number of items stored in the map |map|.
135  */
136 size_t nghttp2_map_size(nghttp2_map *map);
137 
138 /*
139  * Applies the function |func| to each entry in the |map| with the
140  * optional user supplied pointer |ptr|.
141  *
142  * If the |func| returns 0, this function calls the |func| with the
143  * next entry. If the |func| returns nonzero, it will not call the
144  * |func| for further entries and return the return value of the
145  * |func| immediately.  Thus, this function returns 0 if all the
146  * invocations of the |func| return 0, or nonzero value which the last
147  * invocation of |func| returns.
148  *
149  * Don't use this function to free each entry. Use
150  * nghttp2_map_each_free() instead.
151  */
152 int nghttp2_map_each(nghttp2_map *map,
153                      int (*func)(nghttp2_map_entry *entry, void *ptr),
154                      void *ptr);
155 
156 #endif /* NGHTTP2_MAP_H */
157