queue.c revision 2f6e87e64736666857c1bbe2cb0692c1f4e56508
1/*
2 * queue.c, queue
3 *
4 * Copyright (c) 2009-2010 Wind River Systems, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <stdlib.h>
20
21#include <queue.h>
22
23inline void __queue_init(struct queue *queue)
24{
25	queue->head = NULL;
26	queue->tail = NULL;
27	queue->length = 0;
28}
29
30struct queue *queue_alloc(void)
31{
32	struct queue *queue;
33
34	queue = malloc(sizeof(struct queue));
35	if (queue)
36		__queue_init(queue);
37
38	return queue;
39}
40
41inline void __queue_free(struct queue *queue)
42{
43	free(queue);
44}
45
46void queue_free_all(struct queue *queue)
47{
48	struct list *list = queue->head;
49
50	list_free_all(list);
51	__queue_init(queue);
52}
53
54void __queue_push_head(struct queue *queue, struct list *entry)
55{
56	queue->head = __list_add_head(queue->head, entry);
57	if (!queue->tail)
58		queue->tail = queue->head;
59
60	queue->length++;
61}
62
63int queue_push_head(struct queue *queue, void *data)
64{
65	struct list *entry = list_alloc(data);
66
67	if (!entry)
68		return -1;
69	else
70		queue->head = __list_add_head(queue->head, entry);
71
72	if (!queue->tail)
73		queue->tail = queue->head;
74
75	queue->length++;
76        return 0;
77}
78
79void __queue_push_tail(struct queue *queue, struct list *entry)
80{
81	queue->tail = list_add_tail(queue->tail, entry);
82	if (queue->tail->next)
83		queue->tail = queue->tail->next;
84	else
85		queue->head = queue->tail;
86
87	queue->length++;
88}
89
90int queue_push_tail(struct queue *queue, void *data)
91{
92	struct list *entry = list_alloc(data);
93
94	if (!entry)
95		return -1;
96	else
97		queue->tail = __list_add_tail(queue->tail, entry);
98
99	if (queue->tail->next)
100		queue->tail = queue->tail->next;
101	else
102		queue->head = queue->tail;
103
104	queue->length++;
105        return 0;
106}
107
108struct list *__queue_pop_head(struct queue *queue)
109{
110	struct list *entry = queue->head;
111
112	if (entry) {
113		queue->head = __list_remove(queue->head, entry);
114		if (!queue->head)
115			queue->tail = NULL;
116
117		queue->length--;
118	}
119
120	return entry;
121}
122
123void *queue_pop_head(struct queue *queue)
124{
125	struct list *entry;
126	void *data = NULL;
127
128	entry = __queue_pop_head(queue);
129	if (entry) {
130		data = entry->data;
131		__list_free(entry);
132	}
133
134	return data;
135}
136
137struct list *__queue_pop_tail(struct queue *queue)
138{
139	struct list *entry = queue->tail;
140	struct list *prev;
141
142	if (entry) {
143		prev = entry->prev;
144		queue->head = __list_remove(queue->head, entry);
145		queue->tail = prev;
146		queue->length--;
147	}
148
149	return entry;
150}
151
152void *queue_pop_tail(struct queue *queue)
153{
154	struct list *entry;
155	void *data = NULL;
156
157	entry = __queue_pop_tail(queue);
158	if (entry) {
159		data = entry->data;
160		__list_free(entry);
161	}
162
163	return data;
164}
165
166inline struct list *__queue_peek_head(struct queue *queue)
167{
168	return queue->head;
169}
170
171inline struct list *__queue_peek_tail(struct queue *queue)
172{
173	return queue->tail;
174}
175
176inline void *queue_peek_head(struct queue *queue)
177{
178	struct list *entry;
179	void *data = NULL;
180
181	entry = __queue_peek_head(queue);
182	if (entry)
183		data = entry->data;
184
185	return data;
186}
187
188inline void *queue_peek_tail(struct queue *queue)
189{
190	struct list *entry;
191	void *data = NULL;
192
193	entry = __queue_peek_tail(queue);
194	if (entry)
195		data = entry->data;
196
197	return data;
198}
199
200int queue_length(struct queue *queue)
201{
202	return queue->length;
203}
204