xref: /third_party/node/deps/nghttp2/lib/nghttp2_pq.c (revision 1cb0ef41)
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#include "nghttp2_pq.h"
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include <stdio.h>
281cb0ef41Sopenharmony_ci#include <assert.h>
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#include "nghttp2_helper.h"
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_civoid nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem) {
331cb0ef41Sopenharmony_ci  pq->mem = mem;
341cb0ef41Sopenharmony_ci  pq->capacity = 0;
351cb0ef41Sopenharmony_ci  pq->q = NULL;
361cb0ef41Sopenharmony_ci  pq->length = 0;
371cb0ef41Sopenharmony_ci  pq->less = less;
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_civoid nghttp2_pq_free(nghttp2_pq *pq) {
411cb0ef41Sopenharmony_ci  nghttp2_mem_free(pq->mem, pq->q);
421cb0ef41Sopenharmony_ci  pq->q = NULL;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cistatic void swap(nghttp2_pq *pq, size_t i, size_t j) {
461cb0ef41Sopenharmony_ci  nghttp2_pq_entry *a = pq->q[i];
471cb0ef41Sopenharmony_ci  nghttp2_pq_entry *b = pq->q[j];
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  pq->q[i] = b;
501cb0ef41Sopenharmony_ci  b->index = i;
511cb0ef41Sopenharmony_ci  pq->q[j] = a;
521cb0ef41Sopenharmony_ci  a->index = j;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cistatic void bubble_up(nghttp2_pq *pq, size_t index) {
561cb0ef41Sopenharmony_ci  size_t parent;
571cb0ef41Sopenharmony_ci  while (index != 0) {
581cb0ef41Sopenharmony_ci    parent = (index - 1) / 2;
591cb0ef41Sopenharmony_ci    if (!pq->less(pq->q[index], pq->q[parent])) {
601cb0ef41Sopenharmony_ci      return;
611cb0ef41Sopenharmony_ci    }
621cb0ef41Sopenharmony_ci    swap(pq, parent, index);
631cb0ef41Sopenharmony_ci    index = parent;
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciint nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item) {
681cb0ef41Sopenharmony_ci  if (pq->capacity <= pq->length) {
691cb0ef41Sopenharmony_ci    void *nq;
701cb0ef41Sopenharmony_ci    size_t ncapacity;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    ncapacity = nghttp2_max(4, (pq->capacity * 2));
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    nq = nghttp2_mem_realloc(pq->mem, pq->q,
751cb0ef41Sopenharmony_ci                             ncapacity * sizeof(nghttp2_pq_entry *));
761cb0ef41Sopenharmony_ci    if (nq == NULL) {
771cb0ef41Sopenharmony_ci      return NGHTTP2_ERR_NOMEM;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci    pq->capacity = ncapacity;
801cb0ef41Sopenharmony_ci    pq->q = nq;
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci  pq->q[pq->length] = item;
831cb0ef41Sopenharmony_ci  item->index = pq->length;
841cb0ef41Sopenharmony_ci  ++pq->length;
851cb0ef41Sopenharmony_ci  bubble_up(pq, pq->length - 1);
861cb0ef41Sopenharmony_ci  return 0;
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_cinghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) {
901cb0ef41Sopenharmony_ci  if (pq->length == 0) {
911cb0ef41Sopenharmony_ci    return NULL;
921cb0ef41Sopenharmony_ci  } else {
931cb0ef41Sopenharmony_ci    return pq->q[0];
941cb0ef41Sopenharmony_ci  }
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cistatic void bubble_down(nghttp2_pq *pq, size_t index) {
981cb0ef41Sopenharmony_ci  size_t i, j, minindex;
991cb0ef41Sopenharmony_ci  for (;;) {
1001cb0ef41Sopenharmony_ci    j = index * 2 + 1;
1011cb0ef41Sopenharmony_ci    minindex = index;
1021cb0ef41Sopenharmony_ci    for (i = 0; i < 2; ++i, ++j) {
1031cb0ef41Sopenharmony_ci      if (j >= pq->length) {
1041cb0ef41Sopenharmony_ci        break;
1051cb0ef41Sopenharmony_ci      }
1061cb0ef41Sopenharmony_ci      if (pq->less(pq->q[j], pq->q[minindex])) {
1071cb0ef41Sopenharmony_ci        minindex = j;
1081cb0ef41Sopenharmony_ci      }
1091cb0ef41Sopenharmony_ci    }
1101cb0ef41Sopenharmony_ci    if (minindex == index) {
1111cb0ef41Sopenharmony_ci      return;
1121cb0ef41Sopenharmony_ci    }
1131cb0ef41Sopenharmony_ci    swap(pq, index, minindex);
1141cb0ef41Sopenharmony_ci    index = minindex;
1151cb0ef41Sopenharmony_ci  }
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_civoid nghttp2_pq_pop(nghttp2_pq *pq) {
1191cb0ef41Sopenharmony_ci  if (pq->length > 0) {
1201cb0ef41Sopenharmony_ci    pq->q[0] = pq->q[pq->length - 1];
1211cb0ef41Sopenharmony_ci    pq->q[0]->index = 0;
1221cb0ef41Sopenharmony_ci    --pq->length;
1231cb0ef41Sopenharmony_ci    bubble_down(pq, 0);
1241cb0ef41Sopenharmony_ci  }
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_civoid nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) {
1281cb0ef41Sopenharmony_ci  assert(pq->q[item->index] == item);
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  if (item->index == 0) {
1311cb0ef41Sopenharmony_ci    nghttp2_pq_pop(pq);
1321cb0ef41Sopenharmony_ci    return;
1331cb0ef41Sopenharmony_ci  }
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  if (item->index == pq->length - 1) {
1361cb0ef41Sopenharmony_ci    --pq->length;
1371cb0ef41Sopenharmony_ci    return;
1381cb0ef41Sopenharmony_ci  }
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  pq->q[item->index] = pq->q[pq->length - 1];
1411cb0ef41Sopenharmony_ci  pq->q[item->index]->index = item->index;
1421cb0ef41Sopenharmony_ci  --pq->length;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  if (pq->less(item, pq->q[item->index])) {
1451cb0ef41Sopenharmony_ci    bubble_down(pq, item->index);
1461cb0ef41Sopenharmony_ci  } else {
1471cb0ef41Sopenharmony_ci    bubble_up(pq, item->index);
1481cb0ef41Sopenharmony_ci  }
1491cb0ef41Sopenharmony_ci}
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ciint nghttp2_pq_empty(nghttp2_pq *pq) { return pq->length == 0; }
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_cisize_t nghttp2_pq_size(nghttp2_pq *pq) { return pq->length; }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_civoid nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) {
1561cb0ef41Sopenharmony_ci  size_t i;
1571cb0ef41Sopenharmony_ci  int rv = 0;
1581cb0ef41Sopenharmony_ci  if (pq->length == 0) {
1591cb0ef41Sopenharmony_ci    return;
1601cb0ef41Sopenharmony_ci  }
1611cb0ef41Sopenharmony_ci  for (i = 0; i < pq->length; ++i) {
1621cb0ef41Sopenharmony_ci    rv |= (*fun)(pq->q[i], arg);
1631cb0ef41Sopenharmony_ci  }
1641cb0ef41Sopenharmony_ci  if (rv) {
1651cb0ef41Sopenharmony_ci    for (i = pq->length; i > 0; --i) {
1661cb0ef41Sopenharmony_ci      bubble_down(pq, i - 1);
1671cb0ef41Sopenharmony_ci    }
1681cb0ef41Sopenharmony_ci  }
1691cb0ef41Sopenharmony_ci}
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ciint nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) {
1721cb0ef41Sopenharmony_ci  size_t i;
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  if (pq->length == 0) {
1751cb0ef41Sopenharmony_ci    return 0;
1761cb0ef41Sopenharmony_ci  }
1771cb0ef41Sopenharmony_ci  for (i = 0; i < pq->length; ++i) {
1781cb0ef41Sopenharmony_ci    if ((*fun)(pq->q[i], arg)) {
1791cb0ef41Sopenharmony_ci      return 1;
1801cb0ef41Sopenharmony_ci    }
1811cb0ef41Sopenharmony_ci  }
1821cb0ef41Sopenharmony_ci  return 0;
1831cb0ef41Sopenharmony_ci}
184