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#include "nghttp2_queue.h"
262c593315Sopenharmony_ci
272c593315Sopenharmony_ci#include <string.h>
282c593315Sopenharmony_ci#include <assert.h>
292c593315Sopenharmony_ci
302c593315Sopenharmony_civoid nghttp2_queue_init(nghttp2_queue *queue) {
312c593315Sopenharmony_ci  queue->front = queue->back = NULL;
322c593315Sopenharmony_ci}
332c593315Sopenharmony_ci
342c593315Sopenharmony_civoid nghttp2_queue_free(nghttp2_queue *queue) {
352c593315Sopenharmony_ci  if (!queue) {
362c593315Sopenharmony_ci    return;
372c593315Sopenharmony_ci  } else {
382c593315Sopenharmony_ci    nghttp2_queue_cell *p = queue->front;
392c593315Sopenharmony_ci    while (p) {
402c593315Sopenharmony_ci      nghttp2_queue_cell *next = p->next;
412c593315Sopenharmony_ci      free(p);
422c593315Sopenharmony_ci      p = next;
432c593315Sopenharmony_ci    }
442c593315Sopenharmony_ci  }
452c593315Sopenharmony_ci}
462c593315Sopenharmony_ci
472c593315Sopenharmony_ciint nghttp2_queue_push(nghttp2_queue *queue, void *data) {
482c593315Sopenharmony_ci  nghttp2_queue_cell *new_cell =
492c593315Sopenharmony_ci      (nghttp2_queue_cell *)malloc(sizeof(nghttp2_queue_cell));
502c593315Sopenharmony_ci  if (!new_cell) {
512c593315Sopenharmony_ci    return NGHTTP2_ERR_NOMEM;
522c593315Sopenharmony_ci  }
532c593315Sopenharmony_ci  new_cell->data = data;
542c593315Sopenharmony_ci  new_cell->next = NULL;
552c593315Sopenharmony_ci  if (queue->back) {
562c593315Sopenharmony_ci    queue->back->next = new_cell;
572c593315Sopenharmony_ci    queue->back = new_cell;
582c593315Sopenharmony_ci
592c593315Sopenharmony_ci  } else {
602c593315Sopenharmony_ci    queue->front = queue->back = new_cell;
612c593315Sopenharmony_ci  }
622c593315Sopenharmony_ci  return 0;
632c593315Sopenharmony_ci}
642c593315Sopenharmony_ci
652c593315Sopenharmony_civoid nghttp2_queue_pop(nghttp2_queue *queue) {
662c593315Sopenharmony_ci  nghttp2_queue_cell *front = queue->front;
672c593315Sopenharmony_ci  assert(front);
682c593315Sopenharmony_ci  queue->front = front->next;
692c593315Sopenharmony_ci  if (front == queue->back) {
702c593315Sopenharmony_ci    queue->back = NULL;
712c593315Sopenharmony_ci  }
722c593315Sopenharmony_ci  free(front);
732c593315Sopenharmony_ci}
742c593315Sopenharmony_ci
752c593315Sopenharmony_civoid *nghttp2_queue_front(nghttp2_queue *queue) {
762c593315Sopenharmony_ci  assert(queue->front);
772c593315Sopenharmony_ci  return queue->front->data;
782c593315Sopenharmony_ci}
792c593315Sopenharmony_ci
802c593315Sopenharmony_civoid *nghttp2_queue_back(nghttp2_queue *queue) {
812c593315Sopenharmony_ci  assert(queue->back);
822c593315Sopenharmony_ci  return queue->back->data;
832c593315Sopenharmony_ci}
842c593315Sopenharmony_ci
852c593315Sopenharmony_ciint nghttp2_queue_empty(nghttp2_queue *queue) { return queue->front == NULL; }
86