11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * nghttp3
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Copyright (c) 2019 nghttp3 contributors
51cb0ef41Sopenharmony_ci * Copyright (c) 2017 ngtcp2 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#include "nghttp3_idtr.h"
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#include <assert.h>
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_civoid nghttp3_idtr_init(nghttp3_idtr *idtr, int server, const nghttp3_mem *mem) {
311cb0ef41Sopenharmony_ci  nghttp3_gaptr_init(&idtr->gap, mem);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  idtr->server = server;
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_civoid nghttp3_idtr_free(nghttp3_idtr *idtr) {
371cb0ef41Sopenharmony_ci  if (idtr == NULL) {
381cb0ef41Sopenharmony_ci    return;
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  nghttp3_gaptr_free(&idtr->gap);
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci/*
451cb0ef41Sopenharmony_ci * id_from_stream_id translates |stream_id| to id space used by
461cb0ef41Sopenharmony_ci * nghttp3_idtr.
471cb0ef41Sopenharmony_ci */
481cb0ef41Sopenharmony_cistatic uint64_t id_from_stream_id(int64_t stream_id) {
491cb0ef41Sopenharmony_ci  return (uint64_t)(stream_id >> 2);
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciint nghttp3_idtr_open(nghttp3_idtr *idtr, int64_t stream_id) {
531cb0ef41Sopenharmony_ci  uint64_t q;
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  assert((idtr->server && (stream_id % 2)) ||
561cb0ef41Sopenharmony_ci         (!idtr->server && (stream_id % 2)) == 0);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  q = id_from_stream_id(stream_id);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  if (nghttp3_gaptr_is_pushed(&idtr->gap, q, 1)) {
611cb0ef41Sopenharmony_ci    return NGHTTP3_ERR_STREAM_IN_USE;
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  return nghttp3_gaptr_push(&idtr->gap, q, 1);
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciint nghttp3_idtr_is_open(nghttp3_idtr *idtr, int64_t stream_id) {
681cb0ef41Sopenharmony_ci  uint64_t q;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  assert((idtr->server && (stream_id % 2)) ||
711cb0ef41Sopenharmony_ci         (!idtr->server && (stream_id % 2)) == 0);
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  q = id_from_stream_id(stream_id);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  return nghttp3_gaptr_is_pushed(&idtr->gap, q, 1);
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciuint64_t nghttp3_idtr_first_gap(nghttp3_idtr *idtr) {
791cb0ef41Sopenharmony_ci  return nghttp3_gaptr_first_gap_offset(&idtr->gap);
801cb0ef41Sopenharmony_ci}
81