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