Path: blob/master/Utilities/cmnghttp2/lib/nghttp2_queue.c
3153 views
/*1* nghttp2 - HTTP/2 C Library2*3* Copyright (c) 2012 Tatsuhiro Tsujikawa4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sublicense, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice shall be14* included in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE20* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*/24#include "nghttp2_queue.h"2526#include <string.h>27#include <assert.h>2829void nghttp2_queue_init(nghttp2_queue *queue) {30queue->front = queue->back = NULL;31}3233void nghttp2_queue_free(nghttp2_queue *queue) {34if (!queue) {35return;36} else {37nghttp2_queue_cell *p = queue->front;38while (p) {39nghttp2_queue_cell *next = p->next;40free(p);41p = next;42}43}44}4546int nghttp2_queue_push(nghttp2_queue *queue, void *data) {47nghttp2_queue_cell *new_cell =48(nghttp2_queue_cell *)malloc(sizeof(nghttp2_queue_cell));49if (!new_cell) {50return NGHTTP2_ERR_NOMEM;51}52new_cell->data = data;53new_cell->next = NULL;54if (queue->back) {55queue->back->next = new_cell;56queue->back = new_cell;5758} else {59queue->front = queue->back = new_cell;60}61return 0;62}6364void nghttp2_queue_pop(nghttp2_queue *queue) {65nghttp2_queue_cell *front = queue->front;66assert(front);67queue->front = front->next;68if (front == queue->back) {69queue->back = NULL;70}71free(front);72}7374void *nghttp2_queue_front(nghttp2_queue *queue) {75assert(queue->front);76return queue->front->data;77}7879void *nghttp2_queue_back(nghttp2_queue *queue) {80assert(queue->back);81return queue->back->data;82}8384int nghttp2_queue_empty(nghttp2_queue *queue) { return queue->front == NULL; }858687