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_ringbuf.h"
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#include <assert.h>
291cb0ef41Sopenharmony_ci#include <string.h>
301cb0ef41Sopenharmony_ci#ifdef WIN32
311cb0ef41Sopenharmony_ci#  include <intrin.h>
321cb0ef41Sopenharmony_ci#endif
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#include "nghttp3_macro.h"
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#if defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || defined(_M_ARM64))
371cb0ef41Sopenharmony_ciunsigned int __popcnt(unsigned int x) {
381cb0ef41Sopenharmony_ci  unsigned int c = 0;
391cb0ef41Sopenharmony_ci  for (; x; ++c) {
401cb0ef41Sopenharmony_ci    x &= x - 1;
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci  return c;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci#endif
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciint nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size,
471cb0ef41Sopenharmony_ci                         const nghttp3_mem *mem) {
481cb0ef41Sopenharmony_ci  if (nmemb) {
491cb0ef41Sopenharmony_ci#ifdef WIN32
501cb0ef41Sopenharmony_ci    assert(1 == __popcnt((unsigned int)nmemb));
511cb0ef41Sopenharmony_ci#else
521cb0ef41Sopenharmony_ci    assert(1 == __builtin_popcount((unsigned int)nmemb));
531cb0ef41Sopenharmony_ci#endif
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    rb->buf = nghttp3_mem_malloc(mem, nmemb * size);
561cb0ef41Sopenharmony_ci    if (rb->buf == NULL) {
571cb0ef41Sopenharmony_ci      return NGHTTP3_ERR_NOMEM;
581cb0ef41Sopenharmony_ci    }
591cb0ef41Sopenharmony_ci  } else {
601cb0ef41Sopenharmony_ci    rb->buf = NULL;
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  rb->mem = mem;
641cb0ef41Sopenharmony_ci  rb->nmemb = nmemb;
651cb0ef41Sopenharmony_ci  rb->size = size;
661cb0ef41Sopenharmony_ci  rb->first = 0;
671cb0ef41Sopenharmony_ci  rb->len = 0;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  return 0;
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_civoid nghttp3_ringbuf_free(nghttp3_ringbuf *rb) {
731cb0ef41Sopenharmony_ci  if (rb == NULL) {
741cb0ef41Sopenharmony_ci    return;
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  nghttp3_mem_free(rb->mem, rb->buf);
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_civoid *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb) {
811cb0ef41Sopenharmony_ci  rb->first = (rb->first - 1) & (rb->nmemb - 1);
821cb0ef41Sopenharmony_ci  rb->len = nghttp3_min(rb->nmemb, rb->len + 1);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  return (void *)&rb->buf[rb->first * rb->size];
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_civoid *nghttp3_ringbuf_push_back(nghttp3_ringbuf *rb) {
881cb0ef41Sopenharmony_ci  size_t offset = (rb->first + rb->len) & (rb->nmemb - 1);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  if (rb->len == rb->nmemb) {
911cb0ef41Sopenharmony_ci    rb->first = (rb->first + 1) & (rb->nmemb - 1);
921cb0ef41Sopenharmony_ci  } else {
931cb0ef41Sopenharmony_ci    ++rb->len;
941cb0ef41Sopenharmony_ci  }
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  return (void *)&rb->buf[offset * rb->size];
971cb0ef41Sopenharmony_ci}
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_civoid nghttp3_ringbuf_pop_front(nghttp3_ringbuf *rb) {
1001cb0ef41Sopenharmony_ci  rb->first = (rb->first + 1) & (rb->nmemb - 1);
1011cb0ef41Sopenharmony_ci  --rb->len;
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_civoid nghttp3_ringbuf_pop_back(nghttp3_ringbuf *rb) {
1051cb0ef41Sopenharmony_ci  assert(rb->len);
1061cb0ef41Sopenharmony_ci  --rb->len;
1071cb0ef41Sopenharmony_ci}
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_civoid nghttp3_ringbuf_resize(nghttp3_ringbuf *rb, size_t len) {
1101cb0ef41Sopenharmony_ci  assert(len <= rb->nmemb);
1111cb0ef41Sopenharmony_ci  rb->len = len;
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_civoid *nghttp3_ringbuf_get(nghttp3_ringbuf *rb, size_t offset) {
1151cb0ef41Sopenharmony_ci  assert(offset < rb->len);
1161cb0ef41Sopenharmony_ci  offset = (rb->first + offset) & (rb->nmemb - 1);
1171cb0ef41Sopenharmony_ci  return &rb->buf[offset * rb->size];
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ciint nghttp3_ringbuf_full(nghttp3_ringbuf *rb) { return rb->len == rb->nmemb; }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ciint nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb) {
1231cb0ef41Sopenharmony_ci  uint8_t *buf;
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  if (rb->nmemb >= nmemb) {
1261cb0ef41Sopenharmony_ci    return 0;
1271cb0ef41Sopenharmony_ci  }
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci#ifdef WIN32
1301cb0ef41Sopenharmony_ci  assert(1 == __popcnt((unsigned int)nmemb));
1311cb0ef41Sopenharmony_ci#else
1321cb0ef41Sopenharmony_ci  assert(1 == __builtin_popcount((unsigned int)nmemb));
1331cb0ef41Sopenharmony_ci#endif
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  buf = nghttp3_mem_malloc(rb->mem, nmemb * rb->size);
1361cb0ef41Sopenharmony_ci  if (buf == NULL) {
1371cb0ef41Sopenharmony_ci    return NGHTTP3_ERR_NOMEM;
1381cb0ef41Sopenharmony_ci  }
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  if (rb->buf != NULL) {
1411cb0ef41Sopenharmony_ci    if (rb->first + rb->len <= rb->nmemb) {
1421cb0ef41Sopenharmony_ci      memcpy(buf, rb->buf + rb->first * rb->size, rb->len * rb->size);
1431cb0ef41Sopenharmony_ci      rb->first = 0;
1441cb0ef41Sopenharmony_ci    } else {
1451cb0ef41Sopenharmony_ci      memcpy(buf, rb->buf + rb->first * rb->size,
1461cb0ef41Sopenharmony_ci             (rb->nmemb - rb->first) * rb->size);
1471cb0ef41Sopenharmony_ci      memcpy(buf + (rb->nmemb - rb->first) * rb->size, rb->buf,
1481cb0ef41Sopenharmony_ci             (rb->len - (rb->nmemb - rb->first)) * rb->size);
1491cb0ef41Sopenharmony_ci      rb->first = 0;
1501cb0ef41Sopenharmony_ci    }
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci    nghttp3_mem_free(rb->mem, rb->buf);
1531cb0ef41Sopenharmony_ci  }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  rb->buf = buf;
1561cb0ef41Sopenharmony_ci  rb->nmemb = nmemb;
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  return 0;
1591cb0ef41Sopenharmony_ci}
160