nghttp2_queue.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "nghttp2_queue.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. void nghttp2_queue_init(nghttp2_queue *queue) {
  29. queue->front = queue->back = NULL;
  30. }
  31. void nghttp2_queue_free(nghttp2_queue *queue) {
  32. if (!queue) {
  33. return;
  34. } else {
  35. nghttp2_queue_cell *p = queue->front;
  36. while (p) {
  37. nghttp2_queue_cell *next = p->next;
  38. free(p);
  39. p = next;
  40. }
  41. }
  42. }
  43. int nghttp2_queue_push(nghttp2_queue *queue, void *data) {
  44. nghttp2_queue_cell *new_cell =
  45. (nghttp2_queue_cell *)malloc(sizeof(nghttp2_queue_cell));
  46. if (!new_cell) {
  47. return NGHTTP2_ERR_NOMEM;
  48. }
  49. new_cell->data = data;
  50. new_cell->next = NULL;
  51. if (queue->back) {
  52. queue->back->next = new_cell;
  53. queue->back = new_cell;
  54. } else {
  55. queue->front = queue->back = new_cell;
  56. }
  57. return 0;
  58. }
  59. void nghttp2_queue_pop(nghttp2_queue *queue) {
  60. nghttp2_queue_cell *front = queue->front;
  61. assert(front);
  62. queue->front = front->next;
  63. if (front == queue->back) {
  64. queue->back = NULL;
  65. }
  66. free(front);
  67. }
  68. void *nghttp2_queue_front(nghttp2_queue *queue) {
  69. assert(queue->front);
  70. return queue->front->data;
  71. }
  72. void *nghttp2_queue_back(nghttp2_queue *queue) {
  73. assert(queue->back);
  74. return queue->back->data;
  75. }
  76. int nghttp2_queue_empty(nghttp2_queue *queue) { return queue->front == NULL; }