xref: /freebsd/cddl/contrib/opensolaris/tools/ctf/cvt/fifo.c (revision 1673e4046da975ab0e2bf67d45499930ebab0dbe)
11673e404SJohn Birrell /*
21673e404SJohn Birrell  * CDDL HEADER START
31673e404SJohn Birrell  *
41673e404SJohn Birrell  * The contents of this file are subject to the terms of the
51673e404SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
61673e404SJohn Birrell  * (the "License").  You may not use this file except in compliance
71673e404SJohn Birrell  * with the License.
81673e404SJohn Birrell  *
91673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
101673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
111673e404SJohn Birrell  * See the License for the specific language governing permissions
121673e404SJohn Birrell  * and limitations under the License.
131673e404SJohn Birrell  *
141673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
151673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
161673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
171673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
181673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
191673e404SJohn Birrell  *
201673e404SJohn Birrell  * CDDL HEADER END
211673e404SJohn Birrell  */
221673e404SJohn Birrell /*
231673e404SJohn Birrell  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
241673e404SJohn Birrell  * Use is subject to license terms.
251673e404SJohn Birrell  */
261673e404SJohn Birrell 
271673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
281673e404SJohn Birrell 
291673e404SJohn Birrell /*
301673e404SJohn Birrell  * Routines for manipulating a FIFO queue
311673e404SJohn Birrell  */
321673e404SJohn Birrell 
331673e404SJohn Birrell #include <stdlib.h>
341673e404SJohn Birrell 
351673e404SJohn Birrell #include "fifo.h"
361673e404SJohn Birrell #include "memory.h"
371673e404SJohn Birrell 
381673e404SJohn Birrell typedef struct fifonode {
391673e404SJohn Birrell 	void *fn_data;
401673e404SJohn Birrell 	struct fifonode *fn_next;
411673e404SJohn Birrell } fifonode_t;
421673e404SJohn Birrell 
431673e404SJohn Birrell struct fifo {
441673e404SJohn Birrell 	fifonode_t *f_head;
451673e404SJohn Birrell 	fifonode_t *f_tail;
461673e404SJohn Birrell };
471673e404SJohn Birrell 
481673e404SJohn Birrell fifo_t *
fifo_new(void)491673e404SJohn Birrell fifo_new(void)
501673e404SJohn Birrell {
511673e404SJohn Birrell 	fifo_t *f;
521673e404SJohn Birrell 
531673e404SJohn Birrell 	f = xcalloc(sizeof (fifo_t));
541673e404SJohn Birrell 
551673e404SJohn Birrell 	return (f);
561673e404SJohn Birrell }
571673e404SJohn Birrell 
581673e404SJohn Birrell /* Add to the end of the fifo */
591673e404SJohn Birrell void
fifo_add(fifo_t * f,void * data)601673e404SJohn Birrell fifo_add(fifo_t *f, void *data)
611673e404SJohn Birrell {
621673e404SJohn Birrell 	fifonode_t *fn = xmalloc(sizeof (fifonode_t));
631673e404SJohn Birrell 
641673e404SJohn Birrell 	fn->fn_data = data;
651673e404SJohn Birrell 	fn->fn_next = NULL;
661673e404SJohn Birrell 
671673e404SJohn Birrell 	if (f->f_tail == NULL)
681673e404SJohn Birrell 		f->f_head = f->f_tail = fn;
691673e404SJohn Birrell 	else {
701673e404SJohn Birrell 		f->f_tail->fn_next = fn;
711673e404SJohn Birrell 		f->f_tail = fn;
721673e404SJohn Birrell 	}
731673e404SJohn Birrell }
741673e404SJohn Birrell 
751673e404SJohn Birrell /* Remove from the front of the fifo */
761673e404SJohn Birrell void *
fifo_remove(fifo_t * f)771673e404SJohn Birrell fifo_remove(fifo_t *f)
781673e404SJohn Birrell {
791673e404SJohn Birrell 	fifonode_t *fn;
801673e404SJohn Birrell 	void *data;
811673e404SJohn Birrell 
821673e404SJohn Birrell 	if ((fn = f->f_head) == NULL)
831673e404SJohn Birrell 		return (NULL);
841673e404SJohn Birrell 
851673e404SJohn Birrell 	data = fn->fn_data;
861673e404SJohn Birrell 	if ((f->f_head = fn->fn_next) == NULL)
871673e404SJohn Birrell 		f->f_tail = NULL;
881673e404SJohn Birrell 
891673e404SJohn Birrell 	free(fn);
901673e404SJohn Birrell 
911673e404SJohn Birrell 	return (data);
921673e404SJohn Birrell }
931673e404SJohn Birrell 
941673e404SJohn Birrell /*ARGSUSED*/
951673e404SJohn Birrell static void
fifo_nullfree(void * arg)961673e404SJohn Birrell fifo_nullfree(void *arg)
971673e404SJohn Birrell {
981673e404SJohn Birrell 	/* this function intentionally left blank */
991673e404SJohn Birrell }
1001673e404SJohn Birrell 
1011673e404SJohn Birrell /* Free an entire fifo */
1021673e404SJohn Birrell void
fifo_free(fifo_t * f,void (* freefn)(void *))1031673e404SJohn Birrell fifo_free(fifo_t *f, void (*freefn)(void *))
1041673e404SJohn Birrell {
1051673e404SJohn Birrell 	fifonode_t *fn = f->f_head;
1061673e404SJohn Birrell 	fifonode_t *tmp;
1071673e404SJohn Birrell 
1081673e404SJohn Birrell 	if (freefn == NULL)
1091673e404SJohn Birrell 		freefn = fifo_nullfree;
1101673e404SJohn Birrell 
1111673e404SJohn Birrell 	while (fn) {
1121673e404SJohn Birrell 		(*freefn)(fn->fn_data);
1131673e404SJohn Birrell 
1141673e404SJohn Birrell 		tmp = fn;
1151673e404SJohn Birrell 		fn = fn->fn_next;
1161673e404SJohn Birrell 		free(tmp);
1171673e404SJohn Birrell 	}
1181673e404SJohn Birrell 
1191673e404SJohn Birrell 	free(f);
1201673e404SJohn Birrell }
1211673e404SJohn Birrell 
1221673e404SJohn Birrell int
fifo_len(fifo_t * f)1231673e404SJohn Birrell fifo_len(fifo_t *f)
1241673e404SJohn Birrell {
1251673e404SJohn Birrell 	fifonode_t *fn;
1261673e404SJohn Birrell 	int i;
1271673e404SJohn Birrell 
1281673e404SJohn Birrell 	for (i = 0, fn = f->f_head; fn; fn = fn->fn_next, i++);
1291673e404SJohn Birrell 
1301673e404SJohn Birrell 	return (i);
1311673e404SJohn Birrell }
1321673e404SJohn Birrell 
1331673e404SJohn Birrell int
fifo_empty(fifo_t * f)1341673e404SJohn Birrell fifo_empty(fifo_t *f)
1351673e404SJohn Birrell {
1361673e404SJohn Birrell 	return (f->f_head == NULL);
1371673e404SJohn Birrell }
1381673e404SJohn Birrell 
1391673e404SJohn Birrell int
fifo_iter(fifo_t * f,int (* iter)(void * data,void * arg),void * arg)1401673e404SJohn Birrell fifo_iter(fifo_t *f, int (*iter)(void *data, void *arg), void *arg)
1411673e404SJohn Birrell {
1421673e404SJohn Birrell 	fifonode_t *fn;
1431673e404SJohn Birrell 	int rc;
1441673e404SJohn Birrell 	int ret = 0;
1451673e404SJohn Birrell 
1461673e404SJohn Birrell 	for (fn = f->f_head; fn; fn = fn->fn_next) {
1471673e404SJohn Birrell 		if ((rc = iter(fn->fn_data, arg)) < 0)
1481673e404SJohn Birrell 			return (-1);
1491673e404SJohn Birrell 		ret += rc;
1501673e404SJohn Birrell 	}
1511673e404SJohn Birrell 
1521673e404SJohn Birrell 	return (ret);
1531673e404SJohn Birrell }
154