8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / cvt / fifo.c
blobfb9a11fccf5655b0e7190ea0aa0a523c40ff1965
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Routines for manipulating a FIFO queue
33 #include <stdlib.h>
35 #include "fifo.h"
36 #include "memory.h"
38 typedef struct fifonode {
39 void *fn_data;
40 struct fifonode *fn_next;
41 } fifonode_t;
43 struct fifo {
44 fifonode_t *f_head;
45 fifonode_t *f_tail;
48 fifo_t *
49 fifo_new(void)
51 fifo_t *f;
53 f = xcalloc(sizeof (fifo_t));
55 return (f);
58 /* Add to the end of the fifo */
59 void
60 fifo_add(fifo_t *f, void *data)
62 fifonode_t *fn = xmalloc(sizeof (fifonode_t));
64 fn->fn_data = data;
65 fn->fn_next = NULL;
67 if (f->f_tail == NULL)
68 f->f_head = f->f_tail = fn;
69 else {
70 f->f_tail->fn_next = fn;
71 f->f_tail = fn;
75 /* Remove from the front of the fifo */
76 void *
77 fifo_remove(fifo_t *f)
79 fifonode_t *fn;
80 void *data;
82 if ((fn = f->f_head) == NULL)
83 return (NULL);
85 data = fn->fn_data;
86 if ((f->f_head = fn->fn_next) == NULL)
87 f->f_tail = NULL;
89 free(fn);
91 return (data);
94 /*ARGSUSED*/
95 static void
96 fifo_nullfree(void *arg)
98 /* this function intentionally left blank */
101 /* Free an entire fifo */
102 void
103 fifo_free(fifo_t *f, void (*freefn)(void *))
105 fifonode_t *fn = f->f_head;
106 fifonode_t *tmp;
108 if (freefn == NULL)
109 freefn = fifo_nullfree;
111 while (fn) {
112 (*freefn)(fn->fn_data);
114 tmp = fn;
115 fn = fn->fn_next;
116 free(tmp);
119 free(f);
123 fifo_len(fifo_t *f)
125 fifonode_t *fn;
126 int i;
128 for (i = 0, fn = f->f_head; fn; fn = fn->fn_next, i++);
130 return (i);
134 fifo_empty(fifo_t *f)
136 return (f->f_head == NULL);
140 fifo_iter(fifo_t *f, int (*iter)(void *data, void *arg), void *arg)
142 fifonode_t *fn;
143 int rc;
144 int ret = 0;
146 for (fn = f->f_head; fn; fn = fn->fn_next) {
147 if ((rc = iter(fn->fn_data, arg)) < 0)
148 return (-1);
149 ret += rc;
152 return (ret);