11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * nghttp2 - HTTP/2 C Library 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Copyright (c) 2017 ngtcp2 contributors 51cb0ef41Sopenharmony_ci * Copyright (c) 2012 nghttp2 contributors 61cb0ef41Sopenharmony_ci * 71cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 81cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the 91cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 101cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 111cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 121cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 131cb0ef41Sopenharmony_ci * the following conditions: 141cb0ef41Sopenharmony_ci * 151cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be 161cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software. 171cb0ef41Sopenharmony_ci * 181cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 191cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 201cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 211cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 221cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 231cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 241cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci#ifndef NGHTTP2_MAP_H 271cb0ef41Sopenharmony_ci#define NGHTTP2_MAP_H 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci#ifdef HAVE_CONFIG_H 301cb0ef41Sopenharmony_ci# include <config.h> 311cb0ef41Sopenharmony_ci#endif /* HAVE_CONFIG_H */ 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci#include <nghttp2/nghttp2.h> 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci#include "nghttp2_mem.h" 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci/* Implementation of unordered map */ 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_citypedef int32_t nghttp2_map_key_type; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_citypedef struct nghttp2_map_bucket { 421cb0ef41Sopenharmony_ci uint32_t hash; 431cb0ef41Sopenharmony_ci nghttp2_map_key_type key; 441cb0ef41Sopenharmony_ci void *data; 451cb0ef41Sopenharmony_ci} nghttp2_map_bucket; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_citypedef struct nghttp2_map { 481cb0ef41Sopenharmony_ci nghttp2_map_bucket *table; 491cb0ef41Sopenharmony_ci nghttp2_mem *mem; 501cb0ef41Sopenharmony_ci size_t size; 511cb0ef41Sopenharmony_ci uint32_t tablelen; 521cb0ef41Sopenharmony_ci uint32_t tablelenbits; 531cb0ef41Sopenharmony_ci} nghttp2_map; 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci/* 561cb0ef41Sopenharmony_ci * Initializes the map |map|. 571cb0ef41Sopenharmony_ci */ 581cb0ef41Sopenharmony_civoid nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci/* 611cb0ef41Sopenharmony_ci * Deallocates any resources allocated for |map|. The stored entries 621cb0ef41Sopenharmony_ci * are not freed by this function. Use nghttp2_map_each_free() to free 631cb0ef41Sopenharmony_ci * each entries. 641cb0ef41Sopenharmony_ci */ 651cb0ef41Sopenharmony_civoid nghttp2_map_free(nghttp2_map *map); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci/* 681cb0ef41Sopenharmony_ci * Deallocates each entries using |func| function and any resources 691cb0ef41Sopenharmony_ci * allocated for |map|. The |func| function is responsible for freeing 701cb0ef41Sopenharmony_ci * given the |data| object. The |ptr| will be passed to the |func| as 711cb0ef41Sopenharmony_ci * send argument. The return value of the |func| will be ignored. 721cb0ef41Sopenharmony_ci */ 731cb0ef41Sopenharmony_civoid nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr), 741cb0ef41Sopenharmony_ci void *ptr); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci/* 771cb0ef41Sopenharmony_ci * Inserts the new |data| with the |key| to the map |map|. 781cb0ef41Sopenharmony_ci * 791cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or one of the following 801cb0ef41Sopenharmony_ci * negative error codes: 811cb0ef41Sopenharmony_ci * 821cb0ef41Sopenharmony_ci * NGHTTP2_ERR_INVALID_ARGUMENT 831cb0ef41Sopenharmony_ci * The item associated by |key| already exists. 841cb0ef41Sopenharmony_ci * NGHTTP2_ERR_NOMEM 851cb0ef41Sopenharmony_ci * Out of memory 861cb0ef41Sopenharmony_ci */ 871cb0ef41Sopenharmony_ciint nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci/* 901cb0ef41Sopenharmony_ci * Returns the data associated by the key |key|. If there is no such 911cb0ef41Sopenharmony_ci * data, this function returns NULL. 921cb0ef41Sopenharmony_ci */ 931cb0ef41Sopenharmony_civoid *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci/* 961cb0ef41Sopenharmony_ci * Removes the data associated by the key |key| from the |map|. The 971cb0ef41Sopenharmony_ci * removed data is not freed by this function. 981cb0ef41Sopenharmony_ci * 991cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or one of the following 1001cb0ef41Sopenharmony_ci * negative error codes: 1011cb0ef41Sopenharmony_ci * 1021cb0ef41Sopenharmony_ci * NGHTTP2_ERR_INVALID_ARGUMENT 1031cb0ef41Sopenharmony_ci * The data associated by |key| does not exist. 1041cb0ef41Sopenharmony_ci */ 1051cb0ef41Sopenharmony_ciint nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci/* 1081cb0ef41Sopenharmony_ci * Removes all entries from |map|. 1091cb0ef41Sopenharmony_ci */ 1101cb0ef41Sopenharmony_civoid nghttp2_map_clear(nghttp2_map *map); 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci/* 1131cb0ef41Sopenharmony_ci * Returns the number of items stored in the map |map|. 1141cb0ef41Sopenharmony_ci */ 1151cb0ef41Sopenharmony_cisize_t nghttp2_map_size(nghttp2_map *map); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci/* 1181cb0ef41Sopenharmony_ci * Applies the function |func| to each data in the |map| with the 1191cb0ef41Sopenharmony_ci * optional user supplied pointer |ptr|. 1201cb0ef41Sopenharmony_ci * 1211cb0ef41Sopenharmony_ci * If the |func| returns 0, this function calls the |func| with the 1221cb0ef41Sopenharmony_ci * next data. If the |func| returns nonzero, it will not call the 1231cb0ef41Sopenharmony_ci * |func| for further entries and return the return value of the 1241cb0ef41Sopenharmony_ci * |func| immediately. Thus, this function returns 0 if all the 1251cb0ef41Sopenharmony_ci * invocations of the |func| return 0, or nonzero value which the last 1261cb0ef41Sopenharmony_ci * invocation of |func| returns. 1271cb0ef41Sopenharmony_ci * 1281cb0ef41Sopenharmony_ci * Don't use this function to free each data. Use 1291cb0ef41Sopenharmony_ci * nghttp2_map_each_free() instead. 1301cb0ef41Sopenharmony_ci */ 1311cb0ef41Sopenharmony_ciint nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr), 1321cb0ef41Sopenharmony_ci void *ptr); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_civoid nghttp2_map_print_distance(nghttp2_map *map); 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci#endif /* NGHTTP2_MAP_H */ 137