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
27 #include <string.h>
28 #include <assert.h>
29 #ifdef INFRA_MEM_STATS
30 #include "infra_mem_stats.h"
31 #endif
32
33 extern void *HAL_Malloc(uint32_t size);
34 extern void HAL_Free(void *ptr);
35
36 #if INFRA_MEM_STATS
37 #define NGHTTP2_QUEUE_MALLOC(size) LITE_malloc(size, MEM_MAGIC, "nghttp2.queue")
38 #define NGHTTP2_QUEUE_FREE(ptr) LITE_free(ptr)
39 #else
40 #define NGHTTP2_QUEUE_MALLOC(size) HAL_Malloc(size)
41 #define NGHTTP2_QUEUE_FREE(ptr) \
42 { \
43 HAL_Free((void *)ptr); \
44 ptr = NULL; \
45 }
46 #endif
47
nghttp2_queue_init(nghttp2_queue * queue)48 void nghttp2_queue_init(nghttp2_queue *queue)
49 {
50 queue->front = queue->back = NULL;
51 }
52
nghttp2_queue_free(nghttp2_queue * queue)53 void nghttp2_queue_free(nghttp2_queue *queue)
54 {
55 if (!queue) {
56 return;
57 } else {
58 nghttp2_queue_cell *p = queue->front;
59 while (p) {
60 nghttp2_queue_cell *next = p->next;
61 NGHTTP2_QUEUE_FREE(p);
62 p = next;
63 }
64 }
65 }
66
nghttp2_queue_push(nghttp2_queue * queue,void * data)67 int nghttp2_queue_push(nghttp2_queue *queue, void *data)
68 {
69 nghttp2_queue_cell *new_cell =
70 (nghttp2_queue_cell *)NGHTTP2_QUEUE_MALLOC(sizeof(nghttp2_queue_cell));
71 if (!new_cell) {
72 return NGHTTP2_ERR_NOMEM;
73 }
74 new_cell->data = data;
75 new_cell->next = NULL;
76 if (queue->back) {
77 queue->back->next = new_cell;
78 queue->back = new_cell;
79
80 } else {
81 queue->front = queue->back = new_cell;
82 }
83 return 0;
84 }
85
nghttp2_queue_pop(nghttp2_queue * queue)86 void nghttp2_queue_pop(nghttp2_queue *queue)
87 {
88 nghttp2_queue_cell *front = queue->front;
89 assert(front);
90 queue->front = front->next;
91 if (front == queue->back) {
92 queue->back = NULL;
93 }
94 NGHTTP2_QUEUE_FREE(front);
95 }
96
nghttp2_queue_front(nghttp2_queue * queue)97 void *nghttp2_queue_front(nghttp2_queue *queue)
98 {
99 assert(queue->front);
100 return queue->front->data;
101 }
102
nghttp2_queue_back(nghttp2_queue * queue)103 void *nghttp2_queue_back(nghttp2_queue *queue)
104 {
105 assert(queue->back);
106 return queue->back->data;
107 }
108
nghttp2_queue_empty(nghttp2_queue * queue)109 int nghttp2_queue_empty(nghttp2_queue *queue)
110 {
111 return queue->front == NULL;
112 }
113