Path: blob/main/cddl/contrib/opensolaris/tools/ctf/cvt/fifo.c
39586 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2002 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* Routines for manipulating a FIFO queue30*/3132#include <stdlib.h>3334#include "fifo.h"35#include "memory.h"3637typedef struct fifonode {38void *fn_data;39struct fifonode *fn_next;40} fifonode_t;4142struct fifo {43fifonode_t *f_head;44fifonode_t *f_tail;45};4647fifo_t *48fifo_new(void)49{50fifo_t *f;5152f = xcalloc(sizeof (fifo_t));5354return (f);55}5657/* Add to the end of the fifo */58void59fifo_add(fifo_t *f, void *data)60{61fifonode_t *fn = xmalloc(sizeof (fifonode_t));6263fn->fn_data = data;64fn->fn_next = NULL;6566if (f->f_tail == NULL)67f->f_head = f->f_tail = fn;68else {69f->f_tail->fn_next = fn;70f->f_tail = fn;71}72}7374/* Remove from the front of the fifo */75void *76fifo_remove(fifo_t *f)77{78fifonode_t *fn;79void *data;8081if ((fn = f->f_head) == NULL)82return (NULL);8384data = fn->fn_data;85if ((f->f_head = fn->fn_next) == NULL)86f->f_tail = NULL;8788free(fn);8990return (data);91}9293/*ARGSUSED*/94static void95fifo_nullfree(void *arg)96{97/* this function intentionally left blank */98}99100/* Free an entire fifo */101void102fifo_free(fifo_t *f, void (*freefn)(void *))103{104fifonode_t *fn = f->f_head;105fifonode_t *tmp;106107if (freefn == NULL)108freefn = fifo_nullfree;109110while (fn) {111(*freefn)(fn->fn_data);112113tmp = fn;114fn = fn->fn_next;115free(tmp);116}117118free(f);119}120121int122fifo_len(fifo_t *f)123{124fifonode_t *fn;125int i;126127for (i = 0, fn = f->f_head; fn; fn = fn->fn_next, i++);128129return (i);130}131132int133fifo_empty(fifo_t *f)134{135return (f->f_head == NULL);136}137138int139fifo_iter(fifo_t *f, int (*iter)(void *data, void *arg), void *arg)140{141fifonode_t *fn;142int rc;143int ret = 0;144145for (fn = f->f_head; fn; fn = fn->fn_next) {146if ((rc = iter(fn->fn_data, arg)) < 0)147return (-1);148ret += rc;149}150151return (ret);152}153154155