12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2012 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifndef NGHTTP2_PQ_H
262c593315Sopenharmony_ci#define NGHTTP2_PQ_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#ifdef HAVE_CONFIG_H
292c593315Sopenharmony_ci#  include <config.h>
302c593315Sopenharmony_ci#endif /* HAVE_CONFIG_H */
312c593315Sopenharmony_ci
322c593315Sopenharmony_ci#include <nghttp2/nghttp2.h>
332c593315Sopenharmony_ci#include "nghttp2_int.h"
342c593315Sopenharmony_ci#include "nghttp2_mem.h"
352c593315Sopenharmony_ci
362c593315Sopenharmony_ci/* Implementation of priority queue */
372c593315Sopenharmony_ci
382c593315Sopenharmony_citypedef struct {
392c593315Sopenharmony_ci  size_t index;
402c593315Sopenharmony_ci} nghttp2_pq_entry;
412c593315Sopenharmony_ci
422c593315Sopenharmony_citypedef struct {
432c593315Sopenharmony_ci  /* The pointer to the pointer to the item stored */
442c593315Sopenharmony_ci  nghttp2_pq_entry **q;
452c593315Sopenharmony_ci  /* Memory allocator */
462c593315Sopenharmony_ci  nghttp2_mem *mem;
472c593315Sopenharmony_ci  /* The number of items stored */
482c593315Sopenharmony_ci  size_t length;
492c593315Sopenharmony_ci  /* The maximum number of items this pq can store. This is
502c593315Sopenharmony_ci     automatically extended when length is reached to this value. */
512c593315Sopenharmony_ci  size_t capacity;
522c593315Sopenharmony_ci  /* The less function between items */
532c593315Sopenharmony_ci  nghttp2_less less;
542c593315Sopenharmony_ci} nghttp2_pq;
552c593315Sopenharmony_ci
562c593315Sopenharmony_ci/*
572c593315Sopenharmony_ci * Initializes priority queue |pq| with compare function |cmp|.
582c593315Sopenharmony_ci */
592c593315Sopenharmony_civoid nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem);
602c593315Sopenharmony_ci
612c593315Sopenharmony_ci/*
622c593315Sopenharmony_ci * Deallocates any resources allocated for |pq|.  The stored items are
632c593315Sopenharmony_ci * not freed by this function.
642c593315Sopenharmony_ci */
652c593315Sopenharmony_civoid nghttp2_pq_free(nghttp2_pq *pq);
662c593315Sopenharmony_ci
672c593315Sopenharmony_ci/*
682c593315Sopenharmony_ci * Adds |item| to the priority queue |pq|.
692c593315Sopenharmony_ci *
702c593315Sopenharmony_ci * This function returns 0 if it succeeds, or one of the following
712c593315Sopenharmony_ci * negative error codes:
722c593315Sopenharmony_ci *
732c593315Sopenharmony_ci * NGHTTP2_ERR_NOMEM
742c593315Sopenharmony_ci *     Out of memory.
752c593315Sopenharmony_ci */
762c593315Sopenharmony_ciint nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item);
772c593315Sopenharmony_ci
782c593315Sopenharmony_ci/*
792c593315Sopenharmony_ci * Returns item at the top of the queue |pq|. If the queue is empty,
802c593315Sopenharmony_ci * this function returns NULL.
812c593315Sopenharmony_ci */
822c593315Sopenharmony_cinghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq);
832c593315Sopenharmony_ci
842c593315Sopenharmony_ci/*
852c593315Sopenharmony_ci * Pops item at the top of the queue |pq|. The popped item is not
862c593315Sopenharmony_ci * freed by this function.
872c593315Sopenharmony_ci */
882c593315Sopenharmony_civoid nghttp2_pq_pop(nghttp2_pq *pq);
892c593315Sopenharmony_ci
902c593315Sopenharmony_ci/*
912c593315Sopenharmony_ci * Returns nonzero if the queue |pq| is empty.
922c593315Sopenharmony_ci */
932c593315Sopenharmony_ciint nghttp2_pq_empty(nghttp2_pq *pq);
942c593315Sopenharmony_ci
952c593315Sopenharmony_ci/*
962c593315Sopenharmony_ci * Returns the number of items in the queue |pq|.
972c593315Sopenharmony_ci */
982c593315Sopenharmony_cisize_t nghttp2_pq_size(nghttp2_pq *pq);
992c593315Sopenharmony_ci
1002c593315Sopenharmony_citypedef int (*nghttp2_pq_item_cb)(nghttp2_pq_entry *item, void *arg);
1012c593315Sopenharmony_ci
1022c593315Sopenharmony_ci/*
1032c593315Sopenharmony_ci * Updates each item in |pq| using function |fun| and re-construct
1042c593315Sopenharmony_ci * priority queue. The |fun| must return non-zero if it modifies the
1052c593315Sopenharmony_ci * item in a way that it affects ordering in the priority queue. The
1062c593315Sopenharmony_ci * |arg| is passed to the 2nd parameter of |fun|.
1072c593315Sopenharmony_ci */
1082c593315Sopenharmony_civoid nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg);
1092c593315Sopenharmony_ci
1102c593315Sopenharmony_ci/*
1112c593315Sopenharmony_ci * Applies |fun| to each item in |pq|.  The |arg| is passed as arg
1122c593315Sopenharmony_ci * parameter to callback function.  This function must not change the
1132c593315Sopenharmony_ci * ordering key.  If the return value from callback is nonzero, this
1142c593315Sopenharmony_ci * function returns 1 immediately without iterating remaining items.
1152c593315Sopenharmony_ci * Otherwise this function returns 0.
1162c593315Sopenharmony_ci */
1172c593315Sopenharmony_ciint nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg);
1182c593315Sopenharmony_ci
1192c593315Sopenharmony_ci/*
1202c593315Sopenharmony_ci * Removes |item| from priority queue.
1212c593315Sopenharmony_ci */
1222c593315Sopenharmony_civoid nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item);
1232c593315Sopenharmony_ci
1242c593315Sopenharmony_ci#endif /* NGHTTP2_PQ_H */
125