1*1673e404SJohn Birrell /*
2*1673e404SJohn Birrell * CDDL HEADER START
3*1673e404SJohn Birrell *
4*1673e404SJohn Birrell * The contents of this file are subject to the terms of the
5*1673e404SJohn Birrell * Common Development and Distribution License, Version 1.0 only
6*1673e404SJohn Birrell * (the "License"). You may not use this file except in compliance
7*1673e404SJohn Birrell * with the License.
8*1673e404SJohn Birrell *
9*1673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*1673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing.
11*1673e404SJohn Birrell * See the License for the specific language governing permissions
12*1673e404SJohn Birrell * and limitations under the License.
13*1673e404SJohn Birrell *
14*1673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
15*1673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*1673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
17*1673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
18*1673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
19*1673e404SJohn Birrell *
20*1673e404SJohn Birrell * CDDL HEADER END
21*1673e404SJohn Birrell */
22*1673e404SJohn Birrell /*
23*1673e404SJohn Birrell * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24*1673e404SJohn Birrell * Use is subject to license terms.
25*1673e404SJohn Birrell */
26*1673e404SJohn Birrell
27*1673e404SJohn Birrell #pragma ident "%Z%%M% %I% %E% SMI"
28*1673e404SJohn Birrell
29*1673e404SJohn Birrell /*
30*1673e404SJohn Birrell * Routines for manipulating a FIFO queue
31*1673e404SJohn Birrell */
32*1673e404SJohn Birrell
33*1673e404SJohn Birrell #include <stdlib.h>
34*1673e404SJohn Birrell
35*1673e404SJohn Birrell #include "fifo.h"
36*1673e404SJohn Birrell #include "memory.h"
37*1673e404SJohn Birrell
38*1673e404SJohn Birrell typedef struct fifonode {
39*1673e404SJohn Birrell void *fn_data;
40*1673e404SJohn Birrell struct fifonode *fn_next;
41*1673e404SJohn Birrell } fifonode_t;
42*1673e404SJohn Birrell
43*1673e404SJohn Birrell struct fifo {
44*1673e404SJohn Birrell fifonode_t *f_head;
45*1673e404SJohn Birrell fifonode_t *f_tail;
46*1673e404SJohn Birrell };
47*1673e404SJohn Birrell
48*1673e404SJohn Birrell fifo_t *
fifo_new(void)49*1673e404SJohn Birrell fifo_new(void)
50*1673e404SJohn Birrell {
51*1673e404SJohn Birrell fifo_t *f;
52*1673e404SJohn Birrell
53*1673e404SJohn Birrell f = xcalloc(sizeof (fifo_t));
54*1673e404SJohn Birrell
55*1673e404SJohn Birrell return (f);
56*1673e404SJohn Birrell }
57*1673e404SJohn Birrell
58*1673e404SJohn Birrell /* Add to the end of the fifo */
59*1673e404SJohn Birrell void
fifo_add(fifo_t * f,void * data)60*1673e404SJohn Birrell fifo_add(fifo_t *f, void *data)
61*1673e404SJohn Birrell {
62*1673e404SJohn Birrell fifonode_t *fn = xmalloc(sizeof (fifonode_t));
63*1673e404SJohn Birrell
64*1673e404SJohn Birrell fn->fn_data = data;
65*1673e404SJohn Birrell fn->fn_next = NULL;
66*1673e404SJohn Birrell
67*1673e404SJohn Birrell if (f->f_tail == NULL)
68*1673e404SJohn Birrell f->f_head = f->f_tail = fn;
69*1673e404SJohn Birrell else {
70*1673e404SJohn Birrell f->f_tail->fn_next = fn;
71*1673e404SJohn Birrell f->f_tail = fn;
72*1673e404SJohn Birrell }
73*1673e404SJohn Birrell }
74*1673e404SJohn Birrell
75*1673e404SJohn Birrell /* Remove from the front of the fifo */
76*1673e404SJohn Birrell void *
fifo_remove(fifo_t * f)77*1673e404SJohn Birrell fifo_remove(fifo_t *f)
78*1673e404SJohn Birrell {
79*1673e404SJohn Birrell fifonode_t *fn;
80*1673e404SJohn Birrell void *data;
81*1673e404SJohn Birrell
82*1673e404SJohn Birrell if ((fn = f->f_head) == NULL)
83*1673e404SJohn Birrell return (NULL);
84*1673e404SJohn Birrell
85*1673e404SJohn Birrell data = fn->fn_data;
86*1673e404SJohn Birrell if ((f->f_head = fn->fn_next) == NULL)
87*1673e404SJohn Birrell f->f_tail = NULL;
88*1673e404SJohn Birrell
89*1673e404SJohn Birrell free(fn);
90*1673e404SJohn Birrell
91*1673e404SJohn Birrell return (data);
92*1673e404SJohn Birrell }
93*1673e404SJohn Birrell
94*1673e404SJohn Birrell /*ARGSUSED*/
95*1673e404SJohn Birrell static void
fifo_nullfree(void * arg)96*1673e404SJohn Birrell fifo_nullfree(void *arg)
97*1673e404SJohn Birrell {
98*1673e404SJohn Birrell /* this function intentionally left blank */
99*1673e404SJohn Birrell }
100*1673e404SJohn Birrell
101*1673e404SJohn Birrell /* Free an entire fifo */
102*1673e404SJohn Birrell void
fifo_free(fifo_t * f,void (* freefn)(void *))103*1673e404SJohn Birrell fifo_free(fifo_t *f, void (*freefn)(void *))
104*1673e404SJohn Birrell {
105*1673e404SJohn Birrell fifonode_t *fn = f->f_head;
106*1673e404SJohn Birrell fifonode_t *tmp;
107*1673e404SJohn Birrell
108*1673e404SJohn Birrell if (freefn == NULL)
109*1673e404SJohn Birrell freefn = fifo_nullfree;
110*1673e404SJohn Birrell
111*1673e404SJohn Birrell while (fn) {
112*1673e404SJohn Birrell (*freefn)(fn->fn_data);
113*1673e404SJohn Birrell
114*1673e404SJohn Birrell tmp = fn;
115*1673e404SJohn Birrell fn = fn->fn_next;
116*1673e404SJohn Birrell free(tmp);
117*1673e404SJohn Birrell }
118*1673e404SJohn Birrell
119*1673e404SJohn Birrell free(f);
120*1673e404SJohn Birrell }
121*1673e404SJohn Birrell
122*1673e404SJohn Birrell int
fifo_len(fifo_t * f)123*1673e404SJohn Birrell fifo_len(fifo_t *f)
124*1673e404SJohn Birrell {
125*1673e404SJohn Birrell fifonode_t *fn;
126*1673e404SJohn Birrell int i;
127*1673e404SJohn Birrell
128*1673e404SJohn Birrell for (i = 0, fn = f->f_head; fn; fn = fn->fn_next, i++);
129*1673e404SJohn Birrell
130*1673e404SJohn Birrell return (i);
131*1673e404SJohn Birrell }
132*1673e404SJohn Birrell
133*1673e404SJohn Birrell int
fifo_empty(fifo_t * f)134*1673e404SJohn Birrell fifo_empty(fifo_t *f)
135*1673e404SJohn Birrell {
136*1673e404SJohn Birrell return (f->f_head == NULL);
137*1673e404SJohn Birrell }
138*1673e404SJohn Birrell
139*1673e404SJohn Birrell int
fifo_iter(fifo_t * f,int (* iter)(void * data,void * arg),void * arg)140*1673e404SJohn Birrell fifo_iter(fifo_t *f, int (*iter)(void *data, void *arg), void *arg)
141*1673e404SJohn Birrell {
142*1673e404SJohn Birrell fifonode_t *fn;
143*1673e404SJohn Birrell int rc;
144*1673e404SJohn Birrell int ret = 0;
145*1673e404SJohn Birrell
146*1673e404SJohn Birrell for (fn = f->f_head; fn; fn = fn->fn_next) {
147*1673e404SJohn Birrell if ((rc = iter(fn->fn_data, arg)) < 0)
148*1673e404SJohn Birrell return (-1);
149*1673e404SJohn Birrell ret += rc;
150*1673e404SJohn Birrell }
151*1673e404SJohn Birrell
152*1673e404SJohn Birrell return (ret);
153*1673e404SJohn Birrell }
154