/*1* nghttp2 - HTTP/2 C Library2*3* Copyright (c) 2017 ngtcp2 contributors4* Copyright (c) 2012 nghttp2 contributors5*6* Permission is hereby granted, free of charge, to any person obtaining7* a copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sublicense, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice shall be15* 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 OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND20* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE21* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION22* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION23* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*/25#ifndef NGHTTP2_MAP_H26#define NGHTTP2_MAP_H2728#ifdef HAVE_CONFIG_H29# include <config.h>30#endif /* HAVE_CONFIG_H */3132#include <nghttp2/nghttp2.h>3334#include "nghttp2_mem.h"3536/* Implementation of unordered map */3738typedef int32_t nghttp2_map_key_type;3940typedef struct nghttp2_map_bucket {41uint32_t hash;42nghttp2_map_key_type key;43void *data;44} nghttp2_map_bucket;4546typedef struct nghttp2_map {47nghttp2_map_bucket *table;48nghttp2_mem *mem;49size_t size;50uint32_t tablelen;51uint32_t tablelenbits;52} nghttp2_map;5354/*55* Initializes the map |map|.56*57* This function returns 0 if it succeeds, or one of the following58* negative error codes:59*60* NGHTTP2_ERR_NOMEM61* Out of memory62*/63int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem);6465/*66* Deallocates any resources allocated for |map|. The stored entries67* are not freed by this function. Use nghttp2_map_each_free() to free68* each entries.69*/70void nghttp2_map_free(nghttp2_map *map);7172/*73* Deallocates each entries using |func| function and any resources74* allocated for |map|. The |func| function is responsible for freeing75* given the |data| object. The |ptr| will be passed to the |func| as76* send argument. The return value of the |func| will be ignored.77*/78void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),79void *ptr);8081/*82* Inserts the new |data| with the |key| to the map |map|.83*84* This function returns 0 if it succeeds, or one of the following85* negative error codes:86*87* NGHTTP2_ERR_INVALID_ARGUMENT88* The item associated by |key| already exists.89* NGHTTP2_ERR_NOMEM90* Out of memory91*/92int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data);9394/*95* Returns the data associated by the key |key|. If there is no such96* data, this function returns NULL.97*/98void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key);99100/*101* Removes the data associated by the key |key| from the |map|. The102* removed data is not freed by this function.103*104* This function returns 0 if it succeeds, or one of the following105* negative error codes:106*107* NGHTTP2_ERR_INVALID_ARGUMENT108* The data associated by |key| does not exist.109*/110int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key);111112/*113* Removes all entries from |map|.114*/115void nghttp2_map_clear(nghttp2_map *map);116117/*118* Returns the number of items stored in the map |map|.119*/120size_t nghttp2_map_size(nghttp2_map *map);121122/*123* Applies the function |func| to each data in the |map| with the124* optional user supplied pointer |ptr|.125*126* If the |func| returns 0, this function calls the |func| with the127* next data. If the |func| returns nonzero, it will not call the128* |func| for further entries and return the return value of the129* |func| immediately. Thus, this function returns 0 if all the130* invocations of the |func| return 0, or nonzero value which the last131* invocation of |func| returns.132*133* Don't use this function to free each data. Use134* nghttp2_map_each_free() instead.135*/136int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),137void *ptr);138139void nghttp2_map_print_distance(nghttp2_map *map);140141#endif /* NGHTTP2_MAP_H */142143144