File: queue.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (212 lines) | stat: -rw-r--r-- 3,637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 1999 The University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * Contributed by the Computer Security Research division,
 * INFOSEC Research and Technology Office, NSA.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/* FLASK */

/*
 * queue.c
 * 
 * Implementation of a generic queue type.
 */

#include "queue.h"

queue_t 
queue_create(void)
{
	queue_t         q;

	q = (queue_t) MALLOC(sizeof(struct queue_info_t));
	if (q == NULL)
		return NULL;

	q->head = q->tail = NULL;

	return q;
}

int 
queue_insert(queue_t q, queue_element_t e)
{
	queue_node_ptr_t newnode;


	if (!q)
		return -1;

	newnode = (queue_node_ptr_t) MALLOC(sizeof(struct queue_node_t));
	if (newnode == NULL)
		return -1;

	newnode->element = e;
	newnode->next = NULL;

	if (q->head == NULL) {
		q->head = q->tail = newnode;
	} else {
		q->tail->next = newnode;
		q->tail = newnode;
	}

	return 0;
}

int 
queue_push(queue_t q, queue_element_t e)
{
	queue_node_ptr_t newnode;


	if (!q)
		return -1;

	newnode = (queue_node_ptr_t) MALLOC(sizeof(struct queue_node_t));
	if (newnode == NULL)
		return -1;

	newnode->element = e;
	newnode->next = NULL;

	if (q->head == NULL) {
		q->head = q->tail = newnode;
	} else {
		newnode->next = q->head;
		q->head = newnode;
	}

	return 0;
}

queue_element_t 
queue_remove(queue_t q)
{
	queue_node_ptr_t node;
	queue_element_t e;


	if (!q)
		return NULL;

	if (q->head == NULL)
		return NULL;

	node = q->head;
	q->head = q->head->next;
	if (q->head == NULL)
		q->tail = NULL;

	e = node->element;
	FREE(node, sizeof(struct queue_node_t));

	return e;
}

queue_element_t 
queue_head(queue_t q)
{
	if (!q)
		return NULL;

	if (q->head == NULL)
		return NULL;

	return q->head->element;
}

void 
queue_destroy(queue_t q)
{
	queue_node_ptr_t p, temp;


	if (!q)
		return;

	p = q->head;
	while (p != NULL) {
		temp = p;
		p = p->next;
		FREE(temp, sizeof(struct queue_node_t));
	}

	FREE(q, sizeof(struct queue_info_t));
}

int             queue_map(queue_t q, int (*f) (queue_element_t, void *), void *vp) {
	queue_node_ptr_t p;
	int             ret;


	if (!q)
		return 0;

	p = q->head;
	while (p != NULL) {
		ret = f(p->element, vp);
		if (ret)
			return ret;
		p = p->next;
	}
	return 0;
}


void 
queue_map_remove_on_error(queue_t q,
			  int (*f) (queue_element_t, void *),
			  void (*g) (queue_element_t, void *),
			  void *vp)
{
	queue_node_ptr_t p, last, temp;
	int             ret;


	if (!q)
		return;

	last = NULL;
	p = q->head;
	while (p != NULL) {
		ret = f(p->element, vp);
		if (ret) {
			if (last) {
				last->next = p->next;
				if (last->next == NULL)
					q->tail = last;
			} else {
				q->head = p->next;
				if (q->head == NULL)
					q->tail = NULL;
			}

			temp = p;
			p = p->next;
			g(temp->element, vp);
			FREE(temp, sizeof(struct queue_node_t));
		} else {
			last = p;
			p = p->next;
		}
	}

	return;
}


/* FLASK */