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