nghttp2_queue.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include "nghttp2_queue.h"
  5. #include <string.h>
  6. #include <assert.h>
  7. #ifdef INFRA_MEM_STATS
  8. #include "infra_mem_stats.h"
  9. #endif
  10. extern void *HAL_Malloc(uint32_t size);
  11. extern void *HAL_Realloc(void *ptr, uint32_t size);
  12. extern void HAL_Free(void *ptr);
  13. #if INFRA_MEM_STATS
  14. #define NGHTTP2_QUEUE_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "nghttp2.queue")
  15. #define NGHTTP2_QUEUE_FREE(ptr) LITE_free(ptr)
  16. #else
  17. #define NGHTTP2_QUEUE_MALLOC(size) HAL_Malloc(size)
  18. #define NGHTTP2_QUEUE_FREE(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
  19. #endif
  20. void nghttp2_queue_init(nghttp2_queue *queue) {
  21. queue->front = queue->back = NULL;
  22. }
  23. void nghttp2_queue_free(nghttp2_queue *queue) {
  24. if (!queue) {
  25. return;
  26. } else {
  27. nghttp2_queue_cell *p = queue->front;
  28. while (p) {
  29. nghttp2_queue_cell *next = p->next;
  30. NGHTTP2_QUEUE_FREE(p);
  31. p = next;
  32. }
  33. }
  34. }
  35. int nghttp2_queue_push(nghttp2_queue *queue, void *data) {
  36. nghttp2_queue_cell *new_cell =
  37. (nghttp2_queue_cell *)NGHTTP2_QUEUE_MALLOC(sizeof(nghttp2_queue_cell));
  38. if (!new_cell) {
  39. return NGHTTP2_ERR_NOMEM;
  40. }
  41. new_cell->data = data;
  42. new_cell->next = NULL;
  43. if (queue->back) {
  44. queue->back->next = new_cell;
  45. queue->back = new_cell;
  46. } else {
  47. queue->front = queue->back = new_cell;
  48. }
  49. return 0;
  50. }
  51. void nghttp2_queue_pop(nghttp2_queue *queue) {
  52. nghttp2_queue_cell *front = queue->front;
  53. assert(front);
  54. queue->front = front->next;
  55. if (front == queue->back) {
  56. queue->back = NULL;
  57. }
  58. NGHTTP2_QUEUE_FREE(front);
  59. }
  60. void *nghttp2_queue_front(nghttp2_queue *queue) {
  61. assert(queue->front);
  62. return queue->front->data;
  63. }
  64. void *nghttp2_queue_back(nghttp2_queue *queue) {
  65. assert(queue->back);
  66. return queue->back->data;
  67. }
  68. int nghttp2_queue_empty(nghttp2_queue *queue) { return queue->front == NULL; }