11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * nghttp2 - HTTP/2 C Library 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Copyright (c) 2012 Tatsuhiro Tsujikawa 51cb0ef41Sopenharmony_ci * 61cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 71cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the 81cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 91cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 101cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 111cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 121cb0ef41Sopenharmony_ci * the following conditions: 131cb0ef41Sopenharmony_ci * 141cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be 151cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software. 161cb0ef41Sopenharmony_ci * 171cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 181cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 191cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 201cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 211cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 221cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 231cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 241cb0ef41Sopenharmony_ci */ 251cb0ef41Sopenharmony_ci#ifndef NGHTTP2_PQ_H 261cb0ef41Sopenharmony_ci#define NGHTTP2_PQ_H 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci#ifdef HAVE_CONFIG_H 291cb0ef41Sopenharmony_ci# include <config.h> 301cb0ef41Sopenharmony_ci#endif /* HAVE_CONFIG_H */ 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci#include <nghttp2/nghttp2.h> 331cb0ef41Sopenharmony_ci#include "nghttp2_int.h" 341cb0ef41Sopenharmony_ci#include "nghttp2_mem.h" 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci/* Implementation of priority queue */ 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_citypedef struct { 391cb0ef41Sopenharmony_ci size_t index; 401cb0ef41Sopenharmony_ci} nghttp2_pq_entry; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_citypedef struct { 431cb0ef41Sopenharmony_ci /* The pointer to the pointer to the item stored */ 441cb0ef41Sopenharmony_ci nghttp2_pq_entry **q; 451cb0ef41Sopenharmony_ci /* Memory allocator */ 461cb0ef41Sopenharmony_ci nghttp2_mem *mem; 471cb0ef41Sopenharmony_ci /* The number of items stored */ 481cb0ef41Sopenharmony_ci size_t length; 491cb0ef41Sopenharmony_ci /* The maximum number of items this pq can store. This is 501cb0ef41Sopenharmony_ci automatically extended when length is reached to this value. */ 511cb0ef41Sopenharmony_ci size_t capacity; 521cb0ef41Sopenharmony_ci /* The less function between items */ 531cb0ef41Sopenharmony_ci nghttp2_less less; 541cb0ef41Sopenharmony_ci} nghttp2_pq; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci/* 571cb0ef41Sopenharmony_ci * Initializes priority queue |pq| with compare function |cmp|. 581cb0ef41Sopenharmony_ci */ 591cb0ef41Sopenharmony_civoid nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci/* 621cb0ef41Sopenharmony_ci * Deallocates any resources allocated for |pq|. The stored items are 631cb0ef41Sopenharmony_ci * not freed by this function. 641cb0ef41Sopenharmony_ci */ 651cb0ef41Sopenharmony_civoid nghttp2_pq_free(nghttp2_pq *pq); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci/* 681cb0ef41Sopenharmony_ci * Adds |item| to the priority queue |pq|. 691cb0ef41Sopenharmony_ci * 701cb0ef41Sopenharmony_ci * This function returns 0 if it succeeds, or one of the following 711cb0ef41Sopenharmony_ci * negative error codes: 721cb0ef41Sopenharmony_ci * 731cb0ef41Sopenharmony_ci * NGHTTP2_ERR_NOMEM 741cb0ef41Sopenharmony_ci * Out of memory. 751cb0ef41Sopenharmony_ci */ 761cb0ef41Sopenharmony_ciint nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci/* 791cb0ef41Sopenharmony_ci * Returns item at the top of the queue |pq|. If the queue is empty, 801cb0ef41Sopenharmony_ci * this function returns NULL. 811cb0ef41Sopenharmony_ci */ 821cb0ef41Sopenharmony_cinghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci/* 851cb0ef41Sopenharmony_ci * Pops item at the top of the queue |pq|. The popped item is not 861cb0ef41Sopenharmony_ci * freed by this function. 871cb0ef41Sopenharmony_ci */ 881cb0ef41Sopenharmony_civoid nghttp2_pq_pop(nghttp2_pq *pq); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci/* 911cb0ef41Sopenharmony_ci * Returns nonzero if the queue |pq| is empty. 921cb0ef41Sopenharmony_ci */ 931cb0ef41Sopenharmony_ciint nghttp2_pq_empty(nghttp2_pq *pq); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci/* 961cb0ef41Sopenharmony_ci * Returns the number of items in the queue |pq|. 971cb0ef41Sopenharmony_ci */ 981cb0ef41Sopenharmony_cisize_t nghttp2_pq_size(nghttp2_pq *pq); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_citypedef int (*nghttp2_pq_item_cb)(nghttp2_pq_entry *item, void *arg); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci/* 1031cb0ef41Sopenharmony_ci * Updates each item in |pq| using function |fun| and re-construct 1041cb0ef41Sopenharmony_ci * priority queue. The |fun| must return non-zero if it modifies the 1051cb0ef41Sopenharmony_ci * item in a way that it affects ordering in the priority queue. The 1061cb0ef41Sopenharmony_ci * |arg| is passed to the 2nd parameter of |fun|. 1071cb0ef41Sopenharmony_ci */ 1081cb0ef41Sopenharmony_civoid nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci/* 1111cb0ef41Sopenharmony_ci * Applies |fun| to each item in |pq|. The |arg| is passed as arg 1121cb0ef41Sopenharmony_ci * parameter to callback function. This function must not change the 1131cb0ef41Sopenharmony_ci * ordering key. If the return value from callback is nonzero, this 1141cb0ef41Sopenharmony_ci * function returns 1 immediately without iterating remaining items. 1151cb0ef41Sopenharmony_ci * Otherwise this function returns 0. 1161cb0ef41Sopenharmony_ci */ 1171cb0ef41Sopenharmony_ciint nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci/* 1201cb0ef41Sopenharmony_ci * Removes |item| from priority queue. 1211cb0ef41Sopenharmony_ci */ 1221cb0ef41Sopenharmony_civoid nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci#endif /* NGHTTP2_PQ_H */ 125